|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Threading; |
| 5 | +using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Concurrency; |
| 6 | +using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Diagnostics; |
| 7 | +using Microsoft.Azure.IoTSolutions.DeviceSimulation.SimulationAgent; |
| 8 | +using Microsoft.Azure.IoTSolutions.DeviceSimulation.SimulationAgent.DeviceReplay; |
| 9 | +using Microsoft.Azure.IoTSolutions.DeviceSimulation.SimulationAgent.SimulationThreads; |
| 10 | +using Moq; |
| 11 | +using Xunit; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace SimulationAgent.Test.SimulationThreads |
| 15 | +{ |
| 16 | + public class DeviceReplayTaskTest |
| 17 | + { |
| 18 | + private const int NUM_ACTORS = 9; |
| 19 | + private const int MAX_PENDING_TASKS = 5; |
| 20 | + |
| 21 | + private readonly Mock<IAppConcurrencyConfig> mockAppConcurrencyConfig; |
| 22 | + private readonly Mock<ILogger> mockLogger; |
| 23 | + private readonly DeviceReplayTask target; |
| 24 | + private readonly ConcurrentDictionary<string, Mock<IDeviceReplayActor>> mockDeviceReplayActors; |
| 25 | + private readonly ConcurrentDictionary<string, IDeviceReplayActor> mockDeviceReplayActorObjects; |
| 26 | + private readonly ConcurrentDictionary<string, Mock<ISimulationManager>> mockSimulationManagers; |
| 27 | + private readonly ConcurrentDictionary<string, ISimulationManager> mockSimulationManagerObjects; |
| 28 | + |
| 29 | + public DeviceReplayTaskTest() |
| 30 | + { |
| 31 | + this.mockDeviceReplayActors = new ConcurrentDictionary<string, Mock<IDeviceReplayActor>>(); |
| 32 | + this.mockDeviceReplayActorObjects = new ConcurrentDictionary<string, IDeviceReplayActor>(); |
| 33 | + this.mockSimulationManagers = new ConcurrentDictionary<string, Mock<ISimulationManager>>(); |
| 34 | + this.mockSimulationManagerObjects = new ConcurrentDictionary<string, ISimulationManager>(); |
| 35 | + |
| 36 | + this.mockAppConcurrencyConfig = new Mock<IAppConcurrencyConfig>(); |
| 37 | + this.mockAppConcurrencyConfig.SetupGet(x => x.MaxPendingTasks).Returns(MAX_PENDING_TASKS); |
| 38 | + this.mockLogger = new Mock<ILogger>(); |
| 39 | + |
| 40 | + this.target = new DeviceReplayTask(this.mockAppConcurrencyConfig.Object, this.mockLogger.Object); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void ItCallsRunAsyncOnAllReplayActors() |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + var cancellationToken = new CancellationTokenSource(); |
| 48 | + |
| 49 | + this.BuildMockDeviceReplayActors( |
| 50 | + this.mockDeviceReplayActors, |
| 51 | + this.mockDeviceReplayActorObjects, |
| 52 | + cancellationToken, |
| 53 | + NUM_ACTORS); |
| 54 | + |
| 55 | + // Build a list of SimulationManagers |
| 56 | + this.BuildMockSimluationManagers( |
| 57 | + this.mockSimulationManagers, |
| 58 | + this.mockSimulationManagerObjects, |
| 59 | + cancellationToken, |
| 60 | + NUM_ACTORS); |
| 61 | + |
| 62 | + // Act |
| 63 | + // Act on the target. The cancellation token will be cancelled through |
| 64 | + // a callback that will be triggered when each device-replay actor |
| 65 | + // is called. |
| 66 | + var targetTask = this.target.RunAsync( |
| 67 | + this.mockSimulationManagerObjects, |
| 68 | + this.mockDeviceReplayActorObjects, |
| 69 | + cancellationToken.Token); |
| 70 | + |
| 71 | + // Assert |
| 72 | + // Verify that each SimulationManager was called at least once |
| 73 | + foreach (var actor in this.mockDeviceReplayActors) |
| 74 | + actor.Value.Verify(x => x.HasWorkToDo(), Times.Once); |
| 75 | + } |
| 76 | + |
| 77 | + private void BuildMockDeviceReplayActors( |
| 78 | + ConcurrentDictionary<string, Mock<IDeviceReplayActor>> mockDictionary, |
| 79 | + ConcurrentDictionary<string, IDeviceReplayActor> objectDictionary, |
| 80 | + CancellationTokenSource cancellationToken, |
| 81 | + int count) |
| 82 | + { |
| 83 | + mockDictionary.Clear(); |
| 84 | + objectDictionary.Clear(); |
| 85 | + |
| 86 | + for (int i = 0; i < count; i++) |
| 87 | + { |
| 88 | + var deviceName = $"device_{i}"; |
| 89 | + var mockDeviceReplayActor = new Mock<IDeviceReplayActor>(); |
| 90 | + |
| 91 | + // Have each DeviceReplayActor report that it has work to do |
| 92 | + mockDeviceReplayActor.Setup(x => x.HasWorkToDo()).Returns(true); |
| 93 | + mockDeviceReplayActor.Setup(x => x.RunAsync()).Returns(Task.CompletedTask) |
| 94 | + .Callback(() => { cancellationToken.Cancel(); }); |
| 95 | + |
| 96 | + mockDictionary.TryAdd(deviceName, mockDeviceReplayActor); |
| 97 | + objectDictionary.TryAdd(deviceName, mockDeviceReplayActor.Object); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /* |
| 102 | + * Creating two collections: one for the mocks, and another to store the |
| 103 | + * mock objects. If we only created one collection and populated it with |
| 104 | + * the mock objects, we wouldn't have a reference to the backing mock for |
| 105 | + * each. |
| 106 | + */ |
| 107 | + private void BuildMockSimluationManagers( |
| 108 | + ConcurrentDictionary<string, Mock<ISimulationManager>> mockSimulationManagers, |
| 109 | + ConcurrentDictionary<string, ISimulationManager> mockSimulationManagerObjects, |
| 110 | + CancellationTokenSource cancellationToken, |
| 111 | + int count) |
| 112 | + { |
| 113 | + mockSimulationManagers.Clear(); |
| 114 | + mockSimulationManagerObjects.Clear(); |
| 115 | + |
| 116 | + for (int i = 0; i < count; i++) |
| 117 | + { |
| 118 | + var deviceName = $"simulation_{i}"; |
| 119 | + var mockSimulationManager = new Mock<ISimulationManager>(); |
| 120 | + |
| 121 | + // We only want the main loop in the target to run once, so here we'll |
| 122 | + // trigger a callback which will cancel the cancellation token that |
| 123 | + // the main loop uses. |
| 124 | + mockSimulationManager.Setup(x => x.NewConnectionLoop()) |
| 125 | + .Callback(() => cancellationToken.Cancel()); |
| 126 | + |
| 127 | + mockSimulationManagers.TryAdd(deviceName, mockSimulationManager); |
| 128 | + mockSimulationManagerObjects.TryAdd(deviceName, mockSimulationManager.Object); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments