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);
        }
        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 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);
        }
Beispiel #5
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 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 Test_Two_Subscribers()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            using (var pub = new PublisherSocket())
            {
                using (var sub1 = new SubscriberSocket())
                {
                    using (var sub2 = new SubscriberSocket())
                    {
                        var freePort = NUnitUtils.TcpPortFree();
                        pub.Bind("tcp://127.0.0.1:" + freePort);
                        sub1.Connect("tcp://127.0.0.1:" + freePort);
                        sub1.Subscribe("A");
                        sub2.Connect("tcp://127.0.0.1:" + freePort);
                        sub2.Subscribe("B");

                        Thread.Sleep(500);

                        var swInner = Stopwatch.StartNew();
                        {
                            pub.SendFrame("A\n"); // Ping.
                            {
                                string topic;
                                var    pass1 = sub1.TryReceiveFrameString(TimeSpan.FromMilliseconds(250), out topic);
                                if (pass1)
                                {
                                    Console.Write(topic);
                                }
                                else
                                {
                                    Assert.Fail();
                                }
                            }
                            pub.SendFrame("B\n"); // Ping.
                            {
                                string topic;
                                var    pass2 = sub2.TryReceiveFrameString(TimeSpan.FromMilliseconds(250), out topic);
                                if (pass2)
                                {
                                    Console.Write(topic);
                                }
                                else
                                {
                                    Assert.Fail();
                                }
                            }
                        }
                        Console.WriteLine("Connected in {0} ms.", swInner.ElapsedMilliseconds);
                    }
                }
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        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 static void Should_be_able_to_serialize_an_int()
        {
            NUnitUtils.PrintTestName();

            Stopwatch sw = Stopwatch.StartNew();

            const int x        = 42;
            var       rawBytes = x.SerializeProtoBuf();
            var       original = rawBytes.DeserializeProtoBuf <int>();

            Assert.AreEqual(x, original);

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

            Stopwatch sw         = Stopwatch.StartNew();
            var       serializer = new SerializeViaProtoBuf <string>();

            const string x        = "Hello";
            var          rawBytes = serializer.Serialize(x);
            var          original = serializer.Deserialize(rawBytes);

            Assert.AreEqual(x, original);

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        public static void Speed_Test_Publisher_Subscriber()
        {
            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 publisher = new PublisherNetMq <int>("tcp://127.0.0.1:" + freePort,
                                                             loggerDelegate: Console.Write);
                    var subscriber = new SubscriberNetMq <int>("tcp://127.0.0.1:" + freePort,
                                                               loggerDelegate: Console.Write);

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

                    sw.Start();
                    for (var i = 0; i < max; i++)
                    {
                        publisher.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 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 PubSub_Should_Not_Crash_If_No_Thread_Sleep()
        {
            NUnitUtils.PrintTestName();
            var swAll = Stopwatch.StartNew();

            using (var pub = new PublisherSocket())
            {
                using (var sub = new SubscriberSocket())
                {
                    var freePort = NUnitUtils.TcpPortFree();
                    pub.Bind("tcp://127.0.0.1:" + freePort);
                    sub.Connect("tcp://127.0.0.1:" + freePort);

                    sub.Subscribe("*");

                    var sw = Stopwatch.StartNew();
                    {
                        for (var i = 0; i < 50; i++)
                        {
                            pub.SendFrame("*"); // Ping.

                            Console.Write("*");
                            string topic;
                            var    gotTopic = sub.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out topic);
                            string ping;
                            var    gotPing = sub.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out ping);
                            if (gotTopic)
                            {
                                Console.Write("\n");
                                break;
                            }
                        }
                    }
                    Console.WriteLine("Connected in {0} ms.", sw.ElapsedMilliseconds);
                }
            }
            NUnitUtils.PrintElapsedTime(swAll.Elapsed);
        }
        public static void Can_Serialize_An_Exception()
        {
            NUnitUtils.PrintTestName();

            Stopwatch sw = Stopwatch.StartNew();

            var ex1 = new Exception("My Inner Exception 2");
            var ex2 = new Exception("My Exception 1", ex1);

            var originalException = new SerializableException(ex2);

            // Save the full ToString() value, including the exception message and stack trace.

            var rawBytes     = originalException.SerializeException();
            var newException = rawBytes.DeSerializeException();

            var originalExceptionAsString = originalException.ToString();
            var newExceptionAsString      = newException.ToString();

            // Double-check that the exception message and stack trace (owned by the base Exception) are preserved
            Assert.AreEqual(originalExceptionAsString, newExceptionAsString);

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
        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);
        }