Beispiel #1
0
        public void TestLossAndOrder()
        {
            int previousReceivedServer = 0;
            int previousReceivedClient = 0;

            using (var serverNode = TestBootstrap.BuildServer())
            {
                serverNode.OnReceived = msg =>
                {
                    Console.Out.WriteLine("Server received msg: " + msg);
                    SampleGuaranteedMessage sampleMsg = msg as SampleGuaranteedMessage;
                    if (sampleMsg != null)
                    {
                        Assert.AreEqual(previousReceivedServer + 1, sampleMsg.TheNumber, "Server received " + sampleMsg.TheNumber + " out of sequence");
                        previousReceivedServer = sampleMsg.TheNumber;
                        var reply = serverNode.GetMessage <SampleGuaranteedMessage>();
                        reply.TheNumber = sampleMsg.TheNumber;
                        serverNode.SendToAll(reply);
                    }
                };

                serverNode.Start();

                using (var clientNode = TestBootstrap.BuildClient())
                {
                    clientNode.OnReceived = msg =>
                    {
                        Console.Out.WriteLine("Client received msg: " + msg);
                        SampleGuaranteedMessage sampleMsg = msg as SampleGuaranteedMessage;
                        if (sampleMsg != null)
                        {
                            Assert.AreEqual(previousReceivedClient + 1, sampleMsg.TheNumber, "Client received " + sampleMsg.TheNumber + " out of sequence");
                            previousReceivedClient = sampleMsg.TheNumber;
                        }
                    };

                    clientNode.Start();

                    // Finish connect msg, etc.
                    for (int i = 0; i < 100; i++)
                    {
                        clientNode.Update();
                        serverNode.Update();
                        Thread.Sleep(10);
                    }

                    int numOutbound = 0;
                    foreach (var client in clientNode.ConnectedPeers)
                    {
                        numOutbound += client.NumberOfOutboundMessages;
                    }
                    Assert.AreEqual(0, numOutbound, "Client should have purged and ack'ed all messages at this point");

                    numOutbound = 0;
                    foreach (var client in serverNode.ConnectedPeers)
                    {
                        numOutbound += client.NumberOfOutboundMessages;
                    }
                    Assert.AreEqual(0, numOutbound, "Server should have purged and ack'ed all messages at this point");

#if DEBUG
                    serverNode.SimulatedPacketLossRate    = 0.1;
                    serverNode.SimulatedPacketShuffleRate = 0.1;
#else
                    Console.Error.WriteLine("Running packet loss unit test on a RELEASE build, which is fruitless - packet loss simulation only works on DEBUG builds - you should run this test on a DEBUG configuration");
#endif
                    Console.Out.WriteLine("Beginning guaranteed messages...");

                    const int numToSend = 1000;
                    var       clientMsg = clientNode.GetMessage <SampleGuaranteedMessage>();
                    for (int i = 0; i < numToSend; i++)
                    {
                        clientMsg.TheNumber = i + 1;
                        clientNode.SendToAll(clientMsg);
                        clientNode.Update();
                        serverNode.Update();
                        Thread.Sleep(10);
                    }

                    for (int i = 0; i < 1000; i++)
                    {
                        clientNode.Update();
                        serverNode.Update();
                        Thread.Sleep(10);
                    }

                    Assert.AreEqual(numToSend, previousReceivedServer);
                    Assert.AreEqual(numToSend, previousReceivedClient);
                    Assert.AreEqual(0, clientNode.NumberOfOutboundMessages);
                    Assert.AreEqual(0, serverNode.NumberOfOutboundMessages);
                }
            }
        }
Beispiel #2
0
        public void TestGuaranteed()
        {
            int previousReceivedServer = 0;
            int previousReceivedClient = 0;

            using (var serverNode = TestBootstrap.BuildServer())
            {
                serverNode.OnReceived = msg =>
                {
                    Console.Out.WriteLine("Server received msg: " + msg);
                    SampleGuaranteedMessage sampleMsg = msg as SampleGuaranteedMessage;
                    if (sampleMsg != null)
                    {
                        Assert.AreEqual(previousReceivedServer + 1, sampleMsg.TheNumber, "Server received " + sampleMsg.TheNumber + " out of sequence");
                        previousReceivedServer = sampleMsg.TheNumber;
                        var reply = serverNode.GetMessage <SampleGuaranteedMessage>();
                        reply.TheNumber = sampleMsg.TheNumber;
                        serverNode.SendToAll(reply);
                    }
                };

                serverNode.Start();

                using (var clientNode = TestBootstrap.BuildClient())
                {
                    clientNode.OnReceived = msg =>
                    {
                        Console.Out.WriteLine("Client received msg: " + msg);
                        SampleGuaranteedMessage sampleMsg = msg as SampleGuaranteedMessage;
                        if (sampleMsg != null)
                        {
                            Assert.AreEqual(previousReceivedClient + 1, sampleMsg.TheNumber);
                            previousReceivedClient = sampleMsg.TheNumber;
                        }
                    };

                    clientNode.Start();

                    NeutrinoConfig.LogLevel = NeutrinoLogLevel.Warn;

                    // Finish connect msg, etc.
                    for (int i = 0; i < 100; i++)
                    {
                        clientNode.Update();
                        serverNode.Update();
                        Thread.Sleep(10);
                    }

                    int numOutbound = 0;
                    foreach (var client in clientNode.ConnectedPeers)
                    {
                        numOutbound += client.NumberOfOutboundMessages;
                    }
                    Assert.AreEqual(0, numOutbound, "Client should have purged and ack'ed all messages at this point");

                    numOutbound = 0;
                    foreach (var client in serverNode.ConnectedPeers)
                    {
                        numOutbound += client.NumberOfOutboundMessages;
                    }
                    Assert.AreEqual(0, numOutbound, "Server should have purged and ack'ed all messages at this point");

                    NeutrinoConfig.Log("Beginning guaranteed messages...");

                    var clientMsg = clientNode.GetMessage <SampleGuaranteedMessage>();
                    for (int i = 0; i < 100; i++)
                    {
                        clientMsg.TheNumber = i + 1;
                        clientNode.SendToAll(clientMsg);
                        clientNode.Update();
                        serverNode.Update();
                        //Thread.Sleep(1);
                    }

                    for (int i = 0; i < 10; i++)
                    {
                        clientNode.Update();
                        serverNode.Update();
                        Thread.Sleep(100);
                    }

                    Assert.AreEqual(100, previousReceivedServer);
                    Assert.AreEqual(100, previousReceivedClient);
                }
            }
        }