Skip to content

Commit

Permalink
tests: Added connection stubbing for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Sep 10, 2019
1 parent 3778277 commit 19bb81e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 28 deletions.
12 changes: 9 additions & 3 deletions Ruffles.Tests/Channels/ReliableSequencedChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class ReliableSequencedChannelTests
[Test()]
public void TestSimpleMessage()
{
Configuration.SocketConfig config = new Configuration.SocketConfig();
Configuration.SocketConfig config = new Configuration.SocketConfig()
{
MinimumMTU = 1050
};

MemoryManager memoryManager = new MemoryManager(config);

Expand All @@ -37,7 +40,10 @@ public void TestSimpleMessage()
[Test()]
public void TestOutOfOrder()
{
Configuration.SocketConfig config = new Configuration.SocketConfig();
Configuration.SocketConfig config = new Configuration.SocketConfig()
{
MinimumMTU = 1050
};

MemoryManager memoryManager = new MemoryManager(config);

Expand Down Expand Up @@ -90,4 +96,4 @@ public void TestOutOfOrder()
}
}
}
}
}
63 changes: 42 additions & 21 deletions Ruffles.Tests/Channels/ReliableSequencedFragmentedChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Ruffles.Memory;
using Ruffles.Tests.Helpers;
using System;
using System.Linq;

namespace Ruffles.Tests.Channels
{
Expand All @@ -13,7 +14,10 @@ public class ReliableSequencedFragmentedChannelTests
[Test()]
public void TestSimpleMessageSingleFragment()
{
Configuration.SocketConfig config = new Configuration.SocketConfig();
Configuration.SocketConfig config = new Configuration.SocketConfig()
{
MinimumMTU = 1050
};

MemoryManager memoryManager = new MemoryManager(config);

Expand Down Expand Up @@ -41,7 +45,10 @@ public void TestSimpleMessageSingleFragment()
[Test()]
public void TestOutOfOrderSingleFragment()
{
Configuration.SocketConfig config = new Configuration.SocketConfig();
Configuration.SocketConfig config = new Configuration.SocketConfig()
{
MinimumMTU = 1050
};

MemoryManager memoryManager = new MemoryManager(config);

Expand Down Expand Up @@ -100,7 +107,10 @@ public void TestOutOfOrderSingleFragment()
[Test()]
public void TestOutOfFragmentOrderMultiFragment()
{
Configuration.SocketConfig config = new Configuration.SocketConfig();
Configuration.SocketConfig config = new Configuration.SocketConfig()
{
MinimumMTU = 1050
};

MemoryManager memoryManager = new MemoryManager(config);

Expand All @@ -120,35 +130,39 @@ public void TestOutOfFragmentOrderMultiFragment()
object[] message2Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message2, 0, 1024 * 8), out _, out dealloc);
object[] message3Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message3, 0, 1024 * 6), out _, out dealloc);

int message1MemoryLength = message1Memory.Count(x => x != null);
int message2MemoryLength = message2Memory.Count(x => x != null);
int message3MemoryLength = message3Memory.Count(x => x != null);

// Consume 1st payload all except first fragment
bool[] hasMore1s = new bool[message1Memory.Length];
ArraySegment<byte>?[] payload1s = new ArraySegment<byte>?[message1Memory.Length];
for (int i = 1; i < message1Memory.Length; i++)
bool[] hasMore1s = new bool[message1MemoryLength];
ArraySegment<byte>?[] payload1s = new ArraySegment<byte>?[message1MemoryLength];
for (int i = 1; i < message1MemoryLength; i++)
{
payload1s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message1Memory[i]).Buffer, (int)((HeapMemory)message1Memory[i]).VirtualOffset + 2, (int)((HeapMemory)message1Memory[i]).VirtualCount - 2), out _, out bool hasMore1);
hasMore1s[i] = hasMore1;
}
// Consume 3rd payload only last fragment
bool[] hasMore3s = new bool[message3Memory.Length];
ArraySegment<byte>?[] payload3s = new ArraySegment<byte>?[message3Memory.Length];
bool[] hasMore3s = new bool[message3MemoryLength];
ArraySegment<byte>?[] payload3s = new ArraySegment<byte>?[message3MemoryLength];

{
payload3s[payload3s.Length - 1] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message3Memory[payload3s.Length - 1]).Buffer, (int)((HeapMemory)message3Memory[payload3s.Length - 1]).VirtualOffset + 2, (int)((HeapMemory)message3Memory[payload3s.Length - 1]).VirtualCount - 2), out _, out bool hasMore3);
hasMore3s[payload3s.Length - 1] = hasMore3;
}

// Consume 2nd payload all fragments
bool[] hasMore2s = new bool[message2Memory.Length];
ArraySegment<byte>?[] payload2s = new ArraySegment<byte>?[message2Memory.Length];
for (int i = 0; i < message2Memory.Length; i++)
bool[] hasMore2s = new bool[message2MemoryLength];
ArraySegment<byte>?[] payload2s = new ArraySegment<byte>?[message2MemoryLength];
for (int i = 0; i < message2MemoryLength; i++)
{
payload2s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message2Memory[i]).Buffer, (int)((HeapMemory)message2Memory[i]).VirtualOffset + 2, (int)((HeapMemory)message2Memory[i]).VirtualCount - 2), out _, out bool hasMore2);
hasMore2s[i] = hasMore2;
}

