diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs new file mode 100644 index 0000000..31d554f --- /dev/null +++ b/Selector.CLI/Program.cs @@ -0,0 +1,42 @@ +using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Selector.CLI +{ + class Program + { + static void Main(string[] args) + { + var serviceProvider = new ServiceCollection() + //.AddLogging(b => + // b.AddFilter("Microsoft", LogLevel.Warning) + // .AddFilter("System", LogLevel.Warning) + // .AddFilter("Selector.CLI.Program", LogLevel.Debug) + // .AddConsole() + //) + .AddTransient() + .AddTransient() + .BuildServiceProvider(); + + //using var loggerFactory = LoggerFactory.Create(builder => + //{ + // builder + // .AddFilter("Microsoft", LogLevel.Warning) + // .AddFilter("System", LogLevel.Warning) + // .AddFilter("Selector.CLI.Program", LogLevel.Debug) + // .AddConsole(); + //}); + //ILogger logger = loggerFactory.CreateLogger(); + //logger.LogInformation("Example log message"); + + //var logger = serviceProvider.GetService() + // .CreateLogger(); + //logger.LogDebug("Starting application"); + + var logger = serviceProvider.GetService(); + + logger.LogDebug("All done!"); + } + } +} diff --git a/Selector.CLI/Selector.CLI.csproj b/Selector.CLI/Selector.CLI.csproj new file mode 100644 index 0000000..8ae1861 --- /dev/null +++ b/Selector.CLI/Selector.CLI.csproj @@ -0,0 +1,21 @@ + + + + Exe + net5.0 + Selector.CLI.Program + latest + + + + + + + + + + + + + + diff --git a/Selector.Tests/Manager.cs b/Selector.Tests/Manager.cs deleted file mode 100644 index 491a218..0000000 --- a/Selector.Tests/Manager.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Xunit; -using Moq; -using FluentAssertions; -using SpotifyAPI.Web; - -using Selector; -using System.Threading.Tasks; - -namespace Selector.Tests -{ - public class ManagerTests - { - [Fact] - public void Count() - { - var manager = new Manager(); - - var watcherMock = new Mock(); - - manager.Add(watcherMock.Object); - manager.Add(watcherMock.Object); - manager.Add(watcherMock.Object); - - manager.Count.Should().Be(3); - } - - [Fact] - public void StartAndStop() - { - var manager = new Manager(); - - var watcherMock = new Mock(); - - manager.Add(watcherMock.Object); - manager.Count.Should().Be(1); - - manager.Start(); - - manager.IsRunning.Should().BeTrue(); - manager.Running.Count().Should().Be(1); - - var context = manager.Running.First(); - - manager.Stop(); - - manager.IsRunning.Should().BeFalse(); - context.IsRunning.Should().BeFalse(); - - manager.Running.Count().Should().Be(0); - manager.TokenSources.First().IsCancellationRequested.Should().BeTrue(); - } - } -} diff --git a/Selector.Tests/PlayerWatcher.cs b/Selector.Tests/Watcher/PlayerWatcher.cs similarity index 99% rename from Selector.Tests/PlayerWatcher.cs rename to Selector.Tests/Watcher/PlayerWatcher.cs index 9e5e842..f3b4609 100644 --- a/Selector.Tests/PlayerWatcher.cs +++ b/Selector.Tests/Watcher/PlayerWatcher.cs @@ -208,6 +208,7 @@ namespace Selector.Tests [Theory] [InlineData(1000, 3500, 4)] [InlineData(500, 3800, 8)] + [InlineData(100, 250, 3)] public async void Watch(int pollPeriod, int execTime, int numberOfCalls) { var spotMock = new Mock(); diff --git a/Selector.Tests/Watcher/WatcherCollection.cs b/Selector.Tests/Watcher/WatcherCollection.cs new file mode 100644 index 0000000..fc4a21e --- /dev/null +++ b/Selector.Tests/Watcher/WatcherCollection.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; +using Moq; +using FluentAssertions; +using SpotifyAPI.Web; + +using Selector; +using System.Threading.Tasks; + +namespace Selector.Tests +{ + public class WatcherCollectionTests + { + [Fact] + public void Count() + { + var watchCollection = new WatcherCollection(); + + var watcherMock = new Mock(); + + watchCollection.Add(watcherMock.Object); + watchCollection.Add(watcherMock.Object); + watchCollection.Add(watcherMock.Object); + + watchCollection.Count.Should().Be(3); + } + + [Fact] + public void StartAndStop() + { + var watchCollection = new WatcherCollection(); + + var watcherMock = new Mock(); + + watchCollection.Add(watcherMock.Object); + watchCollection.Count.Should().Be(1); + + watchCollection.Start(); + + watchCollection.IsRunning.Should().BeTrue(); + watchCollection.First().IsRunning.Should().BeTrue(); + watchCollection.Running.Count().Should().Be(1); + + watchCollection.Stop(); + + watchCollection.IsRunning.Should().BeFalse(); + watchCollection.First().IsRunning.Should().BeFalse(); + + watchCollection.Running.Count().Should().Be(0); + watchCollection.TokenSources.First().IsCancellationRequested.Should().BeTrue(); + } + } +} diff --git a/Selector.sln b/Selector.sln index aa9b1b9..6c91c1f 100644 --- a/Selector.sln +++ b/Selector.sln @@ -5,7 +5,12 @@ VisualStudioVersion = 16.0.30717.126 MinimumVisualStudioVersion = 15.0.26124.0 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Selector", "Selector\Selector.csproj", "{05B22ACE-2EA1-46AA-8483-A625B08A0D01}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Selector.Tests", "Selector.Tests\Selector.Tests.csproj", "{2C33766F-FFEB-4A91-9509-7B543EDB6F93}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Selector.Tests", "Selector.Tests\Selector.Tests.csproj", "{2C33766F-FFEB-4A91-9509-7B543EDB6F93}" + ProjectSection(ProjectDependencies) = postProject + {05B22ACE-2EA1-46AA-8483-A625B08A0D01} = {05B22ACE-2EA1-46AA-8483-A625B08A0D01} + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Selector.CLI", "Selector.CLI\Selector.CLI.csproj", "{AB0C1359-2A4D-4013-BC5A-79C55203C15E}" ProjectSection(ProjectDependencies) = postProject {05B22ACE-2EA1-46AA-8483-A625B08A0D01} = {05B22ACE-2EA1-46AA-8483-A625B08A0D01} EndProjectSection @@ -24,6 +29,10 @@ Global {2C33766F-FFEB-4A91-9509-7B543EDB6F93}.Debug|Any CPU.Build.0 = Debug|Any CPU {2C33766F-FFEB-4A91-9509-7B543EDB6F93}.Release|Any CPU.ActiveCfg = Release|Any CPU {2C33766F-FFEB-4A91-9509-7B543EDB6F93}.Release|Any CPU.Build.0 = Release|Any CPU + {AB0C1359-2A4D-4013-BC5A-79C55203C15E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB0C1359-2A4D-4013-BC5A-79C55203C15E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB0C1359-2A4D-4013-BC5A-79C55203C15E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB0C1359-2A4D-4013-BC5A-79C55203C15E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Selector/Selector.csproj b/Selector/Selector.csproj index da467f3..3b81dbd 100644 --- a/Selector/Selector.csproj +++ b/Selector/Selector.csproj @@ -7,7 +7,7 @@ - + diff --git a/Selector/Watcher/BaseWatcher.cs b/Selector/Watcher/BaseWatcher.cs index c07d963..76fbf57 100644 --- a/Selector/Watcher/BaseWatcher.cs +++ b/Selector/Watcher/BaseWatcher.cs @@ -16,7 +16,7 @@ namespace Selector while (true) { cancelToken.ThrowIfCancellationRequested(); await WatchOne(cancelToken); - await Task.Delay(PollPeriod); + await Task.Delay(PollPeriod, cancelToken); } } diff --git a/Selector/Watcher/Manager/IManager.cs b/Selector/Watcher/WatcherCollection/IWatcherCollection.cs similarity index 86% rename from Selector/Watcher/Manager/IManager.cs rename to Selector/Watcher/WatcherCollection/IWatcherCollection.cs index 7159732..c5a3899 100644 --- a/Selector/Watcher/Manager/IManager.cs +++ b/Selector/Watcher/WatcherCollection/IWatcherCollection.cs @@ -4,7 +4,7 @@ using System.Text; namespace Selector { - interface IManager + public interface IWatcherCollection { public bool IsRunning { get; } public void Add(IWatcher watcher); diff --git a/Selector/Watcher/Manager/Manager.cs b/Selector/Watcher/WatcherCollection/WatcherCollection.cs similarity index 81% rename from Selector/Watcher/Manager/Manager.cs rename to Selector/Watcher/WatcherCollection/WatcherCollection.cs index d57ff4c..9388a5a 100644 --- a/Selector/Watcher/Manager/Manager.cs +++ b/Selector/Watcher/WatcherCollection/WatcherCollection.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,10 +8,16 @@ using System.Threading.Tasks; namespace Selector { - public class Manager: IManager, IDisposable + public class WatcherCollection: IWatcherCollection, IDisposable, IEnumerable { public bool IsRunning { get; private set; } = true; private List Watchers { get; set; } = new(); + + public WatcherCollection() + { + + } + public int Count => Watchers.Count; public IEnumerable Tasks => Watchers @@ -58,6 +65,9 @@ namespace Selector { watcher.Dispose(); } - } + } + + public IEnumerator GetEnumerator() => Watchers.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } diff --git a/Selector/Watcher/Manager/WatcherContext.cs b/Selector/Watcher/WatcherCollection/WatcherContext.cs similarity index 100% rename from Selector/Watcher/Manager/WatcherContext.cs rename to Selector/Watcher/WatcherCollection/WatcherContext.cs