public void SingleConsumer() { var one = new PoolFiber(); one.Start(); var oneConsumed = 0; var reset = new AutoResetEvent(false); using (one) { var channel = new QueueChannel<int>(); Action<int> onMessage = delegate { oneConsumed++; if (oneConsumed == 20) { reset.Set(); } }; channel.Subscribe(one, onMessage); for (var i = 0; i < 20; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(10000, false)); } }
public void Multiple() { var queues = new List<IFiber>(); var receiveCount = 0; var reset = new AutoResetEvent(false); var channel = new QueueChannel<int>(); var messageCount = 100; var updateLock = new object(); for (var i = 0; i < 5; i++) { Action<int> onReceive = delegate { Thread.Sleep(15); lock (updateLock) { receiveCount++; if (receiveCount == messageCount) { reset.Set(); } } }; var fiber = new PoolFiber(); fiber.Start(); queues.Add(fiber); channel.Subscribe(fiber, onReceive); } for (var i = 0; i < messageCount; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(10000, false)); queues.ForEach(delegate(IFiber q) { q.Dispose(); }); }
public void testMultipleChannelsWithNoSelector() { QueueChannel channel1 = new QueueChannel(); QueueChannel channel2 = new QueueChannel(); channel1.Send(new StringMessage("test1")); channel1.Send(new StringMessage("test2")); channel2.Send(new StringMessage("test1")); channel2.Send(new StringMessage("test2")); ChannelPurger purger = new ChannelPurger(channel1, channel2); IList <IMessage> purgedMessages = purger.Purge(); Assert.That(purgedMessages.Count, Is.EqualTo(4)); Assert.IsNull(channel1.Receive(TimeSpan.Zero)); Assert.IsNull(channel2.Receive(TimeSpan.Zero)); }
public void TestCorrelationNotCopiedFromRequestMessgeIdIfAlreadySetByHandler() { IMessage message = new StringMessage("test"); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "CreateMessage"); serviceActivator.OutputChannel = outputChannel; EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.Start(); Assert.IsTrue(inputChannel.Send(message)); IMessage reply = outputChannel.Receive(TimeSpan.Zero); Assert.That(reply.Headers.CorrelationId, Is.EqualTo("456-XYZ")); }
public void TestInt2809JavaTypePropertiesToRabbit() { var config = new ConfigurationBuilder().Build(); var services = new ServiceCollection().BuildServiceProvider(); var context = new GenericApplicationContext(services, config); var channel = new Mock <RC.IModel>(); channel.Setup(c => c.IsOpen).Returns(true); var connection = new Mock <Messaging.RabbitMQ.Connection.IConnection>(); connection.Setup(c => c.IsOpen).Returns(true); connection.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel.Object); var connectionFactory = new Mock <Messaging.RabbitMQ.Connection.IConnectionFactory>(); connectionFactory.Setup(f => f.CreateConnection()).Returns(connection.Object); var container = new DirectMessageListenerContainer(); container.ConnectionFactory = connectionFactory.Object; container.AcknowledgeMode = Messaging.RabbitMQ.Core.AcknowledgeMode.MANUAL; var adapter = new RabbitInboundChannelAdapter(context, container); adapter.MessageConverter = new JsonMessageConverter(); var qchannel = new QueueChannel(context); adapter.OutputChannel = qchannel; adapter.BindSourceMessage = true; object payload = new Foo("bar1"); var objectToJsonTransformer = new ObjectToJsonTransformer(context, typeof(byte[])); var jsonMessage = objectToJsonTransformer.Transform(Message.Create(payload)); var accessor = RabbitHeaderAccessor.GetMutableAccessor(jsonMessage); accessor.DeliveryTag = 123ul; var listener = container.MessageListener as IChannelAwareMessageListener; var rabbitChannel = new Mock <RC.IModel>(); listener.OnMessage(jsonMessage, rabbitChannel.Object); var result = qchannel.Receive(1000); Assert.Equal(payload, result.Payload); Assert.Same(rabbitChannel.Object, result.Headers.Get <RC.IModel>(RabbitMessageHeaders.CHANNEL)); Assert.Equal(123ul, result.Headers.DeliveryTag()); var sourceData = result.Headers.Get <IMessage>(IntegrationMessageHeaderAccessor.SOURCE_DATA); Assert.Same(jsonMessage, sourceData); }
public void TestCorrelationNotPassedFromRequestHeaderIfAlreadySetByHandler() { object correlationId = "123-ABC"; IMessage message = MessageBuilder.WithPayload("test").SetCorrelationId(correlationId).Build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "CreateMessage"); serviceActivator.OutputChannel = outputChannel; EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.Start(); Assert.IsTrue(inputChannel.Send(message)); IMessage reply = outputChannel.Receive(TimeSpan.Zero); Assert.That(reply.Headers.CorrelationId, Is.EqualTo("456-XYZ")); }
public void TestCorrelationIdCopiedFromMessageCorrelationIdIfAvailable() { IMessage message = MessageBuilder.WithPayload("test").SetCorrelationId("correlationId").Build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "UpperCase"); serviceActivator.OutputChannel = outputChannel; EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.Start(); Assert.IsTrue(inputChannel.Send(message)); IMessage reply = outputChannel.Receive(TimeSpan.Zero); Assert.That(reply.Headers.CorrelationId, Is.EqualTo(message.Headers.CorrelationId)); Assert.IsTrue(message.Headers.CorrelationId.Equals(reply.Headers.CorrelationId)); }
public void TestPostReceiveInterceptor() { AtomicInteger invokedCount = new AtomicInteger(); AtomicInteger messageCount = new AtomicInteger(); QueueChannel channel = new QueueChannel(); channel.AddInterceptor(new TestPostReceiveInterceptorInterceptor(invokedCount, messageCount, channel)); channel.Receive(TimeSpan.Zero); Assert.That(invokedCount.Value, Is.EqualTo(1)); Assert.That(messageCount.Value, Is.EqualTo(0)); channel.Send(new StringMessage("test")); IMessage result = channel.Receive(TimeSpan.Zero); Assert.IsNotNull(result); Assert.That(invokedCount.Value, Is.EqualTo(2)); Assert.That(messageCount.Value, Is.EqualTo(1)); }
public async Task TestCorrelationNotCopiedFromRequestMessgeIdIfAlreadySetByHandler() { IMessage message = Message.Create("test"); var inputChannel = new DirectChannel(provider.GetService <IApplicationContext>()); var outputChannel = new QueueChannel(provider.GetService <IApplicationContext>(), 1); var serviceActivator = new ServiceActivatingHandler(provider.GetService <IApplicationContext>(), new TestBeanCreateMessage()); serviceActivator.OutputChannel = outputChannel; var endpoint = new EventDrivenConsumerEndpoint(provider.GetService <IApplicationContext>(), inputChannel, serviceActivator); await endpoint.Start(); Assert.True(inputChannel.Send(message)); var reply = outputChannel.Receive(0); var accessor = new IntegrationMessageHeaderAccessor(reply); Assert.Equal("456-XYZ", accessor.GetCorrelationId()); }
public void MultiConsumerYielding() { var queues = new List <IFiber>(); IChannel <string> channel = new QueueChannel <string>(); int o = 0; //Init executing Fibers for (int i = 0; i < 5; i++) { void OnReceive(string message) { var firstChar = message[0]; if (firstChar == firstChar) { o++; } } IFiber threadFiber = new ThreadFiber(new Executor(), new TimerScheduler(), new YieldingQueue(), i.ToString()); threadFiber.Start(); queues.Add(threadFiber); channel.Subscribe(threadFiber, OnReceive); } Stopwatch sw = new Stopwatch(); sw.Start(); //Push messages for (int i = 0; i < 1000000; i++) { string msg = "[" + i + "] Push"; channel.Publish(msg); } sw.Stop(); Console.WriteLine("End : " + sw.ElapsedMilliseconds); // Console.ReadLine(); //#Results: //1 ThreadFiber ~= 1sec //2 ThreadFiber ~=> 3sec //3 ThreadFiber ~=> 5sec //4 ThreadFiber ~=> 8sec //5 ThreadFiber ~=> 10sec }
public void CreateAndUseQueueChannelTenTimes() { QueueChannel channel = new QueueChannel(1); Thread thread = new Thread(new ThreadStart(delegate() { for (int k = 1; k <= 10; k++) { channel.Send(k); } })); thread.Start(); for (int j = 1; j <= 10; j++) { Assert.AreEqual(j, channel.Receive()); } }
public void TestPostSendInterceptorWithUnsentMessage() { AtomicInteger invokedCounter = new AtomicInteger(0); AtomicInteger sentCounter = new AtomicInteger(0); QueueChannel singleItemChannel = new QueueChannel(1); singleItemChannel.AddInterceptor(new TestPostSendInterceptorWithUnsentMessageInterceptor(invokedCounter, sentCounter, singleItemChannel)); Assert.That(invokedCounter.Value, Is.EqualTo(0)); Assert.That(sentCounter.Value, Is.EqualTo(0)); singleItemChannel.Send(new StringMessage("test1")); Assert.That(invokedCounter.Value, Is.EqualTo(1)); Assert.That(sentCounter.Value, Is.EqualTo(1)); singleItemChannel.Send(new StringMessage("test2"), TimeSpan.Zero); Assert.That(invokedCounter.Value, Is.EqualTo(2)); Assert.That(sentCounter.Value, Is.EqualTo(1)); }
public async Task TestCorrelationIdPassedIfAvailable() { object correlationId = "123-ABC"; var message = Support.MessageBuilder.WithPayload("test").SetCorrelationId(correlationId).Build(); var inputChannel = new DirectChannel(provider); var outputChannel = new QueueChannel(provider, 1); var serviceActivator = new ServiceActivatingHandler(provider, new TestBeanUpperCase()); serviceActivator.OutputChannel = outputChannel; var endpoint = new EventDrivenConsumerEndpoint(provider, inputChannel, serviceActivator); await endpoint.Start(); Assert.True(inputChannel.Send(message)); var reply = outputChannel.Receive(0); var accessor = new IntegrationMessageHeaderAccessor(reply); Assert.Equal(correlationId, accessor.GetCorrelationId()); }
public void ListWithRequestReplyHandler() { handler.ReturnValue = new List <string>() { "foo", "bar" }; var channel = new QueueChannel(provider.GetService <IApplicationContext>()); var message = IntegrationMessageBuilder.WithPayload("test").SetReplyChannel(channel).Build(); handler.HandleMessage(message); var reply1 = channel.Receive(0); var reply2 = channel.Receive(0); Assert.NotNull(reply1); Assert.Null(reply2); Assert.IsType <List <string> >(reply1.Payload); Assert.Equal(2, ((List <string>)reply1.Payload).Count); }
public async Task TestCorrelationNotPassedFromRequestHeaderIfAlreadySetByHandler() { object correlationId = "123-ABC"; var message = Support.IntegrationMessageBuilder.WithPayload("test").SetCorrelationId(correlationId).Build(); var inputChannel = new DirectChannel(provider.GetService <IApplicationContext>()); var outputChannel = new QueueChannel(provider.GetService <IApplicationContext>(), 1); var serviceActivator = new ServiceActivatingHandler(provider.GetService <IApplicationContext>(), new TestBeanCreateMessage()); serviceActivator.OutputChannel = outputChannel; var endpoint = new EventDrivenConsumerEndpoint(provider.GetService <IApplicationContext>(), inputChannel, serviceActivator); await endpoint.Start(); Assert.True(inputChannel.Send(message)); var reply = outputChannel.Receive(0); var accessor = new IntegrationMessageHeaderAccessor(reply); Assert.Equal("456-XYZ", accessor.GetCorrelationId()); }
public void TestSolicitResponse() { QueueChannel replyChannel = new QueueChannel(); replyChannel.Send(new StringMessage("foo")); GatewayProxyFactoryObject proxyFactory = new GatewayProxyFactoryObject(); proxyFactory.ServiceInterface = typeof(ITestService); proxyFactory.DefaultRequestChannel = new DirectChannel(); proxyFactory.DefaultReplyChannel = replyChannel; proxyFactory.AfterPropertiesSet(); ITestService service = (ITestService)proxyFactory.GetObject(); string result = service.SolicitResponse(); Assert.IsNotNull(result); Assert.That(result, Is.EqualTo("foo")); }
public void OutputChannelTakesPrecedence() { QueueChannel channel1 = new QueueChannel(1); QueueChannel channel2 = new QueueChannel(1); ServiceActivatingHandler endpoint = CreateEndpoint(); endpoint.OutputChannel = channel1; IMessage message = MessageBuilder.WithPayload("foo").SetReplyChannel(channel2).Build(); endpoint.HandleMessage(message); IMessage reply1 = channel1.Receive(TimeSpan.Zero); Assert.IsNotNull(reply1); Assert.That(reply1.Payload, Is.EqualTo("FOO")); IMessage reply2 = channel2.Receive(TimeSpan.Zero); Assert.IsNull(reply2); }
public void testPurgeSubsetWithSelector() { QueueChannel channel = new QueueChannel(); channel.Send(new StringMessage("test1")); channel.Send(new StringMessage("test2")); channel.Send(new StringMessage("test3")); ChannelPurger purger = new ChannelPurger(delegate(IMessage msg) { return(msg.Payload.Equals("test2")); }, channel); IList <IMessage> purgedMessages = purger.Purge(); Assert.That(purgedMessages.Count, Is.EqualTo(2)); IMessage message = channel.Receive(TimeSpan.Zero); Assert.IsNotNull(message); Assert.That(message.Payload, Is.EqualTo("test2")); Assert.IsNull(channel.Receive(TimeSpan.Zero)); }
public void testPurgeNoneWithSelectorAndMultipleChannels() { QueueChannel channel1 = new QueueChannel(); QueueChannel channel2 = new QueueChannel(); channel1.Send(new StringMessage("test1")); channel1.Send(new StringMessage("test2")); channel2.Send(new StringMessage("test1")); channel2.Send(new StringMessage("test2")); ChannelPurger purger = new ChannelPurger(delegate { return(true); }, channel1, channel2); IList <IMessage> purgedMessages = purger.Purge(); Assert.That(purgedMessages.Count, Is.EqualTo(0)); Assert.IsNotNull(channel1.Receive(TimeSpan.Zero)); Assert.IsNotNull(channel1.Receive(TimeSpan.Zero)); Assert.IsNotNull(channel2.Receive(TimeSpan.Zero)); Assert.IsNotNull(channel2.Receive(TimeSpan.Zero)); }
public void CreateAndUseQueueChannelWithMoreEntriesThanSize() { QueueChannel channel = new QueueChannel(10); Thread thread = new Thread(new ThreadStart(delegate() { for (int k = 1; k <= 20; k++) { channel.Send(k); } })); thread.Start(); for (int k = 1; k <= 20; k++) { Assert.AreEqual(k, channel.Receive()); } }
public void TestPostSendInterceptorWithUnsentMessage() { var singleItemChannel = new QueueChannel(provider, 1); var interceptor = new TestPostSendInterceptorWithUnsentMessageInterceptor(); singleItemChannel.AddInterceptor(interceptor); Assert.Equal(0, interceptor.InvokedCounter); Assert.Equal(0, interceptor.SentCounter); singleItemChannel.Send(new GenericMessage("test1")); Assert.Equal(1, interceptor.InvokedCounter); Assert.Equal(1, interceptor.SentCounter); singleItemChannel.Send(new GenericMessage("test2"), 0); Assert.Equal(2, interceptor.InvokedCounter); Assert.Equal(1, interceptor.SentCounter); Assert.NotNull(singleItemChannel.RemoveInterceptor(0)); singleItemChannel.Send(new GenericMessage("test2"), 0); Assert.Equal(2, interceptor.InvokedCounter); Assert.Equal(1, interceptor.SentCounter); }
public void InterceptedMessageContainsHeaderValue() { QueueChannel mainChannel = new QueueChannel(); QueueChannel secondaryChannel = new QueueChannel(); mainChannel.AddInterceptor(new WireTap(secondaryChannel)); string headerName = "testAttribute"; IMessage message = MessageBuilder.WithPayload("testing").SetHeader(headerName, 123).Build(); mainChannel.Send(message); IMessage original = mainChannel.Receive(TimeSpan.Zero); IMessage intercepted = secondaryChannel.Receive(TimeSpan.Zero); object originalAttribute = original.Headers.Get(headerName); object interceptedAttribute = intercepted.Headers.Get(headerName); Assert.IsNotNull(originalAttribute); Assert.IsNotNull(interceptedAttribute); Assert.That(original, Is.EqualTo(intercepted)); }
public void TestPostSendInterceptorWithUnsentMessage() { var singleItemChannel = new QueueChannel(provider.GetService <IApplicationContext>(), 1); var interceptor = new TestPostSendInterceptorWithUnsentMessageInterceptor(); singleItemChannel.AddInterceptor(interceptor); Assert.Equal(0, interceptor.InvokedCounter); Assert.Equal(0, interceptor.SentCounter); singleItemChannel.Send(Message.Create("test1")); Assert.Equal(1, interceptor.InvokedCounter); Assert.Equal(1, interceptor.SentCounter); singleItemChannel.Send(Message.Create("test2"), 0); Assert.Equal(2, interceptor.InvokedCounter); Assert.Equal(1, interceptor.SentCounter); Assert.NotNull(singleItemChannel.RemoveInterceptor(0)); singleItemChannel.Send(Message.Create("test2"), 0); Assert.Equal(2, interceptor.InvokedCounter); Assert.Equal(1, interceptor.SentCounter); }
public void TestBlockingReceiveWithTimeout() { QueueChannel channel = new QueueChannel(); AtomicBoolean receiveInterrupted = new AtomicBoolean(false); CountDownLatch latch = new CountDownLatch(1); Thread t = new Thread(new ThreadStart(delegate { IMessage message = channel.Receive(new TimeSpan(10000)); receiveInterrupted.Value = true; Assert.IsTrue(message == null); latch.CountDown(); })); t.Start(); //Assert.IsFalse(receiveInterrupted.Value); t.Interrupt(); latch.Await(); Assert.IsTrue(receiveInterrupted.Value); }
public void TestPurge() { QueueChannel channel = new QueueChannel(2); TimeSpan minute = new TimeSpan(0, 0, 1, 0); DateTime now = DateTime.Now; DateTime past = now.Subtract(minute); DateTime future = now.Add(minute); IMessage expiredMessage = MessageBuilder.WithPayload("test1").SetExpirationDate(past).Build(); IMessage unexpiredMessage = MessageBuilder.WithPayload("test2").SetExpirationDate(future).Build(); Assert.IsTrue(channel.Send(expiredMessage, TimeSpan.Zero)); Assert.IsTrue(channel.Send(unexpiredMessage, TimeSpan.Zero)); Assert.IsFalse(channel.Send(new StringMessage("atCapacity"), TimeSpan.Zero)); IList <IMessage> purgedMessages = channel.Purge(new UnexpiredMessageSelector()); Assert.IsNotNull(purgedMessages); Assert.That(purgedMessages.Count, Is.EqualTo(1)); Assert.IsTrue(channel.Send(new StringMessage("roomAvailable"), TimeSpan.Zero)); }
public void TestTwoRequiredGroups() { var binder = GetBinder(); var bindingsOptions = new RabbitBindingsOptions(); var producerOptions = GetProducerOptions("input", bindingsOptions); var producerBindingOptions = CreateProducerBindingOptions(producerOptions); var output = CreateBindableChannel("output", producerBindingOptions); var testDestination = "testDestination" + Guid.NewGuid().ToString().Replace("-", string.Empty); producerOptions.RequiredGroups = new List <string>() { "test1", "test2" }; var producerBinding = binder.BindProducer(testDestination, output, producerOptions); var testPayload = "foo-" + Guid.NewGuid().ToString(); output.Send(MessageBuilder.WithPayload(testPayload).SetHeader("contentType", MimeTypeUtils.TEXT_PLAIN).Build()); var inbound1 = new QueueChannel(); var consumerOptions = GetConsumerOptions("output", bindingsOptions); var consumerBinding1 = binder.BindConsumer(testDestination, "test1", inbound1, consumerOptions); var inbound2 = new QueueChannel(); var consumerBinding2 = binder.BindConsumer(testDestination, "test2", inbound2, consumerOptions); var receivedMessage1 = (Message <byte[]>)Receive(inbound1); Assert.NotNull(receivedMessage1); Assert.Equal(testPayload, Encoding.UTF8.GetString(receivedMessage1.Payload)); var receivedMessage2 = (Message <byte[]>)Receive(inbound2); Assert.NotNull(receivedMessage2); Assert.Equal(testPayload, Encoding.UTF8.GetString(receivedMessage2.Payload)); consumerBinding1.Unbind(); consumerBinding2.Unbind(); producerBinding.Unbind(); }
public void TestRequestReplyWithTypeConversion() { QueueChannel requestChannel = new QueueChannel(); new Thread(new ThreadStart(delegate { IMessage input = requestChannel.Receive(); StringMessage reply = new StringMessage(input.Payload + "456"); ((IMessageChannel)input.Headers.ReplyChannel).Send(reply); })).Start(); GatewayProxyFactoryObject proxyFactory = new GatewayProxyFactoryObject(); proxyFactory.ServiceInterface = typeof(ITestService); proxyFactory.DefaultRequestChannel = requestChannel; proxyFactory.AfterPropertiesSet(); ITestService service = (ITestService)proxyFactory.GetObject(); int result = service.RequestReplyWithIntegers(123); Assert.That(result, Is.EqualTo(123456)); }
public void TestMessageAsReturnValue() { QueueChannel requestChannel = new QueueChannel(); new Thread(new ThreadStart(delegate { IMessage input = requestChannel.Receive(); StringMessage reply = new StringMessage(input.Payload + "bar"); ((IMessageChannel)input.Headers.ReplyChannel).Send(reply); })).Start(); GatewayProxyFactoryObject proxyFactory = new GatewayProxyFactoryObject(); proxyFactory.ServiceInterface = typeof(ITestService); proxyFactory.DefaultRequestChannel = requestChannel; proxyFactory.AfterPropertiesSet(); ITestService service = (ITestService)proxyFactory.GetObject(); IMessage result = service.RequestReplyWithMessageReturnValue("foo"); Assert.That(result.Payload, Is.EqualTo("foobar")); }
public void TestBlockingSendWithTimeout() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var channel = new QueueChannel(provider, 1); var result1 = channel.Send(new GenericMessage("test-1")); Assert.True(result1); var latch = new CountdownEvent(1); Task.Run(() => { channel.Send(new GenericMessage("test-2"), 5); latch.Signal(); }); Assert.True(latch.Wait(10000)); }
public void TestSimpleSendAndReceive() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var latch = new CountdownEvent(1); var channel = new QueueChannel(provider); Task.Run(() => { var message = channel.Receive(); if (message != null) { latch.Signal(); } }); channel.Send(new GenericMessage("testing")); Assert.True(latch.Wait(10000)); }
public void TestImmediateSend() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var channel = new QueueChannel(provider, 3); var result1 = channel.Send(new GenericMessage("test-1")); Assert.True(result1); var result2 = channel.Send(new GenericMessage("test-2"), 100); Assert.True(result2); var result3 = channel.Send(new GenericMessage("test-3"), 0); Assert.True(result3); var result4 = channel.Send(new GenericMessage("test-4"), 0); Assert.False(result4); }
public void TestBlockingReceiveWithTimeout() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var messageNull = false; var channel = new QueueChannel(provider); var latch = new CountdownEvent(1); Task.Run(() => { var message = channel.Receive(5); messageNull = message == null; latch.Signal(); }); Assert.True(latch.Wait(10000)); Assert.True(messageNull); }
public void MultiConsumer() { var queues = new List<FiberBase>(); IChannel<string> channel = new QueueChannel<string>(); //Init executing Fibers for (int i = 0; i < 5; i++) { Action<string> onReceive = (message) => { var firstChar = message[0]; }; FiberBase threadFiber = new ThreadFiber(new Executor(),new TimerScheduler(),new SleepingQueue() , i.ToString());//new DisruptorQueue(1024*1024) queues.Add(threadFiber); channel.Subscribe(threadFiber, onReceive); } Stopwatch sw = new Stopwatch(); sw.Start(); //Push messages for (int i = 0; i < 1000000; i++) { string msg = "[" + i + "] Push"; channel.Publish(msg); } sw.Stop(); Console.WriteLine("End : " + sw.ElapsedMilliseconds); // Console.ReadLine(); //#Results: //1 ThreadFiber ~= 1sec //2 ThreadFiber ~=> 3sec //3 ThreadFiber ~=> 5sec //4 ThreadFiber ~=> 8sec //5 ThreadFiber ~=> 10sec }
public void SingleConsumerWithException() { var exec = new StubExecutor(); var one = new PoolFiber(new DefaultThreadPool(), exec); one.Start(); var reset = new AutoResetEvent(false); using (one) { var channel = new QueueChannel<int>(); Action<int> onMessage = delegate(int num) { if (num == 0) { throw new Exception(); } reset.Set(); }; channel.Subscribe(one, onMessage); channel.Publish(0); channel.Publish(1); Assert.IsTrue(reset.WaitOne(10000, false)); Assert.AreEqual(1, exec.failed.Count); } }
public void Multiple() { var queues = new List<IFiber>(); int receiveCount = 0; using (var reset = new AutoResetEvent(false)) { var channel = new QueueChannel<int>(); const int MessageCount = 100; var updateLock = new object(); for (int i = 0; i < 5; i++) { Action<int> onReceive = delegate { Thread.Sleep(15); lock (updateLock) { receiveCount++; if (receiveCount == MessageCount) reset.Set(); } }; IFiber fiber = PoolFiber.StartNew(); queues.Add(fiber); channel.Subscribe(fiber, onReceive); } for (int i = 0; i < MessageCount; i++) channel.Publish(i); Assert.IsTrue(reset.WaitOne(10000, false)); queues.ForEach(q => q.Dispose()); } }
public void SingleConsumer() { int oneConsumed = 0; using (IFiber one = PoolFiber.StartNew()) using (var reset = new AutoResetEvent(false)) { var channel = new QueueChannel<int>(); Action<int> onMsg = obj => { oneConsumed++; if (oneConsumed == 20) reset.Set(); }; channel.Subscribe(one, onMsg); for (int i = 0; i < 20; i++) channel.Publish(i); Assert.IsTrue(reset.WaitOne(10000, false)); } }
public void SingleConsumerWithException() { var failed = new List<Exception>(); var exec = new ExceptionHandlingExecutor(failed.Add); using (IFiber one = PoolFiber.StartNew(exec)) using (var reset = new AutoResetEvent(false)) { var channel = new QueueChannel<int>(); Action<int> onMsg = num => { if (num == 0) throw new Exception(); reset.Set(); }; channel.Subscribe(one, onMsg); channel.Publish(0); channel.Publish(1); Assert.IsTrue(reset.WaitOne(10000, false)); Assert.AreEqual(1, failed.Count); } }