예제 #1
0
        protected ConsumerOptions GetConsumerOptions()
        {
            var options = new ConsumerOptions();

            options.PostProcess();
            return(options);
        }
예제 #2
0
        protected ConsumerOptions GetConsumerOptions(string bindingName)
        {
            var options = new ConsumerOptions();

            options.PostProcess(bindingName);
            return(options);
        }
예제 #3
0
        public void TestConvertSimpler()
        {
            var searchDirectories            = GetSearchDirectories("TestBinder");
            IServiceProvider serviceProvider = CreateStreamsContainer(searchDirectories, "spring:cloud:stream:bindings:foo:contentType=text/plain").BuildServiceProvider();
            var messageConverter             = serviceProvider.GetService <ISmartMessageConverter>();

            Assert.NotNull(messageConverter);

            var binder = serviceProvider.GetService <IBinder>() as AbstractPollableMessageSourceBinder;

            Assert.NotNull(binder);

            var configurer = serviceProvider.GetService <MessageConverterConfigurer>();

            Assert.NotNull(configurer);

            var options = serviceProvider.GetService <IOptions <BindingServiceOptions> >();

            options.Value.Bindings.TryGetValue("foo", out var bindingOptions);
            Assert.Equal("text/plain", bindingOptions.ContentType);

            var setter        = binder.GetType().GetProperty("MessageSourceDelegate").GetSetMethod();
            var messageSource = new TestMessageSource("foo");

            setter.Invoke(binder, new object[] { messageSource });

            var pollableSource = new DefaultPollableMessageSource(serviceProvider.GetService <IApplicationContext>(), messageConverter);

            configurer.ConfigurePolledMessageSource(pollableSource, "foo");
            var properties = new ConsumerOptions()
            {
                MaxAttempts            = 1,
                BackOffInitialInterval = 0
            };

            properties.PostProcess();

            binder.BindConsumer("foo", "bar", pollableSource, properties);
            var handler = new TestConvertSimpleHandler();

            Assert.True(pollableSource.Poll(handler, typeof(string)));

            Assert.IsType <string>(handler.Payload);
            var str = handler.Payload as string;

            Assert.Equal("foo", str);

            Assert.True(pollableSource.Poll(handler, typeof(string)));

            Assert.IsType <string>(handler.Payload);
            str = handler.Payload as string;
            Assert.Equal("foo", str);
        }
예제 #4
0
        public void TestConvertSimple()
        {
            var searchDirectories            = GetSearchDirectories("TestBinder");
            IServiceProvider serviceProvider = CreateStreamsContainer(searchDirectories).BuildServiceProvider();
            var messageConverter             = serviceProvider.GetService <ISmartMessageConverter>();

            Assert.NotNull(messageConverter);

            var binder = serviceProvider.GetService <IBinder>() as AbstractPollableMessageSourceBinder;

            Assert.NotNull(binder);

            var configurer = serviceProvider.GetService <MessageConverterConfigurer>();

            Assert.NotNull(configurer);

            var setter        = binder.GetType().GetProperty("MessageSourceDelegate").GetSetMethod();
            var messageSource = new TestMessageSource("{\"foo\":\"bar\"}");

            setter.Invoke(binder, new object[] { messageSource });

            var pollableSource = new DefaultPollableMessageSource(serviceProvider.GetService <IApplicationContext>(), messageConverter);

            configurer.ConfigurePolledMessageSource(pollableSource, "foo");

            var properties = new ConsumerOptions()
            {
                MaxAttempts            = 2,
                BackOffInitialInterval = 0
            };

            properties.PostProcess();

            binder.BindConsumer("foo", "bar", pollableSource, properties);
            var handler = new TestConvertSimpleHandler();

            Assert.True(pollableSource.Poll(handler, typeof(FooType)));

            Assert.IsType <FooType>(handler.Payload);
            var fooType = handler.Payload as FooType;

            Assert.Equal("bar", fooType.Foo);

            Assert.True(pollableSource.Poll(handler, typeof(FooType)));

            Assert.IsType <FooType>(handler.Payload);
            fooType = handler.Payload as FooType;
            Assert.Equal("bar", fooType.Foo);
        }
