Skip to content

Improvement - Reliable Unit Test #11

@shasheppard

Description

@shasheppard

Sorry to list this out as an issue; however, I did a little robustification to the reliable unit test to ensure fragmentation occurs. Maybe something herein will be useful:
`///


/// Packets are sent through the Reliable channel with random packet loss. All packets should be received
///

[TestMethod]
public void TestReliability()
{
Random rand = new Random();

		for (int run = 0; run < RANDOM_RUNS; run++)
		{
			Console.WriteLine("RUN: " + run);

			List<byte[]> sentPackets = new List<byte[]>();
			List<byte[]> receivedPackets = new List<byte[]>();

			int droppedPackets = 0;

			ReliableEndpoint endpoint1 = new ReliableEndpoint();
			endpoint1.ReceiveCallback = (buffer, size) =>
			{
                byte[] copy = new byte[size];
                Buffer.BlockCopy(buffer, 0, copy, 0, size);
                receivedPackets.Add(copy);
			};
			endpoint1.TransmitCallback = (buffer, size) =>
			{
                const int PERCENT_PACKET_LOSS = 25;
				if (rand.Next(100) > PERCENT_PACKET_LOSS)
					endpoint1.ReceivePacket(buffer, size);
				else
					droppedPackets++;
			};

            const int MESSAGE_SIZE = 10000;
            for (int i = 0; i < 5; i++)
			{
				byte[] test = new byte[MESSAGE_SIZE];
				test[0] = 0;
				test[1] = (byte)i;

                for (int j = 0; j < MESSAGE_SIZE - 2; ++j)
                {
                    test[j + 2] = (byte)new Random(j).Next(byte.MaxValue);
                }

                sentPackets.Add(test);
                endpoint1.SendMessage(test, MESSAGE_SIZE, QosType.Reliable);

				endpoint1.UpdateFastForward(0.1);
			}

			int iterations = 0;
			while (receivedPackets.Count < sentPackets.Count)
			{
				endpoint1.UpdateFastForward(1.0);
				iterations++;
			}

			Console.WriteLine("Dropped packets: " + droppedPackets);

			if (receivedPackets.Count == sentPackets.Count)
			{
				bool didComparisonPass = true;
				for (int i = 0; i < receivedPackets.Count && didComparisonPass; i++)
				{
                    byte[] received = receivedPackets[i];
                    byte[] sent = sentPackets[i];
                    for (int j = 0; j < MESSAGE_SIZE - 2; ++j)
                    {
                        if (received[j] != sent[j])
                        {
                            didComparisonPass = false;
                            break;
                        }
                    }

                }

				if (!didComparisonPass)
				{
					throw new System.Exception("Received packet contents differ!");
				}
			}
			else
			{
				throw new System.Exception((sentPackets.Count - receivedPackets.Count) + " packets not received");
			}
		}
	}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions