Example #1
0
        public void Run_RequestReplyClose_ShouldWriteQueueAndReplyCorrect ()
        {
            var io = new TitanicMemoryIO ();

            using (var reqWorker = new FakeRequestMDPWorker ())
            using (var repWorker = new FakeReplyMDPWorker ())
            using (var closeWorker = new FakeCloseMDPWorker ())
            using (var dispatchClient = new FakeDispatchMDPClient ())
            using (var sut = new TitanicBroker (io))
            {
                // 1. Request
                // 2. Reply -> test
                reqWorker.Request = new NetMQMessage ();
                reqWorker.Request.Push ("Request Data");
                reqWorker.Request.Push ("echo");

                // start the process chain - worker & client should only run until they hit an AutoResetEvent
                Task.Factory.StartNew (() => sut.Run (reqWorker, repWorker, closeWorker, dispatchClient));

                // give system time to act
                Thread.Sleep (_sleep_for);

                // get reply id for further processing
                var s = reqWorker.Reply.Last.ConvertToString ();
                Guid id;
                Guid.TryParse (s, out id).Should ().BeTrue ();
                // inject to repWorker who is waiting to proceed and prepare request
                repWorker.Request = new NetMQMessage ();
                repWorker.Request.Push (s);
                // get RequestEntry from queue and mark as processed
                var entry = io.GetRequestEntry (id);
                io.SaveProcessedRequestEntry (entry);
                // signal to proceed to send prepared request
                repWorker.waitHandle.Set ();

                // set up the fake client's request
                closeWorker.Request = new NetMQMessage ();
                closeWorker.Request.Push (id.ToString ());
                // signal to proceed to send prepared request
                closeWorker.waitHandle.Set ();

                // give it time to process
                Thread.Sleep (_sleep_for);

                // TEST COMMUNICATION
                closeWorker.Reply.FrameCount.Should ().Be (1, "because only one frame should have been returned.");
                closeWorker.Reply.First.ConvertToString ().Should ().Be ("Ok", "because 'Ok' should have been send.");

                // TEST QUEUE
                io.NumberOfRequests.Should ().Be (0);
                io.ExistsMessage (TitanicOperation.Request, id).Should ().BeFalse ();
                io.ExistsMessage (TitanicOperation.Reply, id).Should ().BeFalse ();
                io.ExistsMessage (TitanicOperation.Close, id).Should ().BeFalse ();
            }
        }
Example #2
0
        public void Run_RequestReply_ShouldWriteQueueAndReplyCorrect ()
        {
            var io = new TitanicMemoryIO ();

            using (var reqWorker = new FakeRequestMDPWorker ())
            using (var repWorker = new FakeReplyMDPWorker ())
            using (var closeWorker = new FakeCloseMDPWorker ())
            using (var dispatchClient = new FakeDispatchMDPClient ())
            using (var sut = new TitanicBroker (io))
            {
                // 1. Request
                // 2. Reply -> test
                reqWorker.Request = new NetMQMessage ();
                reqWorker.Request.Push ("Request Data");
                reqWorker.Request.Push ("echo");

                // start the process chain - worker & client should only run until they hit an AutoResetEvent
                Task.Factory.StartNew (() => sut.Run (reqWorker, repWorker, closeWorker, dispatchClient));

                // give system time to act
                Thread.Sleep (_sleep_for);

                // get reply id for further processing
                var s = reqWorker.Reply.Last.ConvertToString ();
                Guid id;
                Guid.TryParse (s, out id).Should ().BeTrue ();
                // inject to repWorker who is waiting to proceed and prepare request
                repWorker.Request = new NetMQMessage ();
                repWorker.Request.Push (s);
                // get RequestEntry from queue and mark as processed
                var entry = io.GetRequestEntry (id);
                io.SaveProcessedRequestEntry (entry);
                // signal to proceed to send prepared request
                repWorker.waitHandle.Set ();

                Thread.Sleep (_sleep_for);

                // TEST COMMUNICATION
                repWorker.Reply.FrameCount.Should ().Be (3, "because a 3 frame message is expected. ({0})", repWorker.Reply);
                repWorker.Reply.First.ConvertToString ().Should ().Be ("Ok");
                // should be identical since it is an "echo" service we are simulating :-)
                repWorker.Reply.Should ().Equal (entry.Request);
                // TEST QUEUE
                io.NumberOfRequests.Should ().Be (1);
                io.GetRequestEntries (e => e.RequestId == id).Count ().Should ().Be (1);
                var queueEntry = io.GetRequestEntry (id);
                queueEntry.State.Should ().Be (RequestEntry.Is_Processed);
                io.ExistsMessage (TitanicOperation.Close, id).Should ().BeFalse ();

            }
        }
