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 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 void ReceiveWithDefaultChannelProvidedByConstructor()
 {
     QueueChannel channel = new QueueChannel();
     channel.Send(new StringMessage("test"));
     MessageChannelTemplate template = new MessageChannelTemplate(channel);
     IMessage reply = template.Receive();
     Assert.That(reply.Payload, Is.EqualTo("test"));
 }
 public void testSingleSelectorAccepts()
 {
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector = new TestMessageSelector(true, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     Assert.That(channel.Send(new StringMessage("test1")), Is.True);
 }
        public void TestPostSendInterceptorWithSentMessage()
        {
            QueueChannel channel = new QueueChannel();
            AtomicBoolean invoked = new AtomicBoolean(false);

            channel.AddInterceptor(new TestPostSendInterceptorWithSentMessageInterceptor(invoked, channel));

            channel.Send(new StringMessage("test"));
            Assert.IsTrue(invoked.Value);
        }
 public void ReceiveWithExplicitChannelTakesPrecedenceOverDefault()
 {
     QueueChannel explicitChannel = new QueueChannel();
     QueueChannel defaultChannel = new QueueChannel();
     explicitChannel.Send(new StringMessage("test"));
     MessageChannelTemplate template = new MessageChannelTemplate(defaultChannel);
     template.ReceiveTimeout = TimeSpan.Zero;
     IMessage reply = template.Receive(explicitChannel);
     Assert.That(reply.Payload, Is.EqualTo("test"));
     Assert.IsNull(defaultChannel.Receive(TimeSpan.Zero));
 }
 public void testMultipleSelectorsAccept()
 {
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector1 = new TestMessageSelector(true, counter);
     IMessageSelector selector2 = new TestMessageSelector(true, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     Assert.That(channel.Send(new StringMessage("test1")), Is.True);
     Assert.That(counter.Value, Is.EqualTo(2));
 }
 public void WireTapWithAcceptingSelector()
 {
     QueueChannel mainChannel = new QueueChannel();
     QueueChannel secondaryChannel = new QueueChannel();
     mainChannel.AddInterceptor(new WireTap(secondaryChannel, new TestSelector(true)));
     mainChannel.Send(new StringMessage("testing"));
     IMessage original = mainChannel.Receive(TimeSpan.Zero);
     Assert.IsNotNull(original);
     IMessage intercepted = secondaryChannel.Receive(TimeSpan.Zero);
     Assert.IsNotNull(intercepted);
     Assert.That(original, Is.EqualTo(intercepted));
 }
 public void SimpleTargetWireTap()
 {
     QueueChannel mainChannel = new QueueChannel();
     QueueChannel secondaryChannel = new QueueChannel();
     mainChannel.AddInterceptor(new WireTap(secondaryChannel));
     Assert.IsNull(secondaryChannel.Receive(TimeSpan.Zero));
     mainChannel.Send(new StringMessage("testing"));
     IMessage original = mainChannel.Receive(TimeSpan.Zero);
     Assert.IsNotNull(original);
     IMessage intercepted = secondaryChannel.Receive(TimeSpan.Zero);
     Assert.IsNotNull(intercepted);
     Assert.That(original, Is.EqualTo(intercepted));
 }
 public void testMultipleChannelsWithSelector()
 {
     QueueChannel channel1 = new QueueChannel();
     QueueChannel channel2 = new QueueChannel();
     channel1.Send(new StringMessage("test1"));
     channel1.Send(new StringMessage("test2"));
     channel1.Send(new StringMessage("test3"));
     channel2.Send(new StringMessage("test1"));
     channel2.Send(new StringMessage("test2"));
     channel2.Send(new StringMessage("test3"));
     ChannelPurger purger = new ChannelPurger(delegate(IMessage msg) { return (msg.Payload.Equals("test2")); },
                                              channel1, channel2);
     IList<IMessage> purgedMessages = purger.Purge();
     Assert.That(purgedMessages.Count, Is.EqualTo(4));
     IMessage message1 = channel1.Receive(TimeSpan.Zero);
     Assert.IsNotNull(message1);
     Assert.That(message1.Payload, Is.EqualTo("test2"));
     Assert.IsNull(channel1.Receive(TimeSpan.Zero));
     IMessage message2 = channel2.Receive(TimeSpan.Zero);
     Assert.IsNotNull(message2);
     Assert.That(message2.Payload, Is.EqualTo("test2"));
     Assert.IsNull(channel2.Receive(TimeSpan.Zero));
 }
 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 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 testMultipleSelectorsReject()
 {
     bool exceptionThrown = false;
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector1 = new TestMessageSelector(true, counter);
     IMessageSelector selector2 = new TestMessageSelector(false, counter);
     IMessageSelector selector3 = new TestMessageSelector(false, counter);
     IMessageSelector selector4 = new TestMessageSelector(true, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2, selector3,
                                                                               selector4);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     try
     {
         channel.Send(new StringMessage("test1"));
     }
     catch (MessageDeliveryException)
     {
         exceptionThrown = true;
     }
     Assert.That(exceptionThrown, Is.True);
     Assert.That(counter.Value, Is.EqualTo(2));
 }
 public void testSingleSelectorRejects()
 {
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector = new TestMessageSelector(false, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     channel.Send(new StringMessage("test1"));
 }
 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 TestBlockingSendWithNoTimeout()
 {
     QueueChannel channel = new QueueChannel(1);
     bool result1 = channel.Send(new Message<string>("test-1"));
     Assert.IsTrue(result1);
     AtomicBoolean SendInterrupted = new AtomicBoolean(false);
     CountDownLatch latch = new CountDownLatch(1);
     Thread t = new Thread(new ThreadStart(delegate
                                               {
                                                   channel.Send(new Message<string>("test-2"));
                                                   SendInterrupted.Value = true;
                                                   latch.CountDown();
                                               }));
     t.Start();
     //Assert.IsFalse(SendInterrupted.Value);
     t.Interrupt();
     latch.Await();
     Assert.IsTrue(SendInterrupted.Value);
 }
        public void TestSimpleSendAndReceive()
        {
            AtomicBoolean messageReceived = new AtomicBoolean(false);
            CountDownLatch latch = new CountDownLatch(1);
            QueueChannel channel = new QueueChannel();
            new Thread(new ThreadStart(delegate
                                           {
                                               IMessage message = channel.Receive();
                                               if (message != null)
                                               {
                                                   messageReceived.Value = true;
                                                   latch.CountDown();
                                               }
                                           })).Start();

            Assert.That(messageReceived.Value, Is.False);
            channel.Send(new Message<string>("testing"));
            latch.Await(new TimeSpan(0, 0, 0, 0, 25));
            Assert.That(messageReceived.Value, Is.True);
        }
 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 TestImmediateSend()
 {
     QueueChannel channel = new QueueChannel(3);
     bool result1 = channel.Send(new Message<string>("test-1"));
     Assert.IsTrue(result1);
     bool result2 = channel.Send(new Message<string>("test-2"), new TimeSpan(100));
     Assert.IsTrue(result2);
     bool result3 = channel.Send(new Message<string>("test-3"), TimeSpan.Zero);
     Assert.IsTrue(result3);
     bool result4 = channel.Send(new Message<string>("test-4"), TimeSpan.Zero);
     Assert.IsFalse(result4);
 }
        public void TestImmediateReceive()
        {
            AtomicBoolean messageReceived = new AtomicBoolean(false);
            QueueChannel channel = new QueueChannel();
            CountDownLatch latch1 = new CountDownLatch(1);
            CountDownLatch latch2 = new CountDownLatch(1);
            IExecutor singleThreadExecutor = Executors.NewSingleThreadExecutor();

            singleThreadExecutor.Execute(
                delegate
                    {
                        IMessage message = channel.Receive(TimeSpan.Zero);
                        if (message != null)
                        {
                            messageReceived.Value = true;
                        }
                        latch1.CountDown();
                    });

            latch1.Await();
            singleThreadExecutor.Execute(
                delegate { channel.Send(new Message<string>("testing")); });

            Assert.IsFalse(messageReceived.Value);
            singleThreadExecutor.Execute(
                delegate
                    {
                        IMessage message = channel.Receive(TimeSpan.Zero);
                        if (message != null)
                        {
                            messageReceived.Value = true;
                        }
                        latch2.CountDown();
                    });

            latch2.Await();
            Assert.IsNotNull(messageReceived.Value);
        }
 public void TestClear()
 {
     QueueChannel channel = new QueueChannel(2);
     StringMessage message1 = new StringMessage("test1");
     StringMessage message2 = new StringMessage("test2");
     StringMessage message3 = new StringMessage("test3");
     Assert.IsTrue(channel.Send(message1));
     Assert.IsTrue(channel.Send(message2));
     Assert.IsFalse(channel.Send(message3, TimeSpan.Zero));
     IList<IMessage> clearedMessages = channel.Clear();
     Assert.IsNotNull(clearedMessages);
     Assert.That(clearedMessages.Count, Is.EqualTo(2));
     Assert.IsTrue(channel.Send(message3));
 }
 public void TestPreReceiveInterceptorReturnsFalse()
 {
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(new PreReceiveReturnsFalseInterceptor());
     IMessage message = new StringMessage("test");
     channel.Send(message);
     IMessage result = channel.Receive(TimeSpan.Zero);
     Assert.That(PreReceiveReturnsFalseInterceptor.counter.Value, Is.EqualTo(1));
     Assert.IsNull(result);
 }
 public void TestPreSendInterceptorReturnsNull()
 {
     QueueChannel channel = new QueueChannel();
     PreSendReturnsNullInterceptor interceptor = new PreSendReturnsNullInterceptor();
     channel.AddInterceptor(interceptor);
     IMessage message = new StringMessage("test");
     channel.Send(message);
     Assert.That(interceptor.Count, Is.EqualTo(1));
     IMessage result = channel.Receive(TimeSpan.Zero);
     Assert.IsNull(result);
 }
 public void testPurgeAllWithoutSelector()
 {
     QueueChannel channel = new QueueChannel();
     channel.Send(new StringMessage("test1"));
     channel.Send(new StringMessage("test2"));
     channel.Send(new StringMessage("test3"));
     ChannelPurger purger = new ChannelPurger(channel);
     IList<IMessage> purgedMessages = purger.Purge();
     Assert.That(purgedMessages.Count, Is.EqualTo(3));
     Assert.IsNull(channel.Receive(TimeSpan.Zero));
 }
        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 TestPreSendInterceptorReturnsMessage()
        {
            QueueChannel channel = new QueueChannel();

            channel.AddInterceptor(new PreSendReturnsMessageInterceptor());
            channel.Send(new StringMessage("test"));
            IMessage result = channel.Receive(TimeSpan.Zero);
            Assert.IsNotNull(result);
            Assert.That("test", Is.EqualTo(result.Payload));
            Assert.That(result.Headers.Get(typeof (PreSendReturnsMessageInterceptor).Name), Is.EqualTo(1));
        }