public async Task CheckStaleIntervalTests() { var activityId = Guid.NewGuid().ToString(); var workerInfo = new MockWorkerInfo(); var settings = new ScaleSettings { StaleWorkerCheckInterval = TimeSpan.FromMilliseconds(500) }; using (var scaleManager = new MockScaleManager(MockBehavior.Strict, settings)) { // Setup scaleManager.MockWorkerTable.Setup(t => t.List()) .Returns(Task.FromResult(Enumerable.Empty <IWorkerInfo>())); scaleManager.MockScaleTracer.Setup(t => t.TraceInformation(activityId, workerInfo, It.IsAny <string>())); // Test for (int i = 0; i < 10; ++i) { await scaleManager.MockCheckStaleWorker(activityId, workerInfo); await Task.Delay(100); } // Assert scaleManager.MockWorkerTable.Verify(t => t.List(), Times.AtLeast(1)); scaleManager.MockWorkerTable.Verify(t => t.List(), Times.AtMost(4)); } }
public void UpdateWorkerStatusTimerTests() { var worker = new MockWorkerInfo(); var settings = new ScaleSettings { WorkerUpdateInterval = TimeSpan.FromMilliseconds(500) }; var evt = new AutoResetEvent(false); var mockScaleManager = new Mock <MockScaleManager>(Mock.Of <IWorkerInfoProvider>(), Mock.Of <IWorkerTable>(), Mock.Of <IScaleHandler>(), Mock.Of <IScaleTracer>(), settings) { CallBase = true }; // Setup mockScaleManager.Setup(m => m.MockProcessWorkItem(It.IsAny <string>())) .Callback((string id) => evt.Set()) .Returns(Task.CompletedTask); using (var manager = mockScaleManager.Object) { // Assert Assert.False(evt.WaitOne(1000)); // Test manager.Start(); // Assert Assert.True(evt.WaitOne(1000)); } }
public async Task PingWorkerIntervalTests() { var activityId = Guid.NewGuid().ToString(); var workerInfo = new MockWorkerInfo(); var settings = new ScaleSettings { WorkerPingInterval = TimeSpan.FromMilliseconds(500) }; using (var scaleManager = new MockScaleManager(MockBehavior.Strict, settings)) { // Setup scaleManager.MockScaleHandler.Setup(h => h.PingWorker(activityId, workerInfo)) .Returns(Task.FromResult(true)); scaleManager.MockWorkerTable.Setup(t => t.AddOrUpdate(workerInfo)) .Returns(Task.CompletedTask); scaleManager.MockScaleTracer.Setup(t => t.TraceUpdateWorker(activityId, workerInfo, It.Is <string>(s => s.Contains("updated")))); var loop = 10; for (int i = 0; i < loop; ++i) { // test await scaleManager.MockPingWorker(activityId, workerInfo); await Task.Delay(100); } // assert scaleManager.VerifyAll(); scaleManager.MockScaleHandler.Verify(h => h.PingWorker(activityId, workerInfo), Times.Between(2, loop - 1, Range.Inclusive)); scaleManager.MockWorkerTable.Verify(t => t.AddOrUpdate(workerInfo), Times.Exactly(loop)); scaleManager.MockScaleTracer.Verify(t => t.TraceUpdateWorker(activityId, workerInfo, It.Is <string>(s => s.Contains("updated"))), Times.Exactly(loop)); } }
public async Task ValidWorkerTests() { var activityId = Guid.NewGuid().ToString(); var workerInfo = new MockWorkerInfo(); // Test using (var scaleManager = new MockScaleManager(MockBehavior.Strict)) { // Setup scaleManager.MockScaleHandler.Setup(h => h.PingWorker(activityId, workerInfo)) .Returns(Task.FromResult(true)); scaleManager.MockWorkerTable.Setup(t => t.AddOrUpdate(workerInfo)) .Returns(Task.CompletedTask); scaleManager.MockScaleTracer.Setup(t => t.TraceUpdateWorker(activityId, workerInfo, It.Is <string>(s => s.Contains("updated")))); // test await scaleManager.MockPingWorker(activityId, workerInfo); // assert scaleManager.VerifyAll(); } }
public async Task InvalidWorkerTests() { var activityId = Guid.NewGuid().ToString(); var workerInfo = new MockWorkerInfo(); // Test using (var scaleManager = new MockScaleManager(MockBehavior.Strict)) { // Setup scaleManager.MockScaleHandler.Setup(h => h.PingWorker(activityId, workerInfo)) .Returns(Task.FromResult(false)); scaleManager.MockWorkerTable.Setup(t => t.Delete(workerInfo)) .Returns(Task.CompletedTask); scaleManager.MockScaleTracer.Setup(t => t.TraceWarning(activityId, workerInfo, It.Is <string>(s => s.Contains("not belong")))); scaleManager.MockScaleTracer.Setup(t => t.TraceRemoveWorker(activityId, workerInfo, It.Is <string>(s => s.Contains("removed")))); // test var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() => await scaleManager.MockPingWorker(activityId, workerInfo)); // assert Assert.Contains("not belong", exception.Message); scaleManager.VerifyAll(); } }