Example #3
0
        public void Run_RequestProcessStandardFlow_ShouldWriteQueueAndReplyCorrect ()
        {
            var io = new TitanicMemoryIO ();

            using (var reqWorker = new FakeRequestMDPWorker ())
            using (var repWorker = new FakeReplyMDPWorker ())
            using (var closeWorker = new FakeCloseMDPWorker ())
            using (var dispatchClient = new FakeDispatchMDPClient ())
            using (var sut = new TitanicBroker (io))
            {
                // request worker will receive two calls
                //      a) null -> initial call which is answered with a request [service][data]
                //      b) reply -> [Ok][Guid]
                // and TitanicRequest will also write the TitanicQueue -> add a request
                // Run will also write queue with different data
                reqWorker.Request = new NetMQMessage ();
                reqWorker.Request.Push ("Request Data");
                reqWorker.Request.Push ("echo");

                // start the process chain - worker & client should only run until they hit an AutoResetEvent
                Task.Factory.StartNew (() => sut.Run (reqWorker, repWorker, closeWorker, dispatchClient));

                // give system time to act
                Thread.Sleep (200);

                // TEST COMMUNICATION
                reqWorker.Reply.FrameCount.Should ().Be (2, "because it was {0}", reqWorker.Reply.ToString ());
                reqWorker.Reply.First.ConvertToString ().Should ().Be ("Ok");
                var s = reqWorker.Reply.Last.ConvertToString ();

                Guid id;
                Guid.TryParse (s, out id).Should ().BeTrue ();

                // TEST QUEUE (Run was sent the Guid and it kick started the Dispatch(!)
                io.NumberOfRequests.Should ().Be (1, "because only one request was received");
                var request = io.GetRequestEntry (id);
                request.Should ().NotBe (default (RequestEntry), "because the id for the request should allow the retrieval");
                request.RequestId.Should ().Be (id);
                request.State.Should ().Be (RequestEntry.Is_Pending, "because it has not yet been processed");
                request.Request.Should ().Equal (reqWorker.Request, "because {0} was sent.", reqWorker.Request);
            }
        }
Example #4
0
        public void TitanicReply_RequestNonExistingReplyUnknown_ShouldSentCorrectReply ()
        {
            var io = new TitanicMemoryIO ();

            using (var reqWorker = new FakeRequestMDPWorker ())
            using (var repWorker = new FakeReplyMDPWorker ())
            using (var closeWorker = new FakeCloseMDPWorker ())
            using (var dispatchClient = new FakeDispatchMDPClient ())
            using (var sut = new TitanicBroker (io))
            {
                // setup the queue with a request
                var guid = Guid.NewGuid ();
                // setup the fake replyWorker's request
                repWorker.Request = new NetMQMessage ();
                repWorker.Request.Push (guid.ToString ());
                // start the process chain - worker & client should only run until they hit an AutoResetEvent
                Task.Factory.StartNew (() => sut.Run (reqWorker, repWorker, closeWorker, dispatchClient));
                // signal worker to go ahead
                repWorker.waitHandle.Set ();
                // give everything some time to process
                Thread.Sleep (_sleep_for);

                // TEST COMMUNICATION
                repWorker.Reply.FrameCount.Should ().Be (1, "because a 1 frame message is expected. ({0})", repWorker.Reply);
                repWorker.Reply.First.ConvertToString ().Should ().Be ("Unknown");
                // TEST QUEUE
                io.ExistsMessage (TitanicOperation.Request, guid).Should ().BeFalse ();
                io.ExistsMessage (TitanicOperation.Reply, guid).Should ().BeFalse ();
                io.ExistsMessage (TitanicOperation.Close, guid).Should ().BeFalse ();
            }
        }
