async Task Simple() { #region CancelCallbackTesting var tokenSource = new CancellationTokenSource(); var request = new Message(); var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message) => { // When request matches, cancel the token source if (message == request) { tokenSource.Cancel(); } return(false); }, response: 42); Assert.ThrowsAsync <OperationCanceledException>( @delegate: async() => { await session.Request <int>(request, tokenSource.Token) .ConfigureAwait(false); }); #endregion }
public async Task UpdateStock_CallbackTest() { var product = new Product() { Id = Guid.NewGuid(), Name = "test3", Price = 15, Stock = 164, AuditInformation = new AuditInformation() { CreatedBy = Guid.NewGuid(), CreatedDate = DateTime.UtcNow } }; await _testFixture.ProductRepository.SaveProduct(product); var request = new UpdateStockRequest(product.Id, product.AuditInformation.CreatedBy, -100); var simulatedResponse = CallbackResult <string> .SuccessResult("Stock updated"); var session = new TestableCallbackAwareSession(); session.When( matcher: (UpdateStockRequest message) => message == request, response: simulatedResponse); var result = await session.Request <CallbackResult <string> >(request) .ConfigureAwait(false); result.Payload.Should().Be(simulatedResponse.Payload); }
async Task ObjectCallbackTestingWithOptions() { #region ObjectCallbackTestingWithOptions var request = new Message(); var simulatedResponse = new ResponseMessage(); var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message, SendOptions options) => { return(message == request && options.GetHeaders() .ContainsKey("Simulated.Header")); }, response: simulatedResponse); var sendOptions = new SendOptions(); sendOptions.SetHeader("Simulated.Header", "value"); var result = await session.Request <ResponseMessage>(request, sendOptions) .ConfigureAwait(false); Assert.AreEqual(simulatedResponse, result); #endregion }
public async Task When_request_matcher_matches_should_return_response() { var callbackSession = new TestableCallbackAwareSession(); callbackSession.When <Request, string>(r => r.MagicNumber == 42, "HelloWorld"); var result = await callbackSession.Request <string>(new Request { MagicNumber = 42 }); Assert.AreEqual("HelloWorld", result); }
public async Task When_request_option_matcher_matches_should_return_response() { var callbackSession = new TestableCallbackAwareSession(); var expectedHeader = "TestingHeader"; callbackSession.When <Request, string>((r, o) => r.MagicNumber == 42 && o.GetHeaders().ContainsKey(expectedHeader), "HelloWorld"); var options = new SendOptions(); options.SetHeader(expectedHeader, "value"); var result = await callbackSession.Request <string>(new Request { MagicNumber = 42 }, options); Assert.AreEqual("HelloWorld", result); }
public void When_request_matcher_does_not_match_should_honor_cancellation() { var tokenSource = new CancellationTokenSource(); var callbackSession = new TestableCallbackAwareSession(); callbackSession.When <Request, string>(r => { tokenSource.Cancel(); return(r.MagicNumber == 43); }, "HelloWorld"); Assert.ThrowsAsync <TaskCanceledException>(async() => await callbackSession.Request <string>(new Request { MagicNumber = 42 }, tokenSource.Token)); }
public async Task When_request_matcher_matches_should_not_honor_cancellation() { var tokenSource = new CancellationTokenSource(); var callbackSession = new TestableCallbackAwareSession(); callbackSession.When <Request, string>(r => { tokenSource.Cancel(); return(r.MagicNumber == 42); }, "HelloWorld"); var result = await callbackSession.Request <string>(new Request { MagicNumber = 42 }, tokenSource.Token); Assert.AreEqual("HelloWorld", result); }
public async Task SaveProduct_CallbackTest() { var request = new SaveProductRequest("test2", 10, 12, Guid.NewGuid()); var simulatedResponse = CallbackResult <string> .SuccessResult("Product created"); var session = new TestableCallbackAwareSession(); session.When( matcher: (SaveProductRequest message) => message == request, response: simulatedResponse); var result = await session.Request <CallbackResult <string> >(request) .ConfigureAwait(false); result.Payload.Should().Be(simulatedResponse.Payload); }
public void When_request_option_matcher_does_not_match_should_honor_cancellation() { var tokenSource = new CancellationTokenSource(); var expectedHeader = "TestingHeader"; var callbackSession = new TestableCallbackAwareSession(); callbackSession.When <Request, string>((r, o) => { tokenSource.Cancel(); return(r.MagicNumber == 42 && o.GetHeaders().ContainsKey("WrongHeader")); }, "HelloWorld"); var options = new SendOptions(); options.SetHeader(expectedHeader, "value"); Assert.ThrowsAsync <TaskCanceledException>(async() => await callbackSession.Request <string>(new Request { MagicNumber = 42 }, options, tokenSource.Token)); }
public async Task When_request_option_matcher_matches_should_not_honor_cancellation() { var tokenSource = new CancellationTokenSource(); var expectedHeader = "TestingHeader"; var callbackSession = new TestableCallbackAwareSession(); callbackSession.When <Request, string>((r, o) => { tokenSource.Cancel(); return(r.MagicNumber == 42 && o.GetHeaders().ContainsKey(expectedHeader)); }, "HelloWorld"); var options = new SendOptions(); options.SetHeader(expectedHeader, "value"); var result = await callbackSession.Request <string>(new Request { MagicNumber = 42 }, options, tokenSource.Token); Assert.AreEqual("HelloWorld", result); }
async Task Simple() { #region IntCallbackTesting var request = new Message(); var simulatedResponse = 42; var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message) => { return(message == request); }, response: simulatedResponse); var result = await session.Request <int>(request) .ConfigureAwait(false); Assert.AreEqual(simulatedResponse, result); #endregion }
public void When_request_option_matcher_matches_with_wrong_response_type() { var callbackSession = new TestableCallbackAwareSession(); var expectedHeader = "TestingHeader"; callbackSession.When <Request, int>((r, o) => r.MagicNumber == 42 && o.GetHeaders().ContainsKey(expectedHeader), 42); var options = new SendOptions(); options.SetHeader(expectedHeader, "value"); var exception = Assert.ThrowsAsync <InvalidOperationException>(async() => await callbackSession.Request <string>(new Request { MagicNumber = 42 }, options)); StringAssert.Contains("Matcher matched but response type 'System.Int32' is incompatible with expected response type of 'System.String'.", exception.Message); }
public void When_request_matcher_matches_with_wrong_response_type() { var callbackSession = new TestableCallbackAwareSession(); callbackSession.When <Request, int>(r => r.MagicNumber == 42, 42); var exception = Assert.ThrowsAsync <InvalidOperationException>(async() => await callbackSession.Request <string>(new Request { MagicNumber = 42 })); StringAssert.Contains("Matcher matched but response type 'System.Int32' is incompatible with expected response type of 'System.String'.", exception.Message); }