예제 #5
0
        protected ConsumerOptions GetConsumerOptions(string bindingName, RabbitBindingsOptions bindingsOptions, RabbitConsumerOptions rabbitConsumerOptions = null, RabbitBindingOptions bindingOptions = null)
        {
            rabbitConsumerOptions ??= new RabbitConsumerOptions();
            rabbitConsumerOptions.PostProcess();

            bindingOptions ??= new RabbitBindingOptions();
            bindingOptions.Consumer = rabbitConsumerOptions;
            bindingsOptions.Bindings.Add(bindingName, bindingOptions);

            var consumerOptions = new ConsumerOptions()
            {
                BindingName = bindingName
            };

            consumerOptions.PostProcess(bindingName);
            return(consumerOptions);
        }
예제 #6
0
        public void TestErrorsNoRetry()
        {
            var searchDirectories            = GetSearchDirectories("TestBinder");
            IServiceProvider serviceProvider = CreateStreamsContainer(searchDirectories).BuildServiceProvider();
            var messageConverter             = serviceProvider.GetService <ISmartMessageConverter>();

            Assert.NotNull(messageConverter);

            var binder = serviceProvider.GetService <IBinder>() as AbstractPollableMessageSourceBinder;

            Assert.NotNull(binder);

            var configurer = serviceProvider.GetService <MessageConverterConfigurer>();

            Assert.NotNull(configurer);

            var pollableSource = new DefaultPollableMessageSource(serviceProvider.GetService <IApplicationContext>(), messageConverter);

            configurer.ConfigurePolledMessageSource(pollableSource, "foo");
            pollableSource.AddInterceptor(new TestSimpleChannelInterceptor());

            var properties = new ConsumerOptions()
            {
                MaxAttempts = 1
            };

            properties.PostProcess();

            var latch = new CountdownEvent(1);

            binder.BindConsumer("foo", "bar", pollableSource, properties);
            var errorChan        = serviceProvider.GetServices <IMessageChannel>().Where(chan => chan.ServiceName == IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME).Single() as ISubscribableChannel;
            var errorChanHandler = new TestErrorsErrorChannelHandler(latch);

            errorChan.Subscribe(errorChanHandler);

            var h1 = new TestFuncMessageHandler((m) =>
            {
                throw new Exception("test recoverer");
            });

            Assert.True(pollableSource.Poll(h1));
            Assert.Equal(1, h1.Count);
        }
예제 #7
0
        public void TestRequeue()
        {
            var searchDirectories            = GetSearchDirectories("TestBinder");
            IServiceProvider serviceProvider = CreateStreamsContainer(searchDirectories).BuildServiceProvider();
            var messageConverter             = serviceProvider.GetService <ISmartMessageConverter>();

            Assert.NotNull(messageConverter);

            var binder = serviceProvider.GetService <IBinder>() as AbstractPollableMessageSourceBinder;

            Assert.NotNull(binder);

            var configurer = serviceProvider.GetService <MessageConverterConfigurer>();

            Assert.NotNull(configurer);

            var pollableSource = new DefaultPollableMessageSource(serviceProvider.GetService <IApplicationContext>(), messageConverter);

            configurer.ConfigurePolledMessageSource(pollableSource, "foo");

            var mockCallback = new Mock <IAcknowledgmentCallback>(MockBehavior.Default);

            pollableSource.AddInterceptor(new TestRequeueChannelInterceptor(mockCallback));
            var properties = new ConsumerOptions()
            {
                MaxAttempts            = 2,
                BackOffInitialInterval = 0
            };

            properties.PostProcess();

            binder.BindConsumer("foo", "bar", pollableSource, properties);
            var h1 = new TestFuncMessageHandler((m) =>
            {
                throw new RequeueCurrentMessageException("test retry");
            });

            Assert.True(pollableSource.Poll(h1));
            Assert.Equal(2, h1.Count);
            mockCallback.Verify((call) => call.Acknowledge(Status.REQUEUE));
        }
