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 119
/
TransportTests.cs
143 lines (115 loc) · 4.86 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using NUnit.Framework;
namespace IPC.Managed.UnitTests
{
using IPC.Containers.Managed;
[TestFixture(Category = "ManagedTests")]
public class TransportTests
{
private TransportFactory _factory;
private TransportFactory Factory
{
get
{
if (_factory == null)
{
_factory = new TransportFactory();
_factory.Register(System.Reflection.Assembly.Load("TestManagedTransport"));
}
return _factory;
}
}
[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 = Factory.Make<int, int>(config))
using (var acceptor = transport.MakeServerAcceptor(address, (inMemory, outMemory) => x => Task.FromResult(x)))
using (var connector = transport.MakeClientConnector())
{
var servers = new List<IServer<int, int>>();
var newServerEvent = new System.Threading.ManualResetEventSlim(false);
acceptor.Accepted += (sender, args) =>
{
lock (servers)
{
servers.Add(args.Component);
}
newServerEvent.Set();
};
IServer<int, int> 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; };
Assert.AreEqual(100, await client.InvokeAsync(100));
}
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 = Factory.Make<int, int>(config))
using (var serversAccessor = transport.AcceptServers(address, (inMemory, outMemory) => x => Task.FromResult(x)))
using (var clientAccessor = transport.ConnectClient(address, false))
{
Assert.AreEqual(100, await clientAccessor.Client.InvokeAsync(100));
}
}
[Test]
public async Task ContainerTest()
{
var address = Guid.NewGuid().ToString();
var config = new Config { DefaultRequestTimeout = System.Threading.Timeout.InfiniteTimeSpan };
HandlerFactory<IVector<int>, IVector<int>> handlerFactory =
(inMemory, outMemory) => request => Task.Run(() =>
{
var response = Factory.Make<IVector<int>>(outMemory);
using (request)
{
response.Reserve(request.Count);
foreach (int x in request)
{
response.Add(x * 2);
}
}
return response;
});
using (var transport = Factory.Make<IVector<int>, IVector<int>>(config))
using (var serversAccessor = transport.AcceptServers(address, handlerFactory))
using (var clientAccessor = transport.ConnectClient(address, false))
{
var client = clientAccessor.Client;
Task<IVector<int>> responseTask;
using (var request = Factory.Make<IVector<int>>(client.OutputMemory))
{
request.Resize(100, 123);
responseTask = client.InvokeAsync(request);
}
using (var response = await responseTask)
{
CollectionAssert.AreEqual(Enumerable.Repeat(123 * 2, 100), response);
}
}
}
}
}