Example #5
0
        public void TitanicReply_RequestReplyOk_ShouldSentCorrectReply ()
        {
            var io = new TitanicMemoryIO ();

            using (var reqWorker = new FakeRequestMDPWorker ())
            using (var repWorker = new FakeReplyMDPWorker ())
            using (var closeWorker = new FakeCloseMDPWorker ())
            using (var dispatchClient = new FakeDispatchMDPClient ())
            using (var sut = new TitanicBroker (io))
            {
                // setup the queue with a request
                var guid = Guid.NewGuid ();
                io.SaveNewRequestEntry (guid, new NetMQMessage ());
                var entry = io.GetRequestEntry (guid);
                // message content is expected to be [service][reply]
                entry.Request.Push ("REPLY DATA");
                entry.Request.Push ("echo");
                io.SaveProcessedRequestEntry (entry);
                // setup the fake replyWorker's request
                repWorker.Request = new NetMQMessage ();
                repWorker.Request.Push (guid.ToString ());
                // start the process chain - worker & client should only run until they hit an AutoResetEvent
                Task.Factory.StartNew (() => sut.Run (reqWorker, repWorker, closeWorker, dispatchClient));
                // signal worker to go ahead
                repWorker.waitHandle.Set ();
                // give everything some time to process
                Thread.Sleep (_sleep_for);

                // TEST COMMUNICATION
                repWorker.Reply.FrameCount.Should ().Be (3, "because a 3 frame message is expected. ({0})", repWorker.Reply);
                repWorker.Reply.First.ConvertToString ().Should ().Be ("Ok");
                repWorker.Reply.Should ().Equal (entry.Request);
                // TEST QUEUE
                io.GetRequestEntries (e => e.RequestId == guid).Count ().Should ().Be (1);
                var queueEntry = io.GetRequestEntry (guid);
                queueEntry.State.Should ().Be (RequestEntry.Is_Processed);
                io.ExistsMessage (TitanicOperation.Close, guid).Should ().BeFalse ();
            }
        }
Example #6
0
        public void TitanicClose_RequestToCloseRequest_ShouldDeleteRequestFromQueue ()
        {
            var io = new TitanicMemoryIO ();

            using (var reqWorker = new FakeRequestMDPWorker ())
            using (var repWorker = new FakeReplyMDPWorker ())
            using (var closeWorker = new FakeCloseMDPWorker ())
            using (var dispatchClient = new FakeDispatchMDPClient ())
            using (var sut = new TitanicBroker (io))
            {
                // setup the queue with a request
                var guid = Guid.NewGuid ();
                // message content is of no importance here
                io.SaveNewRequestEntry (guid, new NetMQMessage ());
                var entry = io.GetRequestEntry (guid);
                io.SaveProcessedRequestEntry (entry);
                // set up the fake client's request
                closeWorker.Request = new NetMQMessage ();
                closeWorker.Request.Push (guid.ToString ());
                // start the TitanicBroker (Close will automatically start and 
                // wait for a signal to proceed with sending the setup request message
                // start the process chain - worker & client should only run until they hit an AutoResetEvent
                Task.Factory.StartNew (() => sut.Run (reqWorker, repWorker, closeWorker, dispatchClient));
                // signal closeWorker to go ahead
                closeWorker.waitHandle.Set ();
                // give everything some time to process
                Thread.Sleep (_sleep_for);

                // TEST COMMUNICATION
                closeWorker.Reply.FrameCount.Should ().Be (1, "because only one frame should have been returned.");
                closeWorker.Reply.First.ConvertToString ().Should ().Be ("Ok", "because 'Ok' should have been send.");

                // TEST QUEUE
                io.ExistsMessage (TitanicOperation.Request, guid).Should ().BeFalse ();
                io.ExistsMessage (TitanicOperation.Reply, guid).Should ().BeFalse ();
                io.ExistsMessage (TitanicOperation.Close, guid).Should ().BeFalse ();
            }
        }