{
// Consume 3rd payload all except last fragment (completes the last payload)
for (int i = 0; i < message3Memory.Length - 1; i++)
for (int i = 0; i < message3MemoryLength - 1; i++)
{
payload3s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message3Memory[i]).Buffer, (int)((HeapMemory)message3Memory[i]).VirtualOffset + 2, (int)((HeapMemory)message3Memory[i]).VirtualCount - 2), out _, out bool hasMore3);
hasMore3s[i] = hasMore3;
Expand Down Expand Up @@ -202,7 +216,10 @@ public void TestOutOfFragmentOrderMultiFragment()
[Test()]
public void TestOutOfPacketOrderMultiFragment()
{
Configuration.SocketConfig config = new Configuration.SocketConfig();
Configuration.SocketConfig config = new Configuration.SocketConfig()
{
MinimumMTU = 1050
};

MemoryManager memoryManager = new MemoryManager(config);

Expand All @@ -222,26 +239,30 @@ public void TestOutOfPacketOrderMultiFragment()
object[] message2Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message2, 0, 1024 * 8), out _, out dealloc);
object[] message3Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message3, 0, 1024 * 6), out _, out dealloc);

int message1MemoryLength = message1Memory.Count(x => x != null);
int message2MemoryLength = message2Memory.Count(x => x != null);
int message3MemoryLength = message3Memory.Count(x => x != null);

// Consume 1st payload
bool[] hasMore1s = new bool[message1Memory.Length];
ArraySegment<byte>?[] payload1s = new ArraySegment<byte>?[message1Memory.Length];
for (int i = 0; i < message1Memory.Length; i++)
bool[] hasMore1s = new bool[message1MemoryLength];
ArraySegment<byte>?[] payload1s = new ArraySegment<byte>?[message1MemoryLength];
for (int i = 0; i < message1MemoryLength; i++)
{
payload1s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message1Memory[i]).Buffer, (int)((HeapMemory)message1Memory[i]).VirtualOffset + 2, (int)((HeapMemory)message1Memory[i]).VirtualCount - 2), out _, out bool hasMore1);
hasMore1s[i] = hasMore1;
}
// Consume 3rd payload
bool[] hasMore3s = new bool[message3Memory.Length];
bool[] hasMore3s = new bool[message3MemoryLength];
ArraySegment<byte>?[] payload3s = new ArraySegment<byte>?[message3Memory.Length];
for (int i = 0; i < message3Memory.Length; i++)
for (int i = 0; i < message3MemoryLength; i++)
{
payload3s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message3Memory[i]).Buffer, (int)((HeapMemory)message3Memory[i]).VirtualOffset + 2, (int)((HeapMemory)message3Memory[i]).VirtualCount - 2), out _, out bool hasMore3);
hasMore3s[i] = hasMore3;
}
// Consume 2nd payload
bool[] hasMore2s = new bool[message2Memory.Length];
ArraySegment<byte>?[] payload2s = new ArraySegment<byte>?[message2Memory.Length];
for (int i = 0; i < message2Memory.Length; i++)
bool[] hasMore2s = new bool[message2MemoryLength];
ArraySegment<byte>?[] payload2s = new ArraySegment<byte>?[message2MemoryLength];
for (int i = 0; i < message2MemoryLength; i++)
{
payload2s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(((HeapMemory)message2Memory[i]).Buffer, (int)((HeapMemory)message2Memory[i]).VirtualOffset + 2, (int)((HeapMemory)message2Memory[i]).VirtualCount - 2), out _, out bool hasMore2);
hasMore2s[i] = hasMore2;
Expand Down
39 changes: 35 additions & 4 deletions Ruffles/Connections/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#define ALLOW_CONNECTION_STUB

using System;
using System.Net;
using Ruffles.Channeling;
using Ruffles.Channeling.Channels;
Expand Down Expand Up @@ -188,7 +190,14 @@ internal set
internal DateTime HandshakeLastSendTime;

internal Connection(SocketConfig config, MemoryManager memoryManager)
{
{
#if ALLOW_CONNECTION_STUB
if (IsStub)
{
// NOOP
return;
}
#endif
if (config.EnableHeartbeats)
{
HeartbeatChannel = new UnreliableSequencedChannel(0, this, config, memoryManager);
Expand All @@ -197,11 +206,25 @@ internal Connection(SocketConfig config, MemoryManager memoryManager)

internal void SendRaw(ArraySegment<byte> payload, bool noMerge, ushort headerSize)
{
#if ALLOW_CONNECTION_STUB
if (IsStub)
{
// NOOP
return;
}
#endif
Socket.SendRaw(this, payload, noMerge, headerSize);
}

internal void Disconnect(bool sendMessage)
{
{
#if ALLOW_CONNECTION_STUB
if (IsStub)
{
// NOOP
return;
}
#endif
Socket.DisconnectConnection(this, sendMessage, false);
}

Expand Down Expand Up @@ -274,11 +297,19 @@ public void Recycle()
Recycled = true;
}
}

#if ALLOW_CONNECTION_STUB
private bool IsStub { get; set; }

// Used by Test project
internal static Connection Stub(SocketConfig config, MemoryManager manager)
{
return new Connection(config, manager);
return new Connection(config, manager)
{
IsStub = true,
MTU = config.MinimumMTU
};
}
#endif
}
}

0 comments on commit 19bb81e

Please sign in to comment.