Exemple #1
0
        // todo: What if I want to publish a single message, but get multiple responses?
        internal void Execute(ServiceBusRuntime runtime)
        {
            lock (_executeLock)
            {
                Event.Reset();

                SubscriptionEndpoint subscription = new SubscriptionEndpoint(Guid.NewGuid(), "Heartbeat " + HeartbeatId, null, null, HearbeatRequest.ContractType, new HeartbeatReplyDispatcher(this), ResponseFilter, true);
                runtime.Subscribe(subscription);
                try
                {
                    runtime.PublishOneWay(HearbeatRequest);
                    if (Event.WaitOne(Timeout))
                    {
                        // Heartbeat success
                        runtime.PublishOneWay(SuccessRequest);
                    }
                    else
                    {
                        // Hearbeat timeout
                        runtime.PublishOneWay(FailureRequest);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    runtime.RemoveSubscription(subscription);
                }
            }
        }
        public void Not_Yet_Expired_Subscriptions_Get_Messages()
        {
            using (var serviceBusRuntime = new ServiceBusRuntime())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message            = "Publish this message";
                ContractImplementation ci = new ContractImplementation();

                tester.AddTestSubscription(ci, new PassThroughMessageFilter(), DateTime.MaxValue);


                try
                {
                    tester.WaitForDeliveries(2, TimeSpan.FromSeconds(1), () =>
                    {
                        serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                    });
                }
                catch (TimeoutException)
                {
                    Assert.Fail("Message should have been delivered to not yet expired subscription.");
                }
            }
        }
        public void FileSystemDispatcher_Picks_Up_Existing_Messages()
        {
            BinaryMessageEncodingBindingElement element = new BinaryMessageEncodingBindingElement();
            MessageEncoder encoder = element.CreateMessageEncoderFactory().Encoder;

            ServiceBusRuntime dispatchRuntime = new ServiceBusRuntime(new DirectDeliveryCore());
            var subscription = new SubscriptionEndpoint(Guid.NewGuid(), "File System Dispatcher", null, null, typeof(IContract), new FileSystemDispatcher(new ConverterMessageDeliveryWriterFactory(encoder, typeof(IContract)), Config.IncomingFilePath), new PassThroughMessageFilter());

            dispatchRuntime.Subscribe(subscription);

            ServiceBusRuntime listenerRuntime = new ServiceBusRuntime(new DirectDeliveryCore());
            var listener = new ListenerEndpoint(Guid.NewGuid(), "File System Listener", null, null, typeof(IContract), new FileSystemListener(new ConverterMessageDeliveryReaderFactory(encoder, typeof(IContract)), Config.IncomingFilePath, Config.ProcessedFilePath));

            listenerRuntime.AddListener(listener);
            listenerRuntime.Subscribe(new SubscriptionEndpoint(Guid.NewGuid(), "Pass through", null, null, typeof(IContract), new ActionDispatcher((se, md) => { }), new PassThroughMessageFilter()));

            var dispatchTester = new ServiceBusTest(dispatchRuntime);
            var listenerTester = new ServiceBusTest(listenerRuntime);


            string message = "test this thing";

            dispatchTester.StartAndStop(() =>
            {
                dispatchRuntime.PublishOneWay(typeof(IContract), "PublishThis", message);

                listenerTester.WaitForDeliveries(1, TimeSpan.FromSeconds(10), () =>
                {
                });
            });

            dispatchRuntime.RemoveSubscription(subscription);
        }
        public void FileSystemDispatcher_Picks_Up_Existing_Messages()
        {
            BinaryMessageEncodingBindingElement element = new BinaryMessageEncodingBindingElement();
            MessageEncoder encoder = element.CreateMessageEncoderFactory().Encoder;

            ServiceBusRuntime dispatchRuntime = new ServiceBusRuntime(new DirectDeliveryCore());
            var subscription = new SubscriptionEndpoint(Guid.NewGuid(), "File System Dispatcher", null, null, typeof(IContract), new FileSystemDispatcher(new ConverterMessageDeliveryWriterFactory(encoder,typeof(IContract)),Config.IncomingFilePath), new PassThroughMessageFilter());
            dispatchRuntime.Subscribe(subscription);

            ServiceBusRuntime listenerRuntime = new ServiceBusRuntime(new DirectDeliveryCore());
            var listener = new ListenerEndpoint(Guid.NewGuid(), "File System Listener", null, null, typeof(IContract), new FileSystemListener(new ConverterMessageDeliveryReaderFactory(encoder, typeof(IContract)),Config.IncomingFilePath, Config.ProcessedFilePath));
            listenerRuntime.AddListener(listener);
            listenerRuntime.Subscribe(new SubscriptionEndpoint(Guid.NewGuid(), "Pass through", null, null, typeof(IContract), new ActionDispatcher((se, md) => { }), new PassThroughMessageFilter()));

            var dispatchTester = new ServiceBusTest(dispatchRuntime);
            var listenerTester = new ServiceBusTest(listenerRuntime);

            string message = "test this thing";

            dispatchTester.StartAndStop(() =>
            {
                dispatchRuntime.PublishOneWay(typeof(IContract), "PublishThis", message);

                listenerTester.WaitForDeliveries(1, TimeSpan.FromSeconds(10), () =>
                {
                });
            });

            dispatchRuntime.RemoveSubscription(subscription);
        }
        public void TestPythonTransformation()
        {
            ScriptTransformationDispatcher dispatcher = new ScriptTransformationDispatcher("py",
                                                                                           @"import clr

clr.AddReference(""IServiceOriented.ServiceBus"")
clr.AddReference(""IServiceOriented.ServiceBus.Scripting.UnitTests"")

from IServiceOriented.ServiceBus import PublishRequest
from IServiceOriented.ServiceBus.Scripting.UnitTests import AfterTransformation

def Execute():
    outgoing = AfterTransformation(int(request.Message.Value));
    return PublishRequest(request.ContractType, request.Action, outgoing)
"
                                                                                           , Microsoft.Scripting.SourceCodeKind.Statements);

            dispatcher.Script.Check();

            dispatcher.Script.ExecuteWithVariables(new Dictionary <string, object>()
            {
                { "request", new PublishRequest(null, null, new BeforeTransformation()
                    {
                        Value = "1000"
                    }) }
            });

            AutoResetEvent reset = new AutoResetEvent(false);

            bool success = false;

            ServiceBusRuntime runtime = new ServiceBusRuntime(new QueuedDeliveryCore(new NonTransactionalMemoryQueue(), new NonTransactionalMemoryQueue(), new NonTransactionalMemoryQueue()));

            runtime.Subscribe(new SubscriptionEndpoint(Guid.NewGuid(), "Tranformation", null, null, typeof(void), dispatcher, new TypedMessageFilter(typeof(BeforeTransformation))));
            runtime.Subscribe(new SubscriptionEndpoint(Guid.NewGuid(), "AfterTransformation", null, null, typeof(void), new ActionDispatcher((subscription, md) =>
            {
                try
                {
                    success = ((AfterTransformation)md.Message).Value == 1000;
                }
                finally
                {
                    reset.Set();
                }
            }), new TypedMessageFilter(typeof(AfterTransformation))));
            runtime.Start();

            runtime.PublishOneWay(new PublishRequest(null, null, new BeforeTransformation()
            {
                Value = "1000"
            }));

            if (!reset.WaitOne(1000 * 10, true))
            {
                Assert.Fail("Waited too long");
            }

            runtime.Stop();
        }
 public void SendMessage(SendMessageRequest request)
 {
     if (request.From != "System")
     {
         _serviceBus.PublishOneWay(new PublishRequest(typeof(IChatService), "SendMessage", new SendMessageRequest("System", request.From, request.To + " is an invalid user"),
                                                      new MessageDeliveryContext(new KeyValuePair <MessageDeliveryContextKey, object>[] { new KeyValuePair <MessageDeliveryContextKey, object>(MessageDelivery.PrimaryIdentityNameKey, "SYSTEM") })));
     }
 }
