public async Task ProducerQueuesMessages() { int expectedCount = 2; int count = 0; var ctx = new UnitTestContext(); //todo parametrizar tempo do timer var hostedService = new GreetingProducerWorker(ctx.GreetingManager.Object, ctx.GetLoggerMock <GreetingProducerWorker>().Object); //Cancel after X calls ctx.GreetingManager .Setup(x => x.SendAsync(It.IsAny <Greeting>())) .Callback(async() => { count++; if (count == expectedCount) { await hostedService.StopAsync(CancellationToken.None); } }); //act await hostedService.StartAsync(CancellationToken.None); await Task.Delay(6000); //Time to execute 2x ctx.GreetingManager.Verify(x => x.SendAsync(It.IsAny <Greeting>()), Times.Exactly(expectedCount)); }
public async Task ReceiveCallsDisplay() { var ctx = new UnitTestContext(); var greeting = Greeting.Factory.CreateDefault(); var greetingManager = new Mock <GreetingManager>(ctx.MicroserviceIdProvider.Object, ctx.GreetingQueue.Object, ctx.GetLoggerMock <GreetingManager>().Object); greetingManager.CallBase = true; await greetingManager.Object.ReceiveAsync(greeting); greetingManager.Verify(x => x.DisplayGreetingsTraffic(Greeting.Direction.Incoming, greeting), Times.Once()); }
public async Task ConsumerProcessMessages() { int expectedCount = 5; int count = 0; var greeting = Greeting.Factory.CreateDefault(); var ctx = new UnitTestContext(); var hostedService = new GreetingConsumerWorker(ctx.GreetingManager.Object, ctx.GreetingQueue.Object, ctx.GetLoggerMock <GreetingConsumerWorker>().Object); //Cancel after X calls ctx.GreetingQueue .Setup(x => x.ReceiveAsync()) .ReturnsAsync(greeting) .Callback(async() => { count++; if (count == expectedCount) { await hostedService.StopAsync(CancellationToken.None); } }); //act await hostedService.StartAsync(CancellationToken.None); await Task.Delay(1000); //Time to execute ctx.GreetingQueue.Verify(x => x.ReceiveAsync(), Times.Exactly(expectedCount)); ctx.GreetingManager.Verify(x => x.ReceiveAsync(greeting), Times.Exactly(expectedCount)); }