added cli project, renamed manager to watcher collection

This commit is contained in:
andy 2021-10-08 23:48:07 +01:00
parent 7d5ab4477d
commit 507861bc28
11 changed files with 144 additions and 62 deletions

42
Selector.CLI/Program.cs Normal file
View File

@ -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<IPlayerWatcher, PlayerWatcher>()
.AddTransient<IWatcherCollection, WatcherCollection>()
.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<Program>();
//logger.LogInformation("Example log message");
//var logger = serviceProvider.GetService<ILoggerFactory>()
// .CreateLogger<Program>();
//logger.LogDebug("Starting application");
var logger = serviceProvider.GetService<ILogger>();
logger.LogDebug("All done!");
}
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<StartupObject>Selector.CLI.Program</StartupObject>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Selector\Selector.csproj" />
</ItemGroup>
</Project>

View File

@ -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<IWatcher>();
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<IWatcher>();
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();
}
}
}

View File

@ -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<IPlayerClient>();

View File

@ -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<IWatcher>();
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<IWatcher>();
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();
}
}
}

View File

@ -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

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SpotifyAPI.Web" Version="6.2.1" />
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
</ItemGroup>
</Project>

View File

@ -16,7 +16,7 @@ namespace Selector
while (true) {
cancelToken.ThrowIfCancellationRequested();
await WatchOne(cancelToken);
await Task.Delay(PollPeriod);
await Task.Delay(PollPeriod, cancelToken);
}
}

View File

@ -4,7 +4,7 @@ using System.Text;
namespace Selector
{
interface IManager
public interface IWatcherCollection
{
public bool IsRunning { get; }
public void Add(IWatcher watcher);

View File

@ -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<WatcherContext>
{
public bool IsRunning { get; private set; } = true;
private List<WatcherContext> Watchers { get; set; } = new();
public WatcherCollection()
{
}
public int Count => Watchers.Count;
public IEnumerable<Task> Tasks
=> Watchers
@ -58,6 +65,9 @@ namespace Selector
{
watcher.Dispose();
}
}
}
public IEnumerator<WatcherContext> GetEnumerator() => Watchers.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}