예제 #8
0
        public void TestSimple()
        {
            var searchDirectories            = GetSearchDirectories("TestBinder");
            IServiceProvider serviceProvider = CreateStreamsContainer(searchDirectories).BuildServiceProvider();
            var messageConverter             = serviceProvider.GetService <ISmartMessageConverter>();

            Assert.NotNull(messageConverter);

            var binder = serviceProvider.GetService <IBinder>() as AbstractPollableMessageSourceBinder;

            Assert.NotNull(binder);

            var configurer = serviceProvider.GetService <MessageConverterConfigurer>();

            Assert.NotNull(configurer);

            var pollableSource = new DefaultPollableMessageSource(serviceProvider.GetService <IApplicationContext>(), messageConverter);

            configurer.ConfigurePolledMessageSource(pollableSource, "foo");
            pollableSource.AddInterceptor(new TestSimpleChannelInterceptor());

            var properties = new ConsumerOptions()
            {
                MaxAttempts            = 2,
                BackOffInitialInterval = 0
            };

            properties.PostProcess();

            binder.BindConsumer("foo", "bar", pollableSource, properties);
            var handler = new TestSimpleHandler();

            Assert.True(pollableSource.Poll(handler));

            Assert.Equal(2, handler.Count);
        }
        public void TestEndpointLifecycle()
        {
            var binder = serviceProvider.GetService <IBinder>() as TestChannelBinder;

            Assert.NotNull(binder);

            var consumerProperties = new ConsumerOptions()
            {
                MaxAttempts = 1
            };

            consumerProperties.PostProcess();

            // IBinding<IMessageChannel> consumerBinding = await binder.BindConsumer("foo", "fooGroup",  new DirectChannel(serviceProvider),  consumerProperties);
            var consumerBinding = binder.BindConsumer("foo", "fooGroup", new DirectChannel(serviceProvider.GetService <IApplicationContext>()), consumerProperties);

            var defaultBinding = consumerBinding as DefaultBinding <IMessageChannel>;

            Assert.NotNull(defaultBinding);

            // lifecycle
            var messageProducer = defaultBinding.Endpoint as TestChannelBinder.TestMessageProducerSupportEndpoint;

            Assert.NotNull(messageProducer);
            Assert.True(defaultBinding.Endpoint.IsRunning);
            Assert.NotNull(messageProducer.OutputChannel);

            // lifecycle.errorchannel
            Assert.NotNull(messageProducer.ErrorChannel);
            var errorChannel = messageProducer.ErrorChannel as PublishSubscribeChannel;

            Assert.NotNull(errorChannel.Dispatcher);

            // dispatcher.handlers
            Assert.Equal(2, errorChannel.Dispatcher.HandlerCount);
            var dispatcher = errorChannel.Dispatcher as AbstractDispatcher;

            Assert.NotNull(dispatcher);
            var handlers = dispatcher.Handlers;

            Assert.True(handlers[0] is BridgeHandler);
            Assert.True(handlers[1] is ILastSubscriberMessageHandler);

            var registry = serviceProvider.GetRequiredService <IDestinationRegistry>();

            Assert.True(registry.Contains("foo.fooGroup.errors"));
            Assert.True(registry.Contains("foo.fooGroup.errors.recoverer"));
            Assert.True(registry.Contains("foo.fooGroup.errors.handler"));
            Assert.True(registry.Contains("foo.fooGroup.errors.bridge"));

            consumerBinding.Unbind();

            Assert.False(registry.Contains("foo.fooGroup.errors"));
            Assert.False(registry.Contains("foo.fooGroup.errors.recoverer"));
            Assert.False(registry.Contains("foo.fooGroup.errors.handler"));
            Assert.False(registry.Contains("foo.fooGroup.errors.bridge"));

            Assert.False(defaultBinding.Endpoint.IsRunning);

            var producerProps = new ProducerOptions()
            {
                ErrorChannelEnabled = true
            };

            producerProps.PostProcess();

            // IBinding<IMessageChannel> producerBinding = await binder.BindProducer("bar", new DirectChannel(serviceProvider), producerProps);
            var producerBinding = binder.BindProducer("bar", new DirectChannel(serviceProvider.GetService <IApplicationContext>()), producerProps);

            Assert.True(registry.Contains("bar.errors"));
            Assert.True(registry.Contains("bar.errors.bridge"));

            producerBinding.Unbind();
            Assert.False(registry.Contains("bar.errors"));
            Assert.False(registry.Contains("bar.errors.bridge"));
        }
