public void Can_Serialize_Using_Protobuf_With_Class()
		{
			Console.WriteLine(TestContext.CurrentContext.Test.Name);

			CountdownEvent cd = new CountdownEvent(5);
			{
				int freePort = TcpPortFree();

				var pubSub = new SubjectNetMQ<MyMessageClassType1>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
				pubSub.Subscribe(o =>
				{
					Assert.IsTrue(o.Name == "Bob");
					Console.Write("Test: Num={0}, Name={1}\n", o.Num, o.Name);
					cd.Signal();
				},
					ex => { Console.WriteLine("Exception! {0}", ex.Message); });

				pubSub.OnNext(new MyMessageClassType1(38, "Bob"));
				pubSub.OnNext(new MyMessageClassType1(39, "Bob"));
				pubSub.OnNext(new MyMessageClassType1(40, "Bob"));
				pubSub.OnNext(new MyMessageClassType1(41, "Bob"));
				pubSub.OnNext(new MyMessageClassType1(42, "Bob"));
			}

			if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
        public void If_Message_Not_Serializable_By_Protobuf_Throw_A_Meaningful_Error()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            var freePort = NUnitUtils.TcpPortFree();
            var pubSub = new SubjectNetMQ<MessageNotSerializableByProtobuf>("tcp://127.0.0.1:" + freePort,
                loggerDelegate: Console.Write);
            try
            {
                pubSub.OnNext(new MessageNotSerializableByProtobuf());

                // We should have thrown an exception if the class was not serializable by ProtoBuf-Net.
                Assert.Fail();
            }
            catch (InvalidOperationException ex)
            {
                Assert.True(ex.Message.ToLower().Contains("protobuf"));
                Console.Write("Pass - meaningful message thrown if class was not serializable by ProtoBuf-Net.");
            }
            catch (Exception)
            {
                Assert.Fail();
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Simplest_Test_Subject()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);

            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Console.Write("Test 1: {0}\n", o);
                    cd.Signal();
                },
                                 ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(38);
                pubSub.OnNext(39);
                pubSub.OnNext(40);
                pubSub.OnNext(41);
                pubSub.OnNext(42);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
