public async Task CanCancelService() { _mockMessageProvider.Setup(s => s.ReceiveAsync( It.IsAny <int>(), It.IsAny <TimeSpan>())).ReturnsAsync(new List <Message>()); var mockServiceBusProvider = new Mock <IServiceBusManager>(); mockServiceBusProvider.Setup(s => s.CreateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); mockServiceBusProvider.Setup(s => s.CreateMessageReceiver( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).Returns(_mockMessageProvider.Object); var service = new EventReaderService.EventReaderService( _context, _stateManager, _mockedBigBrother, mockServiceBusProvider.Object, new MockActorProxyFactory(), _config); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2)); await service.InvokeRunAsync(cancellationTokenSource.Token); //Assert can cancel the service from running Assert.True(cancellationTokenSource.IsCancellationRequested); }
public async Task CanAddMoreHandlersDynamically(string eventName, int messageCount, int expectedHandlerId) { //create mocked handlers based on the amount of messages passed in to the test var mockActorProxyFactory = new MockActorProxyFactory(); for (var i = 0; i <= messageCount; i++) { mockActorProxyFactory.RegisterActor(CreateMockEventHandlerActor(new ActorId($"{eventName}-{i + 1}"))); } //return messages up to the limit of the test requirements var count = 0; _mockMessageProvider.Setup(s => s.ReceiveAsync( It.IsAny <int>(), It.IsAny <TimeSpan>())).ReturnsAsync(() => { if (count >= messageCount) { return(new List <Message>()); } count++; return(CreateMessage(eventName)); }); var mockServiceBusProvider = new Mock <IServiceBusManager>(); mockServiceBusProvider.Setup(s => s.CreateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); mockServiceBusProvider.Setup(s => s.CreateMessageReceiver( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).Returns(_mockMessageProvider.Object); mockServiceBusProvider.Setup(s => s.GetLockToken(It.IsAny <Message>())).Returns(Guid.NewGuid().ToString); var service = new EventReaderService.EventReaderService( _context, _stateManager, _mockedBigBrother, mockServiceBusProvider.Object, mockActorProxyFactory, _config); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)); await service.InvokeRunAsync(cancellationTokenSource.Token); Assert.Equal(expectedHandlerId, service.HandlerCount); }
public async Task CanGetMessage(string eventName, string handlerName, int expectedHandleCount, int messageCount) { _mockActorProxyFactory.RegisterActor(CreateMockEventHandlerActor(new ActorId(handlerName))); var count = 0; _mockMessageProvider.Setup(s => s.ReceiveAsync( It.IsAny <int>(), It.IsAny <TimeSpan>())).ReturnsAsync(() => { if (count >= messageCount) { return(new List <Message>()); } count++; return(CreateMessage(eventName)); }); var mockServiceBusProvider = new Mock <IServiceBusManager>(); mockServiceBusProvider.Setup(s => s.CreateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); mockServiceBusProvider.Setup(s => s.CreateMessageReceiver( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).Returns(_mockMessageProvider.Object); mockServiceBusProvider.Setup(s => s.GetLockToken(It.IsAny <Message>())).Returns(Guid.NewGuid().ToString); var service = new EventReaderService.EventReaderService( _context, _stateManager, _mockedBigBrother, mockServiceBusProvider.Object, _mockActorProxyFactory, _config); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1)); await service.InvokeRunAsync(cancellationTokenSource.Token); //Assert that the dictionary contains 1 processing message and associated handle Assert.Equal(expectedHandleCount, service._inflightMessages.Count); }
public async Task CanDeleteMessageFromSubscription( string eventName, string handlerName, int messageCount, int expectedHandlerId, bool messageDelivered, int expectedStatMessageCount) { _mockActorProxyFactory.RegisterActor(CreateMockEventHandlerActor(new ActorId(handlerName))); var count = 0; _mockMessageProvider.Setup(s => s.ReceiveAsync( It.IsAny <int>(), It.IsAny <TimeSpan>())).ReturnsAsync(() => { if (count >= messageCount) { return(new List <Message>()); } count++; return(CreateMessage(eventName)); }); var mockServiceBusManager = new Mock <IServiceBusManager>(); mockServiceBusManager.Setup(s => s.CreateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); mockServiceBusManager.Setup(s => s.CreateMessageReceiver( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).Returns(_mockMessageProvider.Object); mockServiceBusManager.Setup(s => s.GetLockToken(It.IsAny <Message>())).Returns(Guid.NewGuid().ToString); var service = new EventReaderService.EventReaderService( _context, _stateManager, _mockedBigBrother, mockServiceBusManager.Object, _mockActorProxyFactory, _config); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1)); await service.InvokeRunAsync(cancellationTokenSource.Token); var dictionary = await _stateManager.GetOrAddAsync <IReliableDictionary2 <int, MessageDataHandle> >(nameof(MessageDataHandle)); MessageData messageData; using (var tx = _stateManager.CreateTransaction()) { var messageDataHandle = await dictionary.TryGetValueAsync(tx, expectedHandlerId); //reconstruct the message so we can call complete messageData = new MessageData("Hello World 1", eventName, "subA", "service"); } await service.CompleteMessageAsync(messageData, messageDelivered, CancellationToken.None); //Assert that the dictionary contains 1 processing message and associated handle dictionary = await _stateManager.GetOrAddAsync <IReliableDictionary2 <int, MessageDataHandle> >(nameof(MessageDataHandle)); Assert.Equal(expectedStatMessageCount, dictionary.Count); }