-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCommunicationTests.cs
More file actions
314 lines (262 loc) · 7.42 KB
/
CommunicationTests.cs
File metadata and controls
314 lines (262 loc) · 7.42 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Events.Console;
using Waher.Networking.Sniffers;
using Waher.Runtime.Console;
namespace Waher.Networking.XMPP.Test
{
[TestClass]
public abstract class CommunicationTests
{
private static ConsoleEventSink sink = null;
private static XmlFileSniffer xmlSniffer1 = null;
private static XmlFileSniffer xmlSniffer2 = null;
protected ManualResetEvent connected1 = new(false);
protected ManualResetEvent error1 = new(false);
protected ManualResetEvent offline1 = new(false);
protected ManualResetEvent connected2 = new(false);
protected ManualResetEvent error2 = new(false);
protected ManualResetEvent offline2 = new(false);
protected XmppClient client1;
protected XmppClient client2;
protected Exception ex1 = null;
protected Exception ex2 = null;
public CommunicationTests()
{
}
public static void SetupSnifferAndLog()
{
sink = new ConsoleEventSink();
Log.Register(sink);
if (xmlSniffer1 is null)
{
File.Delete("XMPP1.xml");
xmlSniffer1 = new XmlFileSniffer("XMPP1.xml",
@"..\..\..\..\..\Waher.IoTGateway.Resources\Transforms\SnifferXmlToHtml.xslt",
int.MaxValue, BinaryPresentationMethod.Base64);
}
if (xmlSniffer2 is null)
{
File.Delete("XMPP2.xml");
xmlSniffer2 = new XmlFileSniffer("XMPP2.xml",
@"..\..\..\..\..\Waher.IoTGateway.Resources\Transforms\SnifferXmlToHtml.xslt",
int.MaxValue, BinaryPresentationMethod.Base64);
}
}
public static async Task DisposeSnifferAndLog()
{
if (xmlSniffer1 is not null)
{
await xmlSniffer1.DisposeAsync();
xmlSniffer1 = null;
}
if (xmlSniffer2 is not null)
{
await xmlSniffer2.DisposeAsync();
xmlSniffer2 = null;
}
if (sink is not null)
{
Log.Unregister(sink);
await sink.DisposeAsync();
sink = null;
}
}
public virtual async Task ConnectClients()
{
this.connected1.Reset();
this.error1.Reset();
this.offline1.Reset();
this.connected2.Reset();
this.error2.Reset();
this.offline2.Reset();
this.ex1 = null;
this.ex2 = null;
this.client1 = new XmppClient(this.GetCredentials1(), "en", typeof(CommunicationTests).Assembly)
{
DefaultNrRetries = 2,
DefaultRetryTimeout = 1000,
DefaultMaxRetryTimeout = 5000,
DefaultDropOff = true
};
this.client1.SetTag("ShowE2E", true);
this.client1.Add(new ConsoleOutSniffer(BinaryPresentationMethod.ByteCount, LineEnding.NewLine));
this.client1.Add(xmlSniffer1);
this.client1.OnConnectionError += this.Client_OnConnectionError1;
this.client1.OnError += this.Client_OnError1;
this.client1.OnStateChanged += this.Client_OnStateChanged1;
this.client1.Information("Starting test, client 1...");
this.PrepareClient1(this.client1);
await this.client1.SetPresence(Availability.Chat, new KeyValuePair<string, string>("en", "Live and well"));
await this.client1.Connect();
this.WaitConnected1(5000);
this.client2 = new XmppClient(this.GetCredentials2(), "en", typeof(CommunicationTests).Assembly)
{
DefaultNrRetries = 2,
DefaultRetryTimeout = 1000,
DefaultMaxRetryTimeout = 5000,
DefaultDropOff = true
};
this.client2.SetTag("ShowE2E", true);
this.client2.Add(xmlSniffer2);
this.client2.OnConnectionError += this.Client_OnConnectionError2;
this.client2.OnError += this.Client_OnError2;
this.client2.OnStateChanged += this.Client_OnStateChanged2;
this.client2.Information("Starting test, client 2...");
this.PrepareClient2(this.client2);
await this.client2.SetPresence(Availability.Chat, new KeyValuePair<string, string>("en", "Ready to chat."));
await this.client2.Connect();
this.WaitConnected2(5000);
}
public virtual void PrepareClient1(XmppClient Client)
{
}
public virtual void PrepareClient2(XmppClient Client)
{
}
public virtual XmppCredentials GetCredentials1()
{
return new XmppCredentials()
{
Host = "waher.se",
TrustServer = false,
//Host = "localhost",
//TrustServer = true,
Port = 5222,
Account = "xmppclient.test01",
Password = "testpassword"
};
}
public virtual XmppCredentials GetCredentials2()
{
return new XmppCredentials()
{
Host = "waher.se",
TrustServer = false,
//Host = "localhost",
//TrustServer = true,
Port = 5222,
Account = "xmppclient.test02",
Password = "testpassword"
};
}
private Task Client_OnStateChanged1(object Sender, XmppState NewState)
{
switch (NewState)
{
case XmppState.Connected:
this.connected1.Set();
break;
case XmppState.Error:
this.error1.Set();
break;
case XmppState.Offline:
this.offline1.Set();
break;
case XmppState.Connecting:
break;
}
return Task.CompletedTask;
}
private Task Client_OnStateChanged2(object Sender, XmppState NewState)
{
switch (NewState)
{
case XmppState.Connected:
this.connected2.Set();
break;
case XmppState.Error:
this.error2.Set();
break;
case XmppState.Offline:
this.offline2.Set();
break;
case XmppState.Connecting:
break;
}
return Task.CompletedTask;
}
Task Client_OnError1(object Sender, Exception Exception)
{
this.ex1 = Exception;
return Task.CompletedTask;
}
Task Client_OnError2(object Sender, Exception Exception)
{
this.ex2 = Exception;
return Task.CompletedTask;
}
Task Client_OnConnectionError1(object Sender, Exception Exception)
{
this.ex1 = Exception;
return Task.CompletedTask;
}
Task Client_OnConnectionError2(object Sender, Exception Exception)
{
this.ex2 = Exception;
return Task.CompletedTask;
}
private int Wait1(int Timeout)
{
return WaitHandle.WaitAny([this.connected1, this.error1, this.offline1], Timeout);
}
private int Wait2(int Timeout)
{
return WaitHandle.WaitAny([this.connected2, this.error2, this.offline2], Timeout);
}
private void WaitConnected1(int Timeout)
{
AssertWaitConnected(this.Wait1(Timeout));
Thread.Sleep(100); // Wait for presence to be processed by server.
}
private void WaitConnected2(int Timeout)
{
AssertWaitConnected(this.Wait2(Timeout));
Thread.Sleep(100); // Wait for presence to be processed by server.
}
private static void AssertWaitConnected(int Event)
{
switch (Event)
{
case -1:
case WaitHandle.WaitTimeout:
Assert.Fail("Unable to connect. Timeout occurred.");
break;
case 0: // Connected
break;
case 1:
Assert.Fail("Unable to connect. Error occurred.");
break;
case 2:
Assert.Fail("Unable to connect. Client turned offline.");
break;
}
}
public virtual async Task DisposeClients()
{
await ConsoleOut.FlushAsync();
if (this.client1 is not null)
{
this.client1.Information("Stopping test, client 1...");
await this.client1.OfflineAndDisposeAsync(false);
this.client1 = null;
}
if (this.client2 is not null)
{
this.client2.Information("Stopping test, client 2...");
await this.client2.OfflineAndDisposeAsync(false);
this.client2 = null;
}
if (this.ex1 is not null)
throw new TargetInvocationException(this.ex1);
if (this.ex2 is not null)
throw new TargetInvocationException(this.ex2);
}
}
}