public void ShouldSendAndReceiveSessionMessagesCorrectly() { var seed1 = "d0de404077ede0fdd1dfd15ab2934018fa2f8d1ac1effb4af577dbedc897b0b8"; var seed2 = "16735a849deaa136ba6030c3695c4cbdc9b275d5d9a9f46b836841ab4a36179e"; var address1 = "a4a5d152f83fe8802ba1329ed3e31aa2f3492fc1a775ee02f41927103f0cc088"; var address2 = "7ade8659d490283303beb2f224cff1f3709364ce6765a7132d65ed1a6e10ecf9"; var data = new List <byte[]> { new byte[] { 1, 1, 1 }, new byte[] { 2, 2, 2 }, new byte[] { 3, 3, 3 }, new byte[] { 4, 4, 4 }, new byte[] { 5, 5, 5 }, }; var dataCount = data.Count; MultiClient client1 = default; var client2 = new MultiClient(new MultiClientOptions { SeedHex = seed2 }); client2.Listen(new string[] { address1 }); var finished = false; client2.OnConnect(async request => { Thread.Sleep(500); client1 = new MultiClient(new MultiClientOptions { SeedHex = seed1 }); client1.OnConnect(async request => { var session = client1.DialAsync(address2); Assert.Equal(address2, session.RemoteAddress); for (int i = 0; i < dataCount; i++) { session.WriteAsync(data[i]); } }); }); while (finished == false) { Thread.Sleep(100); } Thread.Sleep(1_000); client1.CloseAsync(); client2.CloseAsync(); Thread.Sleep(PauseBetweenTests); Assert.True(finished); }
public async Task ShouldSendAndReceiveSessionMessagesCorrectly() { var seed1 = "d0de404077ede0fdd1dfd15ab2934018fa2f8d1ac1effb4af577dbedc897b0b8"; var seed2 = "16735a849deaa136ba6030c3695c4cbdc9b275d5d9a9f46b836841ab4a36179e"; var address1 = "a4a5d152f83fe8802ba1329ed3e31aa2f3492fc1a775ee02f41927103f0cc088"; var address2 = "7ade8659d490283303beb2f224cff1f3709364ce6765a7132d65ed1a6e10ecf9"; var data = new List <byte[]> { new byte[] { 1, 1, 1 }, new byte[] { 2, 2, 2 }, new byte[] { 3, 3, 3 }, new byte[] { 4, 4, 4 }, new byte[] { 5, 5, 5 }, }; var dataCount = data.Count; MultiClient client1 = default; var client2 = new MultiClient(new MultiClientOptions { SeedHex = seed2 }); client2.Listen(new string[] { address1 }); var finished = false; client2.OnSession(session => { Task.Delay(500).GetAwaiter().GetResult(); Assert.Equal(address1, session.RemoteAddress); var dataSize = data.Select(x => x.Length).FirstOrDefault(); for (int i = 0; i < dataCount; i++) { var received = session.ReadAsync(dataSize).GetAwaiter().GetResult(); Assert.Equal(data[i], received); } finished = true; return(Task.FromResult((object)true)); }); client2.OnConnect(async request => { await Task.Delay(500); client1 = new MultiClient(new MultiClientOptions { SeedHex = seed1 }); client1.OnConnect(async request => { var session = await client1.DialAsync(address2); Assert.Equal(address2, session.RemoteAddress); for (int i = 0; i < dataCount; i++) { await session.WriteAsync(data[i]); } }); await Task.Yield(); }); while (finished == false) { await Task.Delay(100); } await Task.Delay(1_000); var closeTasks = new List <Task> { client1.CloseAsync(), client2.CloseAsync() }; await Task.WhenAll(closeTasks); await Task.Delay(PauseBetweenTests); Assert.True(finished); }