internal bool SendPacket(GenericPacket p, TunnelCongestionControllerMock to)
 {
     if (random.Next(0, 100) < lossPercentage)
     {
         //drop the packet
         Console.WriteLine("DROPPING packet Seq#" + p.Seq);
         return(false);
     }
     else
     {
         //send the packet
         if (this.delayOn)
         {
             Thread.Sleep(delayLength);
         }
         to.HandleIncomingPacket(p);
         return(true);
     }
 }
        /// <summary>
        /// Tests the reliablity components of the underlying protocol. In order to do that,
        /// we need to simulate sending a number of packets in-order, out of order, in-time
        /// and out of time (to simulate dropped packets). This is an extremly granular test
        /// of the packets themselves, a seperate message reliabilty test exists to ensure
        /// that pipes receive their data and congestion control does its job or resending
        /// unacked packets.
        ///
        /// The concrete congestion tests will set up two controller mocks and pass them into
        /// this function which will simulate communications between the two points. This function
        /// assumes that all of the objects have been correctly
        /// </summary>
        /// <param name="ep1">Ep1.</param>
        /// <param name="ep2">Ep2.</param>
        /// <param name="maxTimeoutPerPacket">A maximum timeout per packet
        ///                                   before packetloss is deemed a failure</param>
        internal void TestReliablityAtomic(TunnelCongestionControllerMock ep1,
                                           TunnelCongestionControllerMock ep2,
                                           Int32 maxTimeOutPerPacket)
        {
            Assert.IsNotNull(mPs1, "You must set ep1 to use mPs1 as its packet sender");
            Assert.IsNotNull(mPs2, "You must set ep2 to use mPs2 as its packet sender");

            GenericPacketMock p1           = new GenericPacketMock(1);
            GenericPacketMock p2           = new GenericPacketMock(2);
            GenericPacketMock p3           = new GenericPacketMock(3);
            GenericPacketMock p4           = new GenericPacketMock(4);
            Queue <int>       expectedAcks = new Queue <int> ();

            for (int i = 1; i <= 4; i++)
            {
                expectedAcks.Enqueue(i);
            }

            //the test sends packets from ep1 to ep2
            int curCount = 1;

            mPs1.InterceptOutgoingPacket(p => {
                //ensure that the acks come in order
                ep2.HandleIncomingPacket(p);
            });

            mPs2.InterceptOutgoingPacket(p => {
                ep1.HandleIncomingPacket(p);
                Assert.IsTrue(expectedAcks.Dequeue() == p.Ack, "Failed at packet seq" + p.Ack);
            });


            ep1.SendPacket(p1);
            ep1.SendPacket(p2);
            ep1.SendPacket(p4);
            ep1.SendPacket(p3);
        }