예제 #10
0
        public void TestAnonymousGroupBase()
        {
            B   binder          = GetBinder();
            var producerOptions = new ProducerOptions();

            producerOptions.PostProcess(string.Empty);
            var consumerOptions = new ConsumerOptions();

            consumerOptions.PostProcess(string.Empty);
            var producerBindingOptions = CreateProducerBindingOptions(producerOptions);
            var output = CreateBindableChannel("output", producerBindingOptions);

            var producerBinding = binder.BindProducer(string.Format("defaultGroup%s0", GetDestinationNameDelimiter()), output, producerBindingOptions.Producer);

            QueueChannel input1   = new QueueChannel();
            var          binding1 = binder.BindConsumer(string.Format("defaultGroup%s0", GetDestinationNameDelimiter()), null, input1, consumerOptions);

            QueueChannel input2   = new QueueChannel();
            var          binding2 = binder.BindConsumer(string.Format("defaultGroup%s0", GetDestinationNameDelimiter()), null, input2, consumerOptions);

            var testPayload1 = "foo-" + Guid.NewGuid().ToString();

            output.Send(MessageBuilder.WithPayload(testPayload1)
                        .SetHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
                        .Build());

            Message <byte[]> receivedMessage1 = (Message <byte[]>)Receive(input1);

            Assert.NotNull(receivedMessage1);
            Assert.Equal(testPayload1, Encoding.UTF8.GetString(receivedMessage1.Payload));

            Message <byte[]> receivedMessage2 = (Message <byte[]>)Receive(input2);

            Assert.NotNull(receivedMessage2);
            Assert.Equal(testPayload1, Encoding.UTF8.GetString(receivedMessage2.Payload));

            binding2.Unbind();

            var testPayload2 = "foo-" + Guid.NewGuid().ToString();

            output.Send(MessageBuilder.WithPayload(testPayload2)
                        .SetHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
                        .Build());

            binding2 = binder.BindConsumer(string.Format("defaultGroup%s0", GetDestinationNameDelimiter()), null, input2, consumerOptions);
            var testPayload3 = "foo-" + Guid.NewGuid().ToString();

            output.Send(MessageBuilder.WithPayload(testPayload3)
                        .SetHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
                        .Build());

            receivedMessage1 = (Message <byte[]>)Receive(input1);
            Assert.NotNull(receivedMessage1);
            Assert.Equal(testPayload2, Encoding.UTF8.GetString(receivedMessage1.Payload));
            receivedMessage1 = (Message <byte[]>)Receive(input1);
            Assert.NotNull(receivedMessage1);
            Assert.NotNull(receivedMessage1.Payload);

            receivedMessage2 = (Message <byte[]>)Receive(input2);
            Assert.NotNull(receivedMessage2);
            Assert.Equal(testPayload3, Encoding.UTF8.GetString(receivedMessage2.Payload));

            producerBinding.Unbind();
            binding1.Unbind();
            binding2.Unbind();
        }
예제 #11
0
        public void TestErrors()
        {
            var searchDirectories            = GetSearchDirectories("TestBinder");
            IServiceProvider serviceProvider = CreateStreamsContainer(searchDirectories).BuildServiceProvider();
            var messageConverter             = serviceProvider.GetService <ISmartMessageConverter>();

            Assert.NotNull(messageConverter);

            var binder = serviceProvider.GetService <IBinder>() as AbstractPollableMessageSourceBinder;

            Assert.NotNull(binder);

            var configurer = serviceProvider.GetService <MessageConverterConfigurer>();

            Assert.NotNull(configurer);

            var pollableSource = new DefaultPollableMessageSource(serviceProvider.GetService <IApplicationContext>(), messageConverter);

            configurer.ConfigurePolledMessageSource(pollableSource, "foo");
            pollableSource.AddInterceptor(new TestSimpleChannelInterceptor());

            var properties = new ConsumerOptions()
            {
                MaxAttempts            = 2,
                BackOffInitialInterval = 0,
                RetryableExceptions    = new List <string>()
                {
                    "!System.InvalidOperationException"
                }
            };

            properties.PostProcess();

            var latch = new CountdownEvent(2);

            binder.BindConsumer("foo", "bar", pollableSource, properties);
            var errorChan        = serviceProvider.GetServices <IMessageChannel>().Where(chan => chan.ServiceName == IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME).Single() as ISubscribableChannel;
            var errorChanHandler = new TestErrorsErrorChannelHandler(latch);

            errorChan.Subscribe(errorChanHandler);

            var h1 = new TestFuncMessageHandler((m) =>
            {
                throw new Exception("test recoverer");
            });

            Assert.True(pollableSource.Poll(h1));
            Assert.Equal(2, h1.Count);

            var getter = binder.GetType().GetProperty("LastError").GetGetMethod();

            var lastError = getter.Invoke(binder, new object[0]) as IMessage;

            Assert.NotNull(lastError);

            var lastErrorMessage = ((Exception)lastError.Payload).InnerException.Message;

            Assert.Equal("test recoverer", lastErrorMessage);

            var h2 = new TestFuncMessageHandler((m) =>
            {
                throw new InvalidOperationException("no retries");
            });

            Assert.True(pollableSource.Poll(h2));
            Assert.Equal(1, h2.Count);

            lastError        = getter.Invoke(binder, new object[0]) as IMessage;
            lastErrorMessage = ((Exception)lastError.Payload).InnerException.Message;
            Assert.Equal("no retries", lastErrorMessage);
        }