Skip to content

Commit

Permalink
Added a unit test for messages larger than 2 megabytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Nov 30, 2024
1 parent 792f579 commit 967bdc1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
49 changes: 49 additions & 0 deletions CoreRemoting.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,5 +608,54 @@ public void DataTable_roundtrip_works_issue60()
var dt2 = proxy.TestDt(dt, 1);
Assert.NotNull(dt2);
}

[Fact]
public void Large_messages_are_sent_and_received()
{
// max payload size, in bytes
var maxSize = 2 * 1024 * 1024 + 1;

using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
InvocationTimeout = 0,
SendTimeout = 0,
Channel = ClientChannel,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort,
});

client.Connect();
var proxy = client.CreateProxy<ITestService>();

// shouldn't throw exceptions
Roundtrip("Payload", maxSize);
Roundtrip(new byte[] { 1, 2, 3, 4, 5 }, maxSize);
Roundtrip(new int[] { 12345, 67890 }, maxSize);

void Roundtrip<T>(T payload, int maxSize) where T : class
{
var lastSize = 0;
try
{
while (true)
{
// a -> aa -> aaaa ...
var dup = proxy.Duplicate(payload);
if (dup.size >= maxSize)
break;

// save the size for error reporting
lastSize = dup.size;
payload = dup.duplicate;
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to handle " +
$"payload larger than {lastSize}: {ex.Message}", ex);
}
}
}
}
}
2 changes: 2 additions & 0 deletions CoreRemoting.Tests/Tools/ITestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ public interface ITestService : IBaseService
void NonSerializableError(string text, params object[] data);

DataTable TestDt(DataTable dt, long num);

(T duplicate, int size) Duplicate<T>(T sample) where T : class;
}
}
19 changes: 19 additions & 0 deletions CoreRemoting.Tests/Tools/TestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,24 @@ public DataTable TestDt(DataTable dt, long num)
dt.Rows.Clear();
return dt;
}

public (T, int) Duplicate<T>(T sample) where T : class
{
return sample switch
{
byte[] arr => (Dup(arr) as T, arr.Length * 2),
int[] iarr => (Dup(iarr) as T, iarr.Length * 2 * sizeof(int)),
string str => ((str + str) as T, str.Length * 2 * sizeof(char)),
_ => throw new ArgumentOutOfRangeException(),
};

TItem[] Dup<TItem>(TItem[] arr)
{
var length = arr.Length;
Array.Resize(ref arr, length * 2);
Array.Copy(arr, 0, arr, length, length);
return arr;
}
}
}
}

0 comments on commit 967bdc1

Please sign in to comment.