static void Main(string[] args) { var verbose = args.Length > 0 && args[0] == "-v"; var cts = new CancellationTokenSource (); // trapping Ctrl+C as exit signal! Console.CancelKeyPress += (s, e) => { e.Cancel = true; cts.Cancel (); }; Console.WriteLine ("Starting Titanic Broker in {0} - mode.\n\n", verbose ? "verbose" : "silent"); using (var titanic = new TitanicBroker (new TitanicMemoryIO ())) { if (verbose) titanic.LogInfoReady += (s, e) => PrintMessage (e.Info); try { Task.Factory.StartNew (() => titanic.Run (), cts.Token).Wait (cts.Token); } catch (AggregateException e) { Console.WriteLine (e.Flatten ()); } finally { cts.Dispose (); } } }
public void ctor_WithStandardPath_ShouldCreateRootAndQueue () { using (ITitanicBroker sut = new TitanicBroker ()) { sut.Should ().NotBeNull (); var path = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, _titanic_directory); var queue = Path.Combine (path, _titanic_queue); File.Exists (queue).Should ().BeTrue (string.Format ("because {0} should NOT have been created!", queue)); // clean up File.Delete (queue); Directory.Delete (path); } }
public void Run_CheckStandardConfiguration_ShouldHaveCreatedInfrastructure () { using (ITitanicBroker sut = new TitanicBroker ()) using (var cts = new CancellationTokenSource ()) { var t = Task.Factory.StartNew (() => sut.Run (), cts.Token); t.Status.Should ().Be (TaskStatus.WaitingToRun, "because it should wait for a request!"); var path = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, _titanic_directory); var queue = Path.Combine (path, _titanic_queue); Directory.Exists (path).Should ().BeTrue (string.Format ("because {0} should have been created!", path)); File.Exists (queue).Should ().BeTrue (string.Format ("because {0} should have been created!", queue)); // clean up cts.Cancel (); File.Delete (queue); Directory.Delete (path); } }
public void ctor_WithNoneStandardPath_ShouldCreateRootAndQueueInIndicatedPath () { var path = Path.Combine (Path.GetTempPath (), _titanic_directory); // create root for Titanic - it expects the root to exist(!) Directory.CreateDirectory (path); var queue = Path.Combine (path, _titanic_queue); using (ITitanicBroker sut = new TitanicBroker (_titanic_broker_address, path)) { sut.Should ().NotBeNull (); try { File.Exists (queue).Should ().BeTrue (string.Format ("because {0} should have been created!", queue)); } finally { // clean up File.Delete (queue); Directory.Delete (path); } } }
public void Run_CheckNoneStandardConfiguration_ShouldHaveCreatedInfrastructure () { var path = Path.Combine (Path.GetTempPath (), _titanic_directory); // create root for Titanic - it expects the root to exist(!) Directory.CreateDirectory (path); var queue = Path.Combine (path, _titanic_queue); // create queue for Titanic - it expects the queue to exist(!) at that location var f = File.Create (queue); f.Dispose (); using (ITitanicBroker sut = new TitanicBroker (_titanic_broker_address, path)) using (var cts = new CancellationTokenSource ()) { var t = Task.Factory.StartNew (() => sut.Run (), cts.Token); t.Status.Should ().Be (TaskStatus.WaitingToRun, "because it should wait for a request!"); Directory.Exists (path).Should ().BeTrue (string.Format ("because {0} should have been created!", path)); File.Exists (queue).Should ().BeTrue (string.Format ("because {0} should have been created!", queue)); // clean up cts.Cancel (); File.Delete (queue); Directory.Delete (path); } }
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 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 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 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 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 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 Run_NoWorkers_ShouldSitAndWaitFor5Seconds () { using (var sut = new TitanicBroker (new TitanicMemoryIO ())) { Task.Factory.StartNew (() => sut.Run ()); Thread.Sleep (5000); } }