public async Task RunDeviceCommandTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); int?commandIdRan = 0; var arg1Ran = ""; var arg2Ran = ""; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { commandIdRan = commandId; arg1Ran = argument; arg2Ran = argument2; return(Task.FromResult(Result.ReportSuccess())); } }; var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e); return(Task.FromResult(0)); } }; var cts = new CancellationTokenSource(); var runner = new JavaScriptRunner(log, commandProcessor, dbConnection); const string script = @" function f1() { var result = runDeviceNameCommandName('Light Switch','Turn On', '99'); logInfo(result.Message); }; f1();"; var device = UnitTesting.CreateFakeDevice(); using (var context = new ZvsContext(dbConnection)) { var deviceCommand = new DeviceCommand { Name = "Turn On" }; device.Commands.Add(deviceCommand); context.Devices.Add(device); await context.SaveChangesAsync(CancellationToken.None); //Act var result = await runner.ExecuteScriptAsync(script, cts.Token); //Assert Assert.IsFalse(result.HasError, result.Message); Assert.IsTrue(deviceCommand.Id == commandIdRan, "Wrong command ran!"); Assert.IsTrue("99" == arg1Ran, "command arguments not passed in correctly."); Assert.IsTrue(string.Empty == arg2Ran, "command arguments not passed in correctly."); } }
public async Task RunBadDeviceCommandTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List <LogEntry>(); var commandProcessor = new StubICommandProcessor(); var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e); logEntries.Add(e); return(Task.FromResult(0)); } }; var cts = new CancellationTokenSource(); var runner = new JavaScriptRunner(log, commandProcessor, dbConnection); const string script = @" function f1() { var result = runDeviceNameCommandName('Light Switch','Turn On', '99'); logInfo(result.Message); }; f1();"; //Act var result = await runner.ExecuteScriptAsync(script, cts.Token); //Assert Assert.IsFalse(result.HasError, result.Message); Assert.IsTrue(logEntries.Any(o => o.Message.Contains("Cannot find"))); }
public async Task RunSceneAsyncSceneIsRunningTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List <LogEntry>(); var ranstoredCommands = new List <int>(); var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return(Task.FromResult(0)); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) { ranstoredCommands.Add(commandId.Value); } return(Task.FromResult(Result.ReportSuccess())); } }; var scene = new Scene { Name = "I Am Running Test", IsRunning = true }; using (var context = new ZvsContext(dbConnection)) { context.Scenes.Add(scene); await context.SaveChangesAsync(CancellationToken.None); } var cts = new CancellationTokenSource(); var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection); //Act var result = await sceneRunner.RunSceneAsync(scene.Id, cts.Token); Console.WriteLine(result.Message); //Assert Assert.IsTrue(result.HasError); Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Warn), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 0, "Scene runner did not run the correct amount of commands."); }
public async Task RunSceneAsyncInvalidSceneTest() { //Arrange var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "Scene-RunSceneAsyncInvalidSceneTest" }; Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return Task.FromResult(0); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); return Task.FromResult(Result.ReportSuccess()); } }; var cts = new CancellationTokenSource(); var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection); //Act var result = await sceneRunner.RunSceneAsync(-1, cts.Token); //Assert Assert.IsTrue(result.HasError); Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Warn), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 0, "Scene runner did not run the correct amount of commands."); }
public async Task RunSceneAsyncMultipleCommandCancelRunTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List <LogEntry>(); var ranstoredCommands = new List <int>(); var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e); logEntries.Add(e); return(Task.FromResult(0)); } }; var cts = new CancellationTokenSource(); var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) { ranstoredCommands.Add(commandId.Value); } if (ranstoredCommands.Count == 2) { cts.Cancel(); } return(Task.FromResult(Result.ReportSuccessFormat("Ran command Id:{0}", commandId))); } }; var scene = new Scene { Name = "I have one command" }; var cmd1 = new Command { Description = "Command 1" }; var command1 = new SceneStoredCommand { SortOrder = 1, TargetObjectName = "Device 1", Description = "Turn On Device 1", Command = cmd1 }; var cmd2 = new Command { Description = "Command 2" }; var command2 = new SceneStoredCommand { SortOrder = 2, Description = "TIME DELAY", Command = cmd2 }; var cmd3 = new Command { Description = "Command 3" }; var command3 = new SceneStoredCommand { SortOrder = 3, TargetObjectName = "Device 3", Description = "Turn On Device 3", Command = cmd3 }; scene.Commands.Add(command3); scene.Commands.Add(command1); scene.Commands.Add(command2); using (var context = new ZvsContext(dbConnection)) { context.Scenes.Add(scene); await context.SaveChangesAsync(CancellationToken.None); } var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection); //Act var result = await sceneRunner.RunSceneAsync(scene.Id, cts.Token); Console.WriteLine(result.Message); //Assert Assert.IsFalse(result.HasError); Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Warn) == 0, "Expected one warning log entries"); Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 0, "Expected zero error log entries"); Assert.IsTrue(ranstoredCommands.Count == 2, "Scene runner did not run the correct amount of commands."); }
public async Task LessThanInvalidValuesTest() { //Arrange var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "Trigger-LessThanInvalidValuesTest" }; Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return Task.FromResult(0); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); return Task.FromResult(Result.ReportSuccess()); } }; Database.SetInitializer(new CreateFreshDbInitializer()); var cts = new CancellationTokenSource(); var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection); await triggerManager.StartAsync(cts.Token); var cmd = new Command(); var dv = new DeviceValue { Value = "first value", ValueType = DataType.STRING, Triggers = new ObservableCollection<DeviceValueTrigger> { new DeviceValueTrigger { Name = "trigger1", IsEnabled = true, Operator = TriggerOperator.LessThan, Value = "a", Command = cmd } } }; var device = UnitTesting.CreateFakeDevice(); device.Values.Add(dv); using (var context = new ZvsContext(dbConnection)) { context.Devices.Add(device); var r = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r.HasError, r.Message); dv.Value = "3"; //Act var r2 = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r2.HasError, r2.Message); } await Task.Delay(700, cts.Token); await triggerManager.StopAsync(cts.Token); //Assert Assert.IsTrue(logEntries.Any(o => o.Level == LogEntryLevel.Warn), "Expected some warning log entries"); Assert.IsTrue(ranstoredCommands.Count == 0, "Trigger runner did not run the correct amount of commands."); }
public async Task QuickFireTriggerTest() { var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "Trigger-QuickFireTriggerTest" }; Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); //Arrange var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return Task.FromResult(0); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); return Task.FromResult(Result.ReportSuccess()); } }; var cts = new CancellationTokenSource(); var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection); await triggerManager.StartAsync(cts.Token); var cmd = new Command(); var dv = new DeviceValue { Value = "first value", ValueType = DataType.STRING, Triggers = new ObservableCollection<DeviceValueTrigger> { new DeviceValueTrigger { Name = "trigger1", IsEnabled = true, Operator = TriggerOperator.EqualTo, Value = "some unique value", Command = cmd } } }; var device = UnitTesting.CreateFakeDevice(); device.Values.Add(dv); //Act using (var context = new ZvsContext(dbConnection)) { context.Devices.Add(device); await context.TrySaveChangesAsync(cts.Token); dv.Value = "Not It!"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "not this one"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "some unique value"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "not it"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "some unique value"; await context.TrySaveChangesAsync(cts.Token); Console.WriteLine(context.DeviceValueTriggers.Count()); } await Task.Delay(700, cts.Token); await triggerManager.StopAsync(cts.Token); //Assert Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 2, "Trigger runner did not run the correct amount of commands."); Assert.IsTrue(ranstoredCommands.All(o => o == cmd.Id), "Scheduled task runner did not run the correct command."); }
public async Task RunCommandTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); int? commandIdRan = 0; var arg1Ran = ""; var arg2Ran = ""; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { commandIdRan = commandId; arg1Ran = argument; arg2Ran = argument2; return Task.FromResult(Result.ReportSuccess()); } }; var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e); return Task.FromResult(0); } }; var device0 = UnitTesting.CreateFakeDevice(); var device = UnitTesting.CreateFakeDevice(); using (var context = new ZvsContext(dbConnection)) { var deviceCommand = new DeviceCommand { Name = "Turn On" }; device.Commands.Add(deviceCommand); context.Devices.Add(device0); context.Devices.Add(device); await context.SaveChangesAsync(CancellationToken.None); var cts = new CancellationTokenSource(); var runner = new JavaScriptRunner(log, commandProcessor, dbConnection); var script = $@" function f1() {{ var result = runCommand({deviceCommand.Id },'98', '0'); logInfo(result.Message); }}; f1();"; //Act var result = await runner.ExecuteScriptAsync(script, cts.Token); //Assert Assert.IsFalse(result.HasError, result.Message); Assert.IsTrue(deviceCommand.Id == commandIdRan, "Wrong command ran!"); Assert.IsTrue("98" == arg1Ran, "command argument1 not passed in correctly."); Assert.IsTrue("0" == arg2Ran, "command argument2 not passed in correctly."); } }
public async Task RunBadDeviceCommandTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var commandProcessor = new StubICommandProcessor(); var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e); logEntries.Add(e); return Task.FromResult(0); } }; var cts = new CancellationTokenSource(); var runner = new JavaScriptRunner(log, commandProcessor, dbConnection); const string script = @" function f1() { var result = runDeviceNameCommandName('Light Switch','Turn On', '99'); logInfo(result.Message); }; f1();"; //Act var result = await runner.ExecuteScriptAsync(script, cts.Token); //Assert Assert.IsFalse(result.HasError, result.Message); Assert.IsTrue(logEntries.Any(o => o.Message.Contains("Cannot find"))); }
public async Task RunSceneAsyncMultipleCommandCancelRunTest() { //Arrange var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "Scene-RunSceneAsyncMultipleCommandCancelRunTest" }; Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e); logEntries.Add(e); return Task.FromResult(0); } }; var cts = new CancellationTokenSource(); var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); if (ranstoredCommands.Count == 2) { cts.Cancel(); } return Task.FromResult(Result.ReportSuccessFormat("Ran command Id:{0}", commandId)); } }; var scene = new Scene { Name = "I have one command" }; var cmd1 = new Command { Description = "Command 1" }; var command1 = new SceneStoredCommand { SortOrder = 1, TargetObjectName = "Device 1", Description = "Turn On Device 1", Command = cmd1 }; var cmd2 = new Command { Description = "Command 2" }; var command2 = new SceneStoredCommand { SortOrder = 2, Description = "TIME DELAY", Command = cmd2 }; var cmd3 = new Command { Description = "Command 3" }; var command3 = new SceneStoredCommand { SortOrder = 3, TargetObjectName = "Device 3", Description = "Turn On Device 3", Command = cmd3 }; scene.Commands.Add(command3); scene.Commands.Add(command1); scene.Commands.Add(command2); using (var context = new ZvsContext(dbConnection)) { context.Scenes.Add(scene); await context.SaveChangesAsync(CancellationToken.None); } var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection); //Act var result = await sceneRunner.RunSceneAsync(scene.Id, cts.Token); Console.WriteLine(result.Message); //Assert Assert.IsFalse(result.HasError); Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Warn) == 0, "Expected one warning log entries"); Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 0, "Expected zero error log entries"); Assert.IsTrue(ranstoredCommands.Count == 2, "Scene runner did not run the correct amount of commands."); }
public async Task ContraLessThanTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List <LogEntry>(); var ranstoredCommands = new List <int>(); var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return(Task.FromResult(0)); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) { ranstoredCommands.Add(commandId.Value); } return(Task.FromResult(Result.ReportSuccess())); } }; Database.SetInitializer(new CreateFreshDbInitializer()); var cts = new CancellationTokenSource(); var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection); await triggerManager.StartAsync(cts.Token); var cmd = new Command(); var dv = new DeviceValue { Value = "first value", ValueType = DataType.STRING, Triggers = new ObservableCollection <DeviceValueTrigger> { new DeviceValueTrigger { Name = "trigger1", IsEnabled = true, Operator = TriggerOperator.LessThan, Value = "1", Command = cmd } } }; var device = UnitTesting.CreateFakeDevice(); device.Values.Add(dv); using (var context = new ZvsContext(dbConnection)) { context.Devices.Add(device); var r = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r.HasError, r.Message); dv.Value = "3"; //Act var r2 = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r2.HasError, r2.Message); } await Task.Delay(700, cts.Token); await triggerManager.StopAsync(cts.Token); //Assert Assert.IsTrue(logEntries.All(o => o.Level != LogEntryLevel.Error), "Expected no error log entries"); Assert.IsTrue(ranstoredCommands.Count == 0, "Trigger runner did not run the correct amount of commands."); }
public async Task QuickFireTriggerTest() { var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List <LogEntry>(); var ranstoredCommands = new List <int>(); //Arrange var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return(Task.FromResult(0)); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) { ranstoredCommands.Add(commandId.Value); } return(Task.FromResult(Result.ReportSuccess())); } }; var cts = new CancellationTokenSource(); var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection); await triggerManager.StartAsync(cts.Token); var cmd = new Command(); var dv = new DeviceValue { Value = "first value", ValueType = DataType.STRING, Triggers = new ObservableCollection <DeviceValueTrigger> { new DeviceValueTrigger { Name = "trigger1", IsEnabled = true, Operator = TriggerOperator.EqualTo, Value = "some unique value", Command = cmd } } }; var device = UnitTesting.CreateFakeDevice(); device.Values.Add(dv); //Act using (var context = new ZvsContext(dbConnection)) { context.Devices.Add(device); await context.TrySaveChangesAsync(cts.Token); dv.Value = "Not It!"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "not this one"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "some unique value"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "not it"; await context.TrySaveChangesAsync(cts.Token); dv.Value = "some unique value"; await context.TrySaveChangesAsync(cts.Token); Console.WriteLine(context.DeviceValueTriggers.Count()); } await Task.Delay(700, cts.Token); await triggerManager.StopAsync(cts.Token); //Assert Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 2, "Trigger runner did not run the correct amount of commands."); Assert.IsTrue(ranstoredCommands.All(o => o == cmd.Id), "Scheduled task runner did not run the correct command."); }
public async Task RunSceneAsyncOneCommandTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return Task.FromResult(0); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); return Task.FromResult(Result.ReportSuccessFormat("Ran command Id:{0}", commandId)); } }; var scene = new Scene { Name = "I have one command" }; var command1 = new SceneStoredCommand { SortOrder = 1, TargetObjectName = "Device 1", Description = "Turn On", Command = new Command { Description = "Command 1" } }; scene.Commands.Add(command1); using (var context = new ZvsContext(dbConnection)) { context.Scenes.Add(scene); await context.SaveChangesAsync(CancellationToken.None); } var cts = new CancellationTokenSource(); var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection); //Act var result = await sceneRunner.RunSceneAsync(scene.Id, cts.Token); Console.WriteLine(result.Message); //Assert Assert.IsFalse(result.HasError); Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 1, "Scene runner did not run the correct amount of commands."); Assert.IsTrue(ranstoredCommands.All(o => o == command1.Id), "Scene runner did not run the correct command."); }
public async Task RunSceneAsyncSceneNoCommandsTest() { //Arrange var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return Task.FromResult(0); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); return Task.FromResult(Result.ReportSuccess()); } }; var scene = new Scene { Name = "I have no commands" }; using (var context = new ZvsContext(dbConnection)) { context.Scenes.Add(scene); await context.SaveChangesAsync(CancellationToken.None); } var cts = new CancellationTokenSource(); var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection); //Act var result = await sceneRunner.RunSceneAsync(scene.Id, cts.Token); //Assert Assert.IsTrue(result.HasError); Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Warn), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 0, "Scene runner did not run the correct amount of commands."); }
public async Task MultipleAsyncRequestsTest() { var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "MultipleAsyncRequestsTest" }; Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List<LogEntry>(); var ranstoredCommands = new List<int>(); //Arrange var log = new StubIFeedback<LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return Task.FromResult(0); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) ranstoredCommands.Add(commandId.Value); return Task.FromResult(Result.ReportSuccess()); } }; var currentTime = new StubITimeProvider { TimeGet = () => DateTime.Parse("5/21/14 15:02:20") }; var cts = new CancellationTokenSource(); var taskRunner = new ScheduledTaskRunner(log, commandProcessor, dbConnection, currentTime); await taskRunner.StartAsync(cts.Token); var command = new Command(); var commandScheduledTask = new DataModel.ScheduledTask { IsEnabled = true, Name = "New Command added after start", Command = command }; using (var context = new ZvsContext(dbConnection)) { context.ScheduledTasks.Add(commandScheduledTask); var r2 = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r2.HasError, r2.Message); commandScheduledTask.StartTime = DateTime.Parse("5/20/14 15:02:20"); var r = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r.HasError, r.Message); commandScheduledTask.StartTime = DateTime.Parse("5/20/14 15:02:21"); var r3 = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r3.HasError, r.Message); } //Act await Task.Delay(2000, cts.Token); await taskRunner.StopAsync(cts.Token); //Assert Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 0, "Scheduled task runner did not run the correct amount of commands."); }
public async Task NewTaskExecutesTest() { var dbConnection = new UnitTestDbConnection(); Database.SetInitializer(new CreateFreshDbInitializer()); var logEntries = new List <LogEntry>(); var ranstoredCommands = new List <int>(); //Arrange var log = new StubIFeedback <LogEntry> { ReportAsyncT0CancellationToken = (e, c) => { Console.WriteLine(e.ToString()); logEntries.Add(e); return(Task.FromResult(0)); } }; var commandProcessor = new StubICommandProcessor { RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) => { if (commandId.HasValue) { ranstoredCommands.Add(commandId.Value); } return(Task.FromResult(Result.ReportSuccess())); } }; var currentTime = new StubITimeProvider { TimeGet = () => DateTime.Parse("5/21/14 15:02:20") }; var cts = new CancellationTokenSource(); var taskRunner = new ScheduledTaskRunner(log, commandProcessor, dbConnection, currentTime); var command = new Command(); var commandScheduledTask = new DataModel.ScheduledTask { IsEnabled = true, Name = "New Command added after start", Command = command }; using (var context = new ZvsContext(dbConnection)) { context.ScheduledTasks.Add(commandScheduledTask); var r2 = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r2.HasError, r2.Message); await taskRunner.StartAsync(cts.Token); await Task.Delay(500, cts.Token); //Act commandScheduledTask.StartTime = DateTime.Parse("5/20/14 15:02:20"); var r = await context.TrySaveChangesAsync(cts.Token); Assert.IsFalse(r.HasError, r.Message); await Task.Delay(1000, cts.Token); await taskRunner.StopAsync(cts.Token); } //Assert Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries"); Assert.IsTrue(ranstoredCommands.Count == 1, "Scheduled task runner did not run the correct amount of commands."); }