Exemple #7
0
        public void SmtpDispatcher_Can_Send_Messages()
        {
            if (Config.FromMailAddress != null && Config.ToMailAddress != null)
            {
                ServiceBusRuntime dispatchRuntime = new ServiceBusRuntime(new DirectDeliveryCore());
                var subscription = new SubscriptionEndpoint(Guid.NewGuid(), "Smtp Dispatcher", null, null, typeof(IContract), new SmtpDispatcher("this is a test", new MailAddress(Config.FromMailAddress), new MailAddress[] { new MailAddress(Config.ToMailAddress) }), new PassThroughMessageFilter());

                ServiceBusTest tester = new ServiceBusTest(dispatchRuntime);
                tester.StartAndStop(() =>
                {
                    dispatchRuntime.Subscribe(subscription);
                    dispatchRuntime.PublishOneWay(typeof(IContract), "PublishThis", "this is a test message");
                });
            }
            else
            {
                NUnit.Framework.Assert.Ignore("From and to email addresses must be configured to run smtp tests");
            }
        }
        public void Heartbeat_Response_Causes_Success_Request()
        {
            using (HeartbeatRuntimeService heartbeatService = new HeartbeatRuntimeService())
            {
                using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore(), new TimerRuntimeService(), heartbeatService))
                {
                    heartbeatService.RegisterHeartbeat(new Heartbeat(Guid.NewGuid(), TimeSpan.FromSeconds(5), new PublishRequest(null, null, "BeatRequest"),
                                                        new PublishRequest(null, null, "Success"), new PublishRequest(null, null, "Failure"),
                                                        new PredicateMessageFilter(pr => (string)pr.Message == "BeatResponse"), TimeSpan.FromSeconds(1)));

                    AutoResetEvent responseEvt = new AutoResetEvent(false);
                    AutoResetEvent failEvt = new AutoResetEvent(false);
                    AutoResetEvent successEvt = new AutoResetEvent(false);

                    runtime.Subscribe(new SubscriptionEndpoint("BeatResponse", null, null, null, new ActionDispatcher((se, md) => { responseEvt.Set(); runtime.PublishOneWay(new PublishRequest(null, null, "BeatResponse")); }), new PredicateMessageFilter(pr => (string)pr.Message == "BeatRequest")));
                    runtime.Subscribe(new SubscriptionEndpoint("Success", null, null, null, new ActionDispatcher((se, md) => successEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Success")));
                    runtime.Subscribe(new SubscriptionEndpoint("Failure", null, null, null, new ActionDispatcher((se, md) => failEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Failure")));

                    ServiceBusTest tester = new ServiceBusTest(runtime);

                    tester.StartAndStop(() =>
                    {
                        if (!successEvt.WaitOne(TimeSpan.FromSeconds(10)))
                        {
                            if (!responseEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat request was never published.");
                            }
                            if (failEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat fail event was published instead of the success event");
                            }
                            Assert.Fail("The heartbeat success event was not published");
                        }
                    });

                }
            }
        }
Exemple #9
0
        public void Heartbeat_Response_Causes_Success_Request()
        {
            using (HeartbeatRuntimeService heartbeatService = new HeartbeatRuntimeService())
            {
                using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore(), new TimerRuntimeService(), heartbeatService))
                {
                    heartbeatService.RegisterHeartbeat(new Heartbeat(Guid.NewGuid(), TimeSpan.FromSeconds(5), new PublishRequest(null, null, "BeatRequest"),
                                                                     new PublishRequest(null, null, "Success"), new PublishRequest(null, null, "Failure"),
                                                                     new PredicateMessageFilter(pr => (string)pr.Message == "BeatResponse"), TimeSpan.FromSeconds(1)));

                    AutoResetEvent responseEvt = new AutoResetEvent(false);
                    AutoResetEvent failEvt     = new AutoResetEvent(false);
                    AutoResetEvent successEvt  = new AutoResetEvent(false);

                    runtime.Subscribe(new SubscriptionEndpoint("BeatResponse", null, null, null, new ActionDispatcher((se, md) => { responseEvt.Set(); runtime.PublishOneWay(new PublishRequest(null, null, "BeatResponse")); }), new PredicateMessageFilter(pr => (string)pr.Message == "BeatRequest")));
                    runtime.Subscribe(new SubscriptionEndpoint("Success", null, null, null, new ActionDispatcher((se, md) => successEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Success")));
                    runtime.Subscribe(new SubscriptionEndpoint("Failure", null, null, null, new ActionDispatcher((se, md) => failEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Failure")));

                    ServiceBusTest tester = new ServiceBusTest(runtime);

                    tester.StartAndStop(() =>
                    {
                        if (!successEvt.WaitOne(TimeSpan.FromSeconds(10)))
                        {
                            if (!responseEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat request was never published.");
                            }
                            if (failEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat fail event was published instead of the success event");
                            }
                            Assert.Fail("The heartbeat success event was not published");
                        }
                    });
                }
            }
        }
        public void Not_Yet_Expired_Subscriptions_Get_Messages()
        {
            using (var serviceBusRuntime = new ServiceBusRuntime())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message = "Publish this message";
                ContractImplementation ci = new ContractImplementation();

                tester.AddTestSubscription(ci, new PassThroughMessageFilter(), DateTime.MaxValue);

                try
                {
                    tester.WaitForDeliveries(2, TimeSpan.FromSeconds(1), () =>
                    {
                        serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                    });
                }
                catch (TimeoutException)
                {
                    Assert.Fail("Message should have been delivered to not yet expired subscription.");
                }
            }
        }
        // todo: What if I want to publish a single message, but get multiple responses?
        internal void Execute(ServiceBusRuntime runtime)
        {
            lock (_executeLock)
            {
                Event.Reset();

                SubscriptionEndpoint subscription = new SubscriptionEndpoint(Guid.NewGuid(), "Heartbeat " + HeartbeatId, null, null, HearbeatRequest.ContractType, new HeartbeatReplyDispatcher(this), ResponseFilter, true);
                runtime.Subscribe(subscription);
                try
                {
                    runtime.PublishOneWay(HearbeatRequest);
                    if (Event.WaitOne(Timeout))
                    {
                        // Heartbeat success
                        runtime.PublishOneWay(SuccessRequest);
                    }
                    else
                    {
                        // Hearbeat timeout
                        runtime.PublishOneWay(FailureRequest);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    runtime.RemoveSubscription(subscription);
                }
            }
        }