示例#1
0
        protected void TryMillipedeException(Exception exception)
        {
            string tempFileName = Path.GetTempFileName();
            toBeDeleted.Add(tempFileName);

            // Create the mock connector
            MockConnector mockConnector = new MockConnector();
            mockConnector.CreateTransport = delegate { throw exception; };
            mockConnector.Start();

            recorder = new MillipedeRecorder();
            recorder.StartRecording(tempFileName);
            Assert.AreEqual(MillipedeMode.Record, recorder.Mode);
            Assert.AreEqual(0, recorder.NumberEvents);
            MillipedeConnector connector =
                (MillipedeConnector)MillipedeConnector.Wrap(mockConnector, recorder);
            try
            {
                connector.Connect("localhost", "9999", new Dictionary<string, string>());
                Assert.Fail("should have thrown CannotConnectException");
            }
            catch(GTException) { /* ignored */ }
            Assert.AreEqual(1, recorder.NumberEvents);

            recorder.Dispose(); // don't want the disposes add to the list
            mockConnector.Dispose();
            recorder = new MillipedeRecorder();
            recorder.StartReplaying(tempFileName);
            Assert.AreEqual(0, recorder.NumberEvents);
            connector = (MillipedeConnector)MillipedeConnector.Wrap(mockConnector = new MockConnector(),
                recorder);
            try
            {
                connector.Connect("localhost", "9999", new Dictionary<string, string>());
                Assert.Fail("should have thrown " + exception.GetType());
            }
            catch (Exception e)
            {
                Assert.AreEqual(exception.GetType(), e.GetType());
            }
            Assert.AreEqual(1, recorder.NumberEvents);

            recorder.Dispose();
        }
示例#2
0
        public void TestMillipedeConnector()
        {
            string tempFileName = Path.GetTempFileName();
            toBeDeleted.Add(tempFileName);

            // Create the mock connector and transport
            MockTransport mockTransport = new MockTransport("MOCK", new Dictionary<string, string>(),
                Reliability.Reliable, Ordering.Ordered, 1024);
            uint packetsSent = 0;
            mockTransport.PacketSent += delegate { packetsSent++; };
            MockConnector mockConnector = new MockConnector();
            mockConnector.CreateTransport = delegate { return mockTransport; };
            mockConnector.Start();

            recorder.StartRecording(tempFileName);
            Assert.AreEqual(MillipedeMode.Record, recorder.Mode);
            Assert.AreEqual(0, recorder.NumberEvents);
            MillipedeConnector connector =
                (MillipedeConnector)MillipedeConnector.Wrap(mockConnector, recorder);
            ITransport transport = connector.Connect("localhost", "9999", new Dictionary<string, string>());
            Assert.IsInstanceOfType(typeof(MillipedeTransport), transport);
            Assert.AreEqual(1, recorder.NumberEvents);
            transport.SendPacket(new TransportPacket(new byte[10]));
            Assert.AreEqual(2, recorder.NumberEvents);
            Assert.AreEqual(1, packetsSent, "millipede didn't pass down packet-send in record");
            Thread.Sleep(50);  // give a good amount of time for the test below
            mockTransport.InjectReceivedPacket(new byte[5]);
            Assert.AreEqual(3, recorder.NumberEvents);

            recorder.Dispose(); // don't want the disposes add to the list
            mockConnector.Dispose();
            mockTransport.Dispose();

            recorder = new MillipedeRecorder();
            recorder.StartReplaying(tempFileName);
            Assert.AreEqual(0, recorder.NumberEvents);
            connector = (MillipedeConnector)MillipedeConnector.Wrap(mockConnector = new MockConnector(),
                recorder);
            transport = connector.Connect("localhost", "9999", new Dictionary<string, string>());
            transport.PacketReceived += (packet, t) =>
            {
                Assert.AreEqual(new byte[5], packet.ToArray());
                Assert.AreEqual(3, recorder.NumberEvents);
            };

            Assert.IsInstanceOfType(typeof(MillipedeTransport), transport);
            Assert.AreEqual(1, recorder.NumberEvents);
            transport.SendPacket(new TransportPacket(new byte[10]));
            Assert.AreEqual(2, recorder.NumberEvents);
            transport.Update();
            for (int i = 0; i < 5 && recorder.NumberEvents == 2; i++)
            {
                Thread.Sleep(100);
                transport.Update();
            }
            Assert.AreEqual(3, recorder.NumberEvents);  // should have received the packet too
        }