public void SaveRequestEntry_MultipleRequestEntry_ShouldUpdateTheQueue () { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveRequestEntry (new RequestEntry { RequestId = Guid.NewGuid () }); sut.NumberOfRequests.Should ().Be (10, "because we just added 10 Requests"); }
public void CloseRequest_NotExistingRequest_ShouldReturnWithOutChangingQueue() { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveNewRequestEntry (Guid.NewGuid ()); var id = Guid.NewGuid (); sut.CloseRequest (id); sut.NumberOfRequests.Should ().Be (10); }
public void SaveRequestEntry_ValidRequestEntry_ShouldUpdateTheQueue () { var sut = new TitanicMemoryIO (); var request = new NetMQMessage (); var id = Guid.NewGuid (); var entry = new RequestEntry { RequestId = id, Request = request }; sut.SaveRequestEntry (entry); sut.NumberOfRequests.Should ().Be (1, "because we just added one"); var result = sut.GetRequestEntry (id); result.RequestId.Should ().Be (id); }
public void SaveNewRequest_GuidOnly_ShouldUpdateQueue () { var sut = new TitanicMemoryIO (); var request = new NetMQMessage (); var id = Guid.NewGuid (); var entry = new RequestEntry { RequestId = id, Request = request }; sut.SaveRequestEntry (entry); var result = sut.GetRequestEntry (id); result.RequestId.Should ().Be (id); result.Request.ShouldBeEquivalentTo (request); result.Position.Should ().Be (-1); result.State.Should ().Be (RequestEntry.Is_Pending); }
public void SaveNewRequest_GuidAndRequest_ShouldUpdateQueue () { var sut = new TitanicMemoryIO (); var request = new NetMQMessage (); request.Push ("A Request"); var id = Guid.NewGuid (); var entry = new RequestEntry { RequestId = id, Request = request }; sut.SaveRequestEntry (entry); var result = sut.GetRequestEntry (id); sut.NumberOfRequests.Should ().Be (1, "because we just added one"); result.RequestId.Should ().Be (id); result.Request.ShouldBeEquivalentTo (request); result.Position.Should ().Be (-1); result.State.Should ().Be (RequestEntry.Is_Pending); }
public void ExistsMessage_ExistingMessageWrongState_ShouldUpdateCorrectRequestEntry() { const int id_to_retrieve = 7; var sut = new TitanicMemoryIO (); var ids = new Guid[10]; for (var i = 0; i < 10; i++) { ids[i] = Guid.NewGuid (); var request = new NetMQMessage (); request.Push (string.Format ("Request #{0}", i)); request.Push ("echo"); sut.SaveMessage (TitanicOperation.Request, ids[i], request); } sut.ExistsMessage (TitanicOperation.Reply, ids[id_to_retrieve]).Should ().BeFalse (); }
public void CloseRequest_ExistingEntries_ShouldAlterQueueAppropriate() { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveRequestEntry (i % 3 == 0 ? new RequestEntry { RequestId = Guid.NewGuid (), State = RequestEntry.Is_Closed } : new RequestEntry { RequestId = Guid.NewGuid () }); sut.NumberOfRequests.Should ().Be (10); var result = sut.GetRequestEntries (e => e.State == RequestEntry.Is_Closed); result.Count ().Should ().Be (4); foreach (var requestEntry in result) sut.CloseRequest (requestEntry.RequestId); sut.NumberOfRequests.Should ().Be (6); }
public void GetRequestEntries_ExistingEntriesNoResultPredicate_ShouldReturnDefaultSequence () { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveRequestEntry (i % 2 == 0 ? new RequestEntry { RequestId = Guid.NewGuid (), State = RequestEntry.Is_Processed } : new RequestEntry { RequestId = Guid.NewGuid () }); sut.NumberOfRequests.Should ().Be (10); var result = sut.GetRequestEntries (e => e.State == RequestEntry.Is_Closed); result.Should ().BeOfType (typeof (RequestEntry[])); result.Count ().Should ().Be (0); }
public void SaveNewRequest_MultipleRequestGuidOnly_ShouldUpdateQueue () { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveNewRequestEntry (Guid.NewGuid ()); sut.NumberOfRequests.Should ().Be (10); }
public void GetMessage_ExistingRequest_ShouldReturnCorrectMessage () { var sut = new TitanicMemoryIO (); var ids = new Guid[10]; var expected = new NetMQMessage (); expected.Push ("Request #3"); expected.Push ("echo"); for (var i = 0; i < 10; i++) { ids[i] = Guid.NewGuid (); var request = new NetMQMessage (); request.Push (string.Format ("Request #{0}", i)); request.Push ("echo"); sut.SaveNewRequestEntry (ids[i], request); } var result = sut.GetMessage (TitanicOperation.Request, ids[3]); result.Should ().BeEquivalentTo (expected); }
public void SaveMessage_UpdateExistingMessage_ShouldUpdateCorrectRequestEntry () { const int id_to_retrieve = 7; var sut = new TitanicMemoryIO (); var ids = new Guid[10]; for (var i = 0; i < 10; i++) { ids[i] = Guid.NewGuid (); var request = new NetMQMessage (); request.Push ($"Request #{i}"); request.Push ("echo"); sut.SaveMessage (TitanicOperation.Request, ids[i], request); } sut.NumberOfRequests.Should ().Be (10); var reply = new NetMQMessage (); reply.Push ($"This is a REPLY to Request #{id_to_retrieve}"); reply.Push ("echo"); sut.SaveMessage (TitanicOperation.Reply, ids[id_to_retrieve], reply).Should ().BeTrue (); sut.NumberOfRequests.Should ().Be (10); var result = sut.GetRequestEntry (ids[id_to_retrieve]); result.State.Should ().Be (RequestEntry.Is_Processed); result.RequestId.Should ().Be (ids[id_to_retrieve]); result.Request.Should ().Equal (reply); }
public void ExistsMessage_NotExistingMessage_ShouldUpdateCorrectRequestEntry () { var sut = new TitanicMemoryIO (); var ids = new Guid[10]; for (var i = 0; i < 10; i++) { ids[i] = Guid.NewGuid (); var request = new NetMQMessage (); request.Push ($"Request #{i}"); request.Push ("echo"); sut.SaveMessage (TitanicOperation.Request, ids[i], request); } sut.ExistsMessage (TitanicOperation.Request, Guid.NewGuid ()).Should ().BeFalse (); }
public void SaveProcessedRequest_ExistingRequestWithRequestData_ShouldUpdateEntryAppropriate () { var sut = new TitanicMemoryIO (); var request = new NetMQMessage (); request.Push ("Processed Request Data"); var id = Guid.NewGuid (); var entry = new RequestEntry { RequestId = id, Request = request }; sut.SaveRequestEntry (entry); sut.SaveProcessedRequestEntry (entry); var result = sut.GetRequestEntry (id); result.RequestId.Should ().Be (id); result.Request.ShouldBeEquivalentTo (request); result.Position.Should ().Be (-1); result.State.Should ().Be (RequestEntry.Is_Processed); }
public void SaveMessage_NewMessage_ShouldUpdateQueue () { var sut = new TitanicMemoryIO (); var id = Guid.NewGuid (); var request = new NetMQMessage (); request.Push ("Request #1"); request.Push ("echo"); sut.SaveMessage (TitanicOperation.Request, id, request).Should ().BeTrue (); sut.NumberOfRequests.Should ().Be (1); var result = sut.GetRequestEntry (id); result.Request.Should ().Equal (request); result.State.Should ().Be (RequestEntry.Is_Pending); }
public void GetRequestEntry_NotExistingEntry_ShouldReturnDefaultObject () { var sut = new TitanicMemoryIO (); sut.GetRequestEntry (Guid.NewGuid ()).Should ().Be (default (RequestEntry)); }
public void GetMessage_ExistingRequestWrongState_ShouldReturnCorrectMessage () { var sut = new TitanicMemoryIO (); var ids = new Guid[10]; var expected = new NetMQMessage (); for (var i = 0; i < 10; i++) { ids[i] = Guid.NewGuid (); var request = new NetMQMessage (); request.Push ($"Request #{i}"); request.Push ("echo"); sut.SaveNewRequestEntry (ids[i], request); } var result = sut.GetMessage (TitanicOperation.Reply, Guid.NewGuid ()); result.Should ().BeEquivalentTo (expected); }
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 (); } }
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 (); } }
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 (); } }
public void GetRequestEntries_ExistingEntriesCorrectPredicate_ShouldReturnCorrectSequence () { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveRequestEntry (i % 2 == 0 ? new RequestEntry { RequestId = Guid.NewGuid (), State = RequestEntry.Is_Processed } : new RequestEntry { RequestId = Guid.NewGuid () }); var result = sut.GetRequestEntries (e => e.State == RequestEntry.Is_Processed); result.Should ().BeOfType (typeof (RequestEntry[])); result.Count ().Should ().Be (5); result.All (e => e.State == RequestEntry.Is_Processed).Should ().BeTrue (); }
public void GetRequestEntries_NotExistingEntries_ShouldReturnDefaultObject () { var sut = new TitanicMemoryIO (); var id = Guid.NewGuid (); sut.GetRequestEntries (e => e.RequestId == id).ShouldAllBeEquivalentTo (default (IEnumerable<RequestEntry>)); }
public void ctor_CreateObject_ShouldReturnNotNull () { var sut = new TitanicMemoryIO (); sut.Should ().NotBeNull (); }
public void GetRequestEntry_ExistingEntry_ShouldReturnDefaultObject () { var sut = new TitanicMemoryIO (); var id = Guid.NewGuid (); sut.SaveRequestEntry (new RequestEntry { RequestId = id }); var result = sut.GetRequestEntry (id); result.RequestId.Should ().Be (id); result.State.Should ().Be (RequestEntry.Is_Pending); result.Position.Should ().Be (-1); result.Request.Should ().BeNull (); }
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 (); } }
public void GetNotClosedEntries_ExistingEntriesNoResult_ShouldReturnCorrectSequence () { var sut = new TitanicMemoryIO (); for (var i = 0; i < 10; i++) sut.SaveRequestEntry (new RequestEntry { RequestId = Guid.NewGuid (), State = RequestEntry.Is_Closed }); sut.NumberOfRequests.Should ().Be (10); var result = sut.GetNotClosedRequestEntries (); result.Count ().Should ().Be (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); } }
public void SaveRequestEntry_InvalidParameter_ShouldThrowArgumentNullException () { var sut = new TitanicMemoryIO (); sut.Invoking (o => o.SaveRequestEntry (null)).ShouldThrow<ArgumentNullException> (); }
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 (); } }
public void CloseRequest_NoRequestExisting_ShouldReturn () { var sut = new TitanicMemoryIO (); sut.CloseRequest (Guid.NewGuid ()); }