Exemple #4
0
        public void OnException_Should_Get_Passed_To_Subscribers()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var weAreDone = new CountdownEvent(1);

            {
                var freePort = NUnitUtils.TcpPortFree();
                var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(
                    o =>
                {
                    // If this gets called more than max times, it will throw an exception as it is going through 0.
                    Assert.Fail();
                },
                    ex =>
                {
                    Console.Write("Exception: {0}", ex.Message);
                    Assert.True(ex.Message.Contains("passed"));
                    weAreDone.Signal();
                },
                    () => { Assert.Fail(); });

                pubSub.OnError(new Exception("passed"));
            }

            if (weAreDone.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void OnCompleted_Should_Get_Passed_To_Subscribers()
        {
            CountdownEvent weAreDone = new CountdownEvent(1);

            {
                int freePort = TcpPortFree();
                var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort);
                pubSub.Subscribe(
                    o =>
                {
                    // If this gets called more than max times, it will throw an exception as it is going through 0.
                    Console.Write("FAIL!");
                    Assert.Fail();
                },
                    ex =>
                {
                    Console.Write("FAIL!");
                    Assert.Fail();
                },
                    () =>
                {
                    Console.Write("Pass!");
                    weAreDone.Signal();
                });

                pubSub.OnCompleted();
            }
            if (weAreDone.Wait(TimeSpan.FromSeconds(10)) == false)             // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in 10 seconds.");
            }
        }
        public void Can_Serialize_Class_Name_Longer_Then_Thirty_Two_Characters()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);
            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub =
                    new SubjectNetMQ<ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure>(
                        "tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd.Signal();
                },
                    ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(38, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(39, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(40, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(41, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(42, "Bob"));
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		public void Simplest_Test()
		{
			Console.WriteLine(TestContext.CurrentContext.Test.Name);

			CountdownEvent cd = new CountdownEvent(5);
			{
				int freePort = TcpPortFree();

				var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort);
				pubSub.Subscribe(o =>
					{
						Console.Write("Test 1: {0}\n", o);
						cd.Signal();
					},
					ex =>
					{
						Console.WriteLine("Exception! {0}", ex.Message);
					});

				pubSub.OnNext(38);
				pubSub.OnNext(39);
				pubSub.OnNext(40);
				pubSub.OnNext(41);
				pubSub.OnNext(42);
			}

			if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
        public void Simplest_Fanout_Sub()
        {
            CountdownEvent cd = new CountdownEvent(3);

            {
                int freePort = TcpPortFree();
                var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort);
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub1: {0}\n", o);
                    cd.Signal();
                });
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub2: {0}\n", o);
                    cd.Signal();
                });
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub3: {0}\n", o);
                    cd.Signal();
                });

                pubSub.OnNext(42);
            }

            if (cd.Wait(TimeSpan.FromSeconds(10)) == false)             // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in 10 seconds.");
            }
        }
        public static void Disposing_Of_One_Does_Not_Dispose_Of_The_Other()
        {
            Console.WriteLine("Disposing of one subscriber should not dispose of the other.");

            int            max = 1000;
            CountdownEvent cd  = new CountdownEvent(max);

            {
                int freePort = TcpPortFree();
                var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort);
                var d1       = pubSub.Subscribe(o =>
                {
                    cd.Signal();
                });

                var d2 = pubSub.Subscribe(o =>
                {
                    Assert.Fail();
                },
                                          ex =>
                {
                    Console.WriteLine("Exception in subscriber thread.");
                });
                d2.Dispose();

                for (int i = 0; i < max; i++)
                {
                    pubSub.OnNext(i);
                }
            }
            if (cd.Wait(TimeSpan.FromSeconds(10)) == false)             // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in 10 seconds.");
            }
        }
        public void Can_Serialize_Using_Protobuf_With_Struct()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);

            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub = new SubjectNetMQ <MyMessageStructType1>("tcp://127.0.0.1:" + freePort,
                                                                     loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd.Signal();
                },
                                 ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(new MyMessageStructType1(38, "Bob"));
                pubSub.OnNext(new MyMessageStructType1(39, "Bob"));
                pubSub.OnNext(new MyMessageStructType1(40, "Bob"));
                pubSub.OnNext(new MyMessageStructType1(41, "Bob"));
                pubSub.OnNext(new MyMessageStructType1(42, "Bob"));
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void If_Message_Not_Serializable_By_Protobuf_Throw_A_Meaningful_Error()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            var freePort = NUnitUtils.TcpPortFree();
            var pubSub   = new SubjectNetMQ <MessageNotSerializableByProtobuf>("tcp://127.0.0.1:" + freePort,
                                                                               loggerDelegate: Console.Write);

            try
            {
                pubSub.OnNext(new MessageNotSerializableByProtobuf());

                // We should have thrown an exception if the class was not serializable by ProtoBuf-Net.
                Assert.Fail();
            }
            catch (InvalidOperationException ex)
            {
                Assert.True(ex.Message.ToLower().Contains("protobuf"));
                Console.Write("Pass - meaningful message thrown if class was not serializable by ProtoBuf-Net.");
            }
            catch (Exception)
            {
                Assert.Fail();
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Simplest_Test()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);

            CountdownEvent cd = new CountdownEvent(5);

            {
                int freePort = TcpPortFree();

                var pubSub = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort);
                pubSub.Subscribe(o =>
                {
                    Console.Write("Test 1: {0}\n", o);
                    cd.Signal();
                },
                                 ex =>
                {
                    Console.WriteLine("Exception! {0}", ex.Message);
                });

                pubSub.OnNext(38);
                pubSub.OnNext(39);
                pubSub.OnNext(40);
                pubSub.OnNext(41);
                pubSub.OnNext(42);
            }

            if (cd.Wait(TimeSpan.FromSeconds(10)) == false)             // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in 10 seconds.");
            }
        }
		static void Main(string[] args)
		{
			Console.Write("Reactive Extensions publisher demo:\n");

			string endPoint = "tcp://127.0.0.1:56001";
			if (args.Length >= 1)
			{
				endPoint = args[0];
			}

			Console.Write("Endpoint: {0}\n", endPoint);

			var subject = new SubjectNetMQ<MyMessage>(endPoint, loggerDelegate: msg => Console.Write(msg));

			// Debug: subscribe to ourself. If you run the "SampleSubscriber" project now, you will see the same
            // messages appearing in that subscriber too.
			subject.Subscribe(message =>
			{
				Console.Write("Received: {0}, '{1}'.\n", message.Num, message.Name);
			});

			int i = 0;
			while (true)
			{
				var message = new MyMessage(i, "Bob");

				// When we call "OnNext", it binds a publisher to this endpoint endpoint.
				subject.OnNext(message);

				Console.Write("Published: {0}, '{1}'.\n", message.Num, message.Name);
				Thread.Sleep(TimeSpan.FromMilliseconds(1000));
				i++;
			}
			// NOTE: If you run the "SampleSubscriber" project now, you will see the same messages appearing in the subscriber.
		}
        public static void Disposing_Of_One_Does_Not_Dispose_Of_The_Other()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            var max = 1000;
            var cd  = new CountdownEvent(max);

            {
                var freePort = NUnitUtils.TcpPortFree();
                var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                var d1       = pubSub.Subscribe(o => { cd.Signal(); });

                var d2 = pubSub.Subscribe(o => { Assert.Fail(); },
                                          ex => { Console.WriteLine("Exception in subscriber thread."); });
                d2.Dispose();

                for (var i = 0; i < max; i++)
                {
                    pubSub.OnNext(i);
                }
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Send_Two_Types_Simultaneously_Over_Same_Transport()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            var cd1 = new CountdownEvent(5);
            var cd2 = new CountdownEvent(5);

            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub1 = new SubjectNetMQ <MyMessageStructType1>("tcp://127.0.0.1:" + freePort,
                                                                      loggerDelegate: Console.Write);
                var pubSub2 = new SubjectNetMQ <MyMessageStructType2>("tcp://127.0.0.1:" + freePort,
                                                                      loggerDelegate: Console.Write);
                pubSub1.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test 1: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd1.Signal();
                },
                                  ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub2.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test 2: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd2.Signal();
                },
                                  ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub1.OnNext(new MyMessageStructType1(38, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(39, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(40, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(41, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(42, "Bob"));

                pubSub2.OnNext(new MyMessageStructType2(38, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(39, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(40, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(41, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(42, "Bob"));
            }

            if (cd1.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            if (cd2.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Can_Serialize_Using_Protobuf_With_Class()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            List<TimeSpan> timeMilliseconds = new List<TimeSpan>();

            var cd = new CountdownEvent(5);
            {
                var freePort = NUnitUtils.TcpPortFree();

                timeMilliseconds.Add(sw.Elapsed);
                var pubSub = new SubjectNetMQ<MyMessageClassType1>("tcp://127.0.0.1:" + freePort,
                    loggerDelegate: Console.Write);
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.Subscribe(
                    o =>
                    {
                        Assert.IsTrue(o.Name == "Bob");
                        Console.Write("Test: Num={0}, Name={1}\n", o.Num, o.Name);
                        cd.Signal();
                    },
                    ex =>
                    {
                        Console.WriteLine("Exception! {0}", ex.Message);                         
                    });
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnNext(new MyMessageClassType1(38, "Bob"));
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnNext(new MyMessageClassType1(39, "Bob"));
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnNext(new MyMessageClassType1(40, "Bob"));
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnNext(new MyMessageClassType1(41, "Bob"));
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnNext(new MyMessageClassType1(42, "Bob"));
                timeMilliseconds.Add(sw.Elapsed);
            }

            for (int i = 0; i < timeMilliseconds.Count; i++)
            {
                var t = timeMilliseconds[i];
                Console.WriteLine("- Stage {0}: {1:0,000} milliseconds", i, t.TotalMilliseconds);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		static void Main(string[] args)
		{
			Console.Write("Reactive Extensions subscriber demo:\n");

			SubjectNetMQ<MyMessage> subject = new SubjectNetMQ<MyMessage>("tcp://127.0.0.1:56001");
			subject.Subscribe(message =>
			{
				Console.Write("Received: {0}, '{1}'.\n", message.Num, message.Name);
			});

			Console.WriteLine("[waiting for publisher - any key to exit]");
			Console.ReadKey();
		}
        public void OnCompleted_Should_Get_Passed_To_Subscribers()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            List <TimeSpan> timeMilliseconds = new List <TimeSpan>();

            var weAreDone = new CountdownEvent(1);

            {
                timeMilliseconds.Add(sw.Elapsed);
                var freePort = NUnitUtils.TcpPortFree();
                timeMilliseconds.Add(sw.Elapsed);
                var pubSub = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.Subscribe(
                    o =>
                {
                    // If this gets called more than max times, it will throw an exception as it is going through 0.
                    //Console.Write("FAIL!");
                    //Assert.Fail();
                },
                    ex =>
                {
                    Console.Write("FAIL!");
                    Assert.Fail();
                },
                    () =>
                {
                    Console.Write("Pass!");
                    weAreDone.Signal();
                });
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnCompleted();
                timeMilliseconds.Add(sw.Elapsed);
            }

            for (int i = 0; i < timeMilliseconds.Count; i++)
            {
                var t = timeMilliseconds[i];
                Console.WriteLine("- Stage {0}: {1:0,000} milliseconds", i, t.TotalMilliseconds);
            }

            if (weAreDone.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        static void Main(string[] args)
        {
            Console.Write("Reactive Extensions subscriber demo:\n");

            SubjectNetMQ <MyMessage> subject = new SubjectNetMQ <MyMessage>("tcp://127.0.0.1:56001");

            subject.Subscribe(message =>
            {
                Console.Write("Received: {0}, '{1}'.\n", message.Num, message.Name);
            });

            Console.WriteLine("[waiting for publisher - any key to exit]");
            Console.ReadKey();
        }
Exemple #20
0
		static void Main(string[] args)
		{
			Console.Write("Reactive Extensions publisher demo:\n");

			SubjectNetMQ<MyMessage> subject = new SubjectNetMQ<MyMessage>("tcp://127.0.0.1:56001");

			int i = 0;
			while (true)
			{
				var message = new MyMessage(i, "Bob");
				subject.OnNext(message);

				Console.Write("Published: {0}, '{1}'.\n", message.Num, message.Name);
				Thread.Sleep(TimeSpan.FromMilliseconds(1000));
				i++;
			}
		}
        public static void Speed_Test_Subject()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();
            {
                var max = 100 * 1000;

                var cd          = new CountdownEvent(max);
                var receivedNum = 0;
                {
                    Console.Write("Speed test with {0} messages:\n", max);

                    var freePort = NUnitUtils.TcpPortFree();
                    var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);

                    pubSub.Subscribe(i =>
                    {
                        receivedNum++;
                        cd.Signal();
                        if (i % 10000 == 0)
                        {
                            //Console.Write("*");
                        }
                    });

                    sw.Start();
                    for (var i = 0; i < max; i++)
                    {
                        pubSub.OnNext(i);
                    }
                }

                if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
                {
                    Assert.Fail("\nTimed out, this test should complete in {0} seconds. receivedNum={1}",
                                GlobalTimeout.Timeout.TotalSeconds, receivedNum);
                }

                // On my machine, achieved >120,000 messages per second.
                NUnitUtils.PrintElapsedTime(sw.Elapsed, max);
            }
        }
		static void Main(string[] args)
		{
			Console.Write("Reactive Extensions subscriber demo:\n");

			string endPoint = "tcp://127.0.0.1:56001";
			if (args.Length >= 1)
			{
				endPoint = args[0];
			}

			Console.Write("Endpoint: {0}\n", endPoint);

			SubjectNetMQ<MyMessage> subject = new SubjectNetMQ<MyMessage>(endPoint, loggerDelegate: msg => Console.Write(msg));
			subject.Subscribe(message =>
			{
				Console.Write("Received: {0}, '{1}'.\n", message.Num, message.Name);
			});

			Console.WriteLine("[waiting for publisher - any key to exit]");
			Console.ReadKey();
		}
        public static void Speed_Test()
        {
            Stopwatch sw = new Stopwatch();
            {
                var max = 200 * 1000;

                CountdownEvent cd          = new CountdownEvent(max);
                var            receivedNum = 0;
                {
                    Console.Write("Speed test with {0} messages:\n", max);

                    int freePort = TcpPortFree();
                    var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort);

                    pubSub.Subscribe(i =>
                    {
                        receivedNum++;
                        cd.Signal();
                        if (i % 10000 == 0)
                        {
                            //Console.Write("*");
                        }
                    });

                    sw.Start();
                    for (int i = 0; i < max; i++)
                    {
                        pubSub.OnNext(i);
                    }
                }
                if (cd.Wait(TimeSpan.FromSeconds(15)) == false)                 // Blocks until _countdown.Signal has been called.
                {
                    Assert.Fail("\nTimed out, this test should complete in 10 seconds. receivedNum={0}", receivedNum);
                }

                sw.Stop();
                Console.Write("\nElapsed time: {0} milliseconds ({1:0,000}/sec)\n", sw.ElapsedMilliseconds, (double)max / (double)sw.Elapsed.TotalSeconds);
                // On my machine, achieved >120,000 messages per second.
            }
        }
        public void Simplest_Fanout_Sub()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(3);

            {
                var freePort = NUnitUtils.TcpPortFree();
                var pubSub   = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub1: {0}\n", o);
                    cd.Signal();
                });
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub2: {0}\n", o);
                    cd.Signal();
                });
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub3: {0}\n", o);
                    cd.Signal();
                });

                pubSub.OnNext(42);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Initialize_Publisher_Then_Subscriber()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);

            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);

                // Forces the publisher to be initialized. Subscriber not set up yet, so this message will never get
                // delivered to the subscriber, which is what is should do.
                pubSub.OnNext(1);

                pubSub.Subscribe(o =>
                {
                    Assert.IsTrue(o != 1);
                    Console.Write("Test 1: {0}\n", o);
                    cd.Signal();
                },
                                 ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(38);
                pubSub.OnNext(39);
                pubSub.OnNext(40);
                pubSub.OnNext(41);
                pubSub.OnNext(42);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Initialize_Publisher_Then_Subscriber()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);

            CountdownEvent cd = new CountdownEvent(5);

            {
                int freePort = TcpPortFree();

                var pubSub = new SubjectNetMQ <int>("tcp://127.0.0.1:" + freePort);

                // Forces the publisher to be initialized. Subscriber not set up yet, so this message will never get
                // delivered to the subscriber, which is what is should do.
                pubSub.OnNext(1);

                pubSub.Subscribe(o =>
                {
                    Console.Write("Test 1: {0}\n", o);
                    cd.Signal();
                },
                                 ex =>
                {
                    Console.WriteLine("Exception! {0}", ex.Message);
                });

                pubSub.OnNext(38);
                pubSub.OnNext(39);
                pubSub.OnNext(40);
                pubSub.OnNext(41);
                pubSub.OnNext(42);
            }

            if (cd.Wait(TimeSpan.FromSeconds(10)) == false)             // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in 10 seconds.");
            }
        }
		public void Initialize_Publisher_Then_Subscriber()
		{
			Console.WriteLine(TestContext.CurrentContext.Test.Name);

			CountdownEvent cd = new CountdownEvent(5);
			{
				int freePort = TcpPortFree();

				var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort);

				// Forces the publisher to be initialized. Subscriber not set up yet, so this message will never get
                // delivered to the subscriber, which is what is should do.
				pubSub.OnNext(1); 

				pubSub.Subscribe(o =>
				{
					Console.Write("Test 1: {0}\n", o);
					cd.Signal();
				},
					ex =>
					{
						Console.WriteLine("Exception! {0}", ex.Message);
					});

				pubSub.OnNext(38);
				pubSub.OnNext(39);
				pubSub.OnNext(40);
				pubSub.OnNext(41);
				pubSub.OnNext(42);
			}

			if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
        public void Can_Serialize_Class_Name_Longer_Then_Thirty_Two_Characters()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);

            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub =
                    new SubjectNetMQ <ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure>(
                        "tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd.Signal();
                },
                                 ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(38, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(39, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(40, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(41, "Bob"));
                pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(42, "Bob"));
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		public static void Speed_Test()
		{
			Stopwatch sw = new Stopwatch();
			{
				var max = 200 * 1000;

				CountdownEvent cd = new CountdownEvent(max);
				var receivedNum = 0;
				{

					Console.Write("Speed test with {0} messages:\n", max);

					int freePort = TcpPortFree();
					var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort);

					pubSub.Subscribe(i =>
					{
						receivedNum++;
						cd.Signal();
						if (i % 10000 == 0)
						{
							//Console.Write("*");
						}
					});

					sw.Start();
					for (int i = 0; i < max; i++)
					{
						pubSub.OnNext(i);
					}
				}
				if (cd.Wait(TimeSpan.FromSeconds(15)) == false) // Blocks until _countdown.Signal has been called.
				{
					Assert.Fail("\nTimed out, this test should complete in 10 seconds. receivedNum={0}", receivedNum);
				}

				sw.Stop();
				Console.Write("\nElapsed time: {0} milliseconds ({1:0,000}/sec)\n", sw.ElapsedMilliseconds, (double)max / (double)sw.Elapsed.TotalSeconds);
				// On my machine, achieved >120,000 messages per second.
			}
		}
        public static void Speed_Test_Subject()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();
            {
                var max = 100*1000;

                var cd = new CountdownEvent(max);
                var receivedNum = 0;
                {
                    Console.Write("Speed test with {0} messages:\n", max);

                    var freePort = NUnitUtils.TcpPortFree();
                    var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);

                    pubSub.Subscribe(i =>
                    {
                        receivedNum++;
                        cd.Signal();
                        if (i%10000 == 0)
                        {
                            //Console.Write("*");
                        }
                    });

                    sw.Start();
                    for (var i = 0; i < max; i++)
                    {
                        pubSub.OnNext(i);
                    }
                }

                if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
                {
                    Assert.Fail("\nTimed out, this test should complete in {0} seconds. receivedNum={1}",
                        GlobalTimeout.Timeout.TotalSeconds, receivedNum);
                }

                // On my machine, achieved >120,000 messages per second.
                NUnitUtils.PrintElapsedTime(sw.Elapsed, max);
            }
        }
        public void OnCompleted_Should_Get_Passed_To_Subscribers()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            List<TimeSpan> timeMilliseconds = new List<TimeSpan>();

            var weAreDone = new CountdownEvent(1);
            {
                timeMilliseconds.Add(sw.Elapsed);
                var freePort = NUnitUtils.TcpPortFree();
                timeMilliseconds.Add(sw.Elapsed);
                var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.Subscribe(
                    o =>
                    {
                        // If this gets called more than max times, it will throw an exception as it is going through 0.
                        //Console.Write("FAIL!");
                        //Assert.Fail();
                    },
                    ex =>
                    {
                        Console.Write("FAIL!");
                        Assert.Fail();
                    },
                    () =>
                    {
                        Console.Write("Pass!");
                        weAreDone.Signal();
                    });
                timeMilliseconds.Add(sw.Elapsed);
                pubSub.OnCompleted();
                timeMilliseconds.Add(sw.Elapsed);
            }

            for (int i = 0; i < timeMilliseconds.Count; i++)
            {
                var t = timeMilliseconds[i];
                Console.WriteLine("- Stage {0}: {1:0,000} milliseconds", i, t.TotalMilliseconds);
            }

            if (weAreDone.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Send_Two_Types_Simultaneously_Over_Same_Transport()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            var cd1 = new CountdownEvent(5);
            var cd2 = new CountdownEvent(5);
            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub1 = new SubjectNetMQ<MyMessageStructType1>("tcp://127.0.0.1:" + freePort,
                    loggerDelegate: Console.Write);
                var pubSub2 = new SubjectNetMQ<MyMessageStructType2>("tcp://127.0.0.1:" + freePort,
                    loggerDelegate: Console.Write);
                pubSub1.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test 1: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd1.Signal();
                },
                    ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub2.Subscribe(o =>
                {
                    Assert.IsTrue(o.Name == "Bob");
                    Console.Write("Test 2: Num={0}, Name={1}\n", o.Num, o.Name);
                    cd2.Signal();
                },
                    ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub1.OnNext(new MyMessageStructType1(38, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(39, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(40, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(41, "Bob"));
                pubSub1.OnNext(new MyMessageStructType1(42, "Bob"));

                pubSub2.OnNext(new MyMessageStructType2(38, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(39, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(40, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(41, "Bob"));
                pubSub2.OnNext(new MyMessageStructType2(42, "Bob"));
            }

            if (cd1.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            if (cd2.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		public void Simplest_Fanout_Sub()
		{
			CountdownEvent cd = new CountdownEvent(3);
			{
				int freePort = TcpPortFree();
				var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort);
				pubSub.Subscribe(o =>
				{
					Assert.AreEqual(o, 42);
					Console.Write("PubTwoThreadFanoutSub1: {0}\n", o);
					cd.Signal();
				});
				pubSub.Subscribe(o =>
				{
					Assert.AreEqual(o, 42);
					Console.Write("PubTwoThreadFanoutSub2: {0}\n", o);
					cd.Signal();
				});
				pubSub.Subscribe(o =>
				{
					Assert.AreEqual(o, 42);
					Console.Write("PubTwoThreadFanoutSub3: {0}\n", o);
					cd.Signal();
				});

				pubSub.OnNext(42);
			}

			if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
        public void Initialize_Publisher_Then_Subscriber()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);
            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);

                // Forces the publisher to be initialized. Subscriber not set up yet, so this message will never get
                // delivered to the subscriber, which is what is should do.
                pubSub.OnNext(1);

                pubSub.Subscribe(o =>
                {
                    Assert.IsTrue(o != 1);
                    Console.Write("Test 1: {0}\n", o);
                    cd.Signal();
                },
                    ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(38);
                pubSub.OnNext(39);
                pubSub.OnNext(40);
                pubSub.OnNext(41);
                pubSub.OnNext(42);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		public void OnException_Should_Get_Passed_To_Subscribers()
		{
			CountdownEvent weAreDone = new CountdownEvent(1);
			{
				int freePort = TcpPortFree();
				var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
				pubSub.Subscribe(
					o =>
					{
						// If this gets called more than max times, it will throw an exception as it is going through 0.
						Assert.Fail();
					},
					ex =>
					{
						Console.Write("Exception: {0}", ex.Message);
						Assert.True(ex.Message.Contains("passed"));
						weAreDone.Signal();
					},
					() => { Assert.Fail(); });

				pubSub.OnError(new Exception("passed"));
			}
			if (weAreDone.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
		public void Send_Two_Types_Simultaneously_Over_Same_Transport()
		{
			Console.WriteLine(TestContext.CurrentContext.Test.Name);

			CountdownEvent cd1 = new CountdownEvent(5);
			CountdownEvent cd2 = new CountdownEvent(5);
			{
				int freePort = TcpPortFree();

				var pubSub1 = new SubjectNetMQ<MyMessageStructType1>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
				var pubSub2 = new SubjectNetMQ<MyMessageStructType2>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
				pubSub1.Subscribe(o =>
					{
						Assert.IsTrue(o.Name == "Bob");
						Console.Write("Test 1: Num={0}, Name={1}\n", o.Num, o.Name);
						cd1.Signal();
					},
					ex =>
					{
						Console.WriteLine("Exception! {0}", ex.Message);
					});

				pubSub2.Subscribe(o =>
					{
						Assert.IsTrue(o.Name == "Bob");
						Console.Write("Test 2: Num={0}, Name={1}\n", o.Num, o.Name);
						cd2.Signal();
					},
					ex =>
					{
						Console.WriteLine("Exception! {0}", ex.Message);
					});

				pubSub1.OnNext(new MyMessageStructType1(38, "Bob"));
				pubSub1.OnNext(new MyMessageStructType1(39, "Bob"));
				pubSub1.OnNext(new MyMessageStructType1(40, "Bob"));
				pubSub1.OnNext(new MyMessageStructType1(41, "Bob"));
				pubSub1.OnNext(new MyMessageStructType1(42, "Bob"));

				pubSub2.OnNext(new MyMessageStructType2(38, "Bob"));
				pubSub2.OnNext(new MyMessageStructType2(39, "Bob"));
				pubSub2.OnNext(new MyMessageStructType2(40, "Bob"));
				pubSub2.OnNext(new MyMessageStructType2(41, "Bob"));
				pubSub2.OnNext(new MyMessageStructType2(42, "Bob"));
			}

			if (cd1.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}

			if (cd2.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
		public static void Disposing_Of_One_Does_Not_Dispose_Of_The_Other()
		{
			Console.WriteLine("Disposing of one subscriber should not dispose of the other.");

			int max = 1000;
			CountdownEvent cd = new CountdownEvent(max);
			{
				int freePort = TcpPortFree();
				var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort);
				var d1 = pubSub.Subscribe(o =>
					{
						cd.Signal();
					});

				var d2 = pubSub.Subscribe(o =>
					{
						Assert.Fail();
					},
					ex =>
					{
						Console.WriteLine("Exception in subscriber thread.");
					});
				d2.Dispose();

				for (int i = 0; i < max; i++)
				{
					pubSub.OnNext(i);
				}
			}
			if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}
        public static void Disposing_Of_One_Does_Not_Dispose_Of_The_Other()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            var max = 1000;
            var cd = new CountdownEvent(max);
            {
                var freePort = NUnitUtils.TcpPortFree();
                var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                var d1 = pubSub.Subscribe(o => { cd.Signal(); });

                var d2 = pubSub.Subscribe(o => { Assert.Fail(); },
                    ex => { Console.WriteLine("Exception in subscriber thread."); });
                d2.Dispose();

                for (var i = 0; i < max; i++)
                {
                    pubSub.OnNext(i);
                }
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		public void OnCompleted_Should_Get_Passed_To_Subscribers()
		{
			CountdownEvent weAreDone = new CountdownEvent(1);
			{
				int freePort = TcpPortFree();
				var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort);
				pubSub.Subscribe(
					o =>
					{
						// If this gets called more than max times, it will throw an exception as it is going through 0.
						Console.Write("FAIL!");
						Assert.Fail();
					},
					ex =>
					{
						Console.Write("FAIL!");
						Assert.Fail();
					},
					() =>
					{
						Console.Write("Pass!");
						weAreDone.Signal();
					});

				pubSub.OnCompleted();
			}
			if (weAreDone.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}		
        public void OnException_Should_Get_Passed_To_Subscribers()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var weAreDone = new CountdownEvent(1);
            var passed = false;
            {
                var freePort = NUnitUtils.TcpPortFree();
                var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(
                    o =>
                    {
                        // If this gets called more than max times, it will throw an exception as it is going through 0.
                        Assert.Fail();
                    },
                    ex =>
                    {
                        Console.Write("Exception: {0}", ex.Message);
                        if (ex.Message.Contains("passed") == true)
                        {
                            passed = true;
                        }
                        weAreDone.Signal();
                    },
                    () => { Assert.Fail(); });

                pubSub.OnError(new Exception("passed"));
            }

            if (weAreDone.Wait(GlobalTimeout.Timeout) == false || passed == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            if (passed == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Expected exception text did not arrive back.");
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Simplest_Test_Subject()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(5);
            {
                var freePort = NUnitUtils.TcpPortFree();

                var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Console.Write("Test 1: {0}\n", o);
                    cd.Signal();
                },
                    ex => { Console.WriteLine("Exception! {0}", ex.Message); });

                pubSub.OnNext(38);
                pubSub.OnNext(39);
                pubSub.OnNext(40);
                pubSub.OnNext(41);
                pubSub.OnNext(42);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public void Simplest_Fanout_Sub()
        {
            NUnitUtils.PrintTestName();

            var sw = Stopwatch.StartNew();

            var cd = new CountdownEvent(3);
            {
                var freePort = NUnitUtils.TcpPortFree();
                var pubSub = new SubjectNetMQ<int>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub1: {0}\n", o);
                    cd.Signal();
                });
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub2: {0}\n", o);
                    cd.Signal();
                });
                pubSub.Subscribe(o =>
                {
                    Assert.AreEqual(o, 42);
                    Console.Write("PubTwoThreadFanoutSub3: {0}\n", o);
                    cd.Signal();
                });

                pubSub.OnNext(42);
            }

            if (cd.Wait(GlobalTimeout.Timeout) == false) // Blocks until _countdown.Signal has been called.
            {
                Assert.Fail("Timed out, this test should complete in {0} seconds.", GlobalTimeout.Timeout.TotalSeconds);
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
		public void Can_Serialize_Class_Name_Longer_Then_Thirty_Two_Characters()
		{
			Console.WriteLine(TestContext.CurrentContext.Test.Name);

			CountdownEvent cd = new CountdownEvent(5);
			{
				int freePort = TcpPortFree();

				var pubSub = new SubjectNetMQ<ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure>("tcp://127.0.0.1:" + freePort, loggerDelegate: Console.Write);
				pubSub.Subscribe(o =>
				{
					Assert.IsTrue(o.Name == "Bob");
					Console.Write("Test: Num={0}, Name={1}\n", o.Num, o.Name);
					cd.Signal();
				},
					ex => { Console.WriteLine("Exception! {0}", ex.Message); });

				pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(38, "Bob"));
				pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(39, "Bob"));
				pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(40, "Bob"));
				pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(41, "Bob"));
				pubSub.OnNext(new ClassNameIsLongerThenThirtyTwoCharactersForAbsolutelySure(42, "Bob"));
			}

			if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
			{
				Assert.Fail("Timed out, this test should complete in 10 seconds.");
			}
		}