This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTransportTests.cs
87 lines (69 loc) · 3.17 KB
/
TransportTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using NUnit.Framework;
namespace IPC.Bond.Managed.UnitTests
{
[TestFixture(Category = "ManagedTests")]
public class TransportTests
{
[Test]
public async Task AcceptorConnectorTest()
{
var address = Guid.NewGuid().ToString();
bool isClientClosed = false;
bool isServerClosed = false;
var config = new Config { DefaultRequestTimeout = System.Threading.Timeout.InfiniteTimeSpan };
using (var transport = new Transport<global::Bond.Box<int>, global::Bond.Box<int>>(config))
using (var acceptor = transport.MakeServerAcceptor(address, (inMemory, outMemory) => x => Task.FromResult(x)))
using (var connector = transport.MakeClientConnector())
{
var servers = new List<Transport<global::Bond.Box<int>, global::Bond.Box<int>>.Server>();
var newServerEvent = new System.Threading.ManualResetEventSlim(false);
acceptor.Accepted += (sender, args) =>
{
lock (servers)
{
servers.Add(args.Component);
}
newServerEvent.Set();
};
Transport<global::Bond.Box<int>, global::Bond.Box<int>>.Server server;
using (var client = await connector.ConnectAsync(address))
{
newServerEvent.Wait();
lock (servers)
{
Assert.AreEqual(1, servers.Count);
server = servers.First();
}
Assert.IsFalse(server.IsClosed);
server.Closed += (sender, args) => { isServerClosed = true; };
Assert.IsFalse(client.IsClosed);
client.Closed += (sender, args) => { isClientClosed = true; };
var request = new global::Bond.Box<int> { value = 100 };
var response = await client.InvokeAsync(request);
Assert.IsTrue(global::Bond.Comparer.Equal(request, response));
}
server.Dispose();
}
Assert.IsTrue(isClientClosed);
Assert.IsTrue(isServerClosed);
}
[Test]
public async Task AcceptConnectTest()
{
var address = Guid.NewGuid().ToString();
var config = new Config { DefaultRequestTimeout = System.Threading.Timeout.InfiniteTimeSpan };
using (var transport = new Transport<global::Bond.Box<int>, global::Bond.Box<int>>(config))
using (var serversAccessor = transport.AcceptServers(address, (inMemory, outMemory) => x => Task.FromResult(x)))
using (var clientAccessor = transport.ConnectClient(address, false))
{
var request = new global::Bond.Box<int> { value = 200 };
var response = await clientAccessor.Client.InvokeAsync(request);
Assert.IsTrue(global::Bond.Comparer.Equal(request, response));
}
}
}
}