-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathXmppComponentTests.cs
236 lines (197 loc) · 5.64 KB
/
XmppComponentTests.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
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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Waher.Events;
using Waher.Events.Console;
using Waher.Networking.Sniffers;
namespace Waher.Networking.XMPP.Test
{
[TestClass]
public class XmppComponentTests
{
private static ConsoleEventSink sink = null;
protected AutoResetEvent clientConnected = new(false);
protected AutoResetEvent clientError = new(false);
protected AutoResetEvent clientOffline = new(false);
protected AutoResetEvent componentConnected = new(false);
protected AutoResetEvent componentError = new(false);
protected AutoResetEvent componentOffline = new(false);
protected XmppClient client;
protected XmppComponent component;
protected Exception clientEx = null;
protected Exception componentEx = null;
public XmppComponentTests()
{
}
[ClassInitialize]
public static void ClassInitialize(TestContext _)
{
sink = new ConsoleEventSink();
Log.Register(sink);
}
[ClassCleanup]
public static async Task ClassCleanup()
{
if (sink is not null)
{
Log.Unregister(sink);
await sink.DisposeAsync();
sink = null;
}
}
[TestInitialize]
public virtual void Setup()
{
this.clientConnected.Reset();
this.clientError.Reset();
this.clientOffline.Reset();
this.componentConnected.Reset();
this.componentError.Reset();
this.componentOffline.Reset();
this.clientEx = null;
this.componentEx = null;
this.client = new XmppClient("localhost", 5222, "testuser", "testpass", "en", typeof(CommunicationTests).Assembly)
{
TrustServer = true,
DefaultNrRetries = 2,
DefaultRetryTimeout = 1000,
DefaultMaxRetryTimeout = 5000,
DefaultDropOff = true
};
//this.client.Add(new ConsoleOutSniffer(BinaryPresentationMethod.ByteCount));
this.client.SetTag("ShowE2E", true);
this.client.OnConnectionError += this.Client_OnConnectionError;
this.client.OnError += this.Client_OnError;
this.client.OnStateChanged += this.Client_OnStateChanged;
this.client.SetPresence(Availability.Chat, new KeyValuePair<string, string>("en", "Live and well"));
this.client.Connect();
this.component = new XmppComponent("localhost", 5275, "provisioning.peterwaher-hp14", "provisioning", "collaboration", "provisioning", "Provisioning service")
{
DefaultNrRetries = 2,
DefaultRetryTimeout = 1000,
DefaultMaxRetryTimeout = 5000,
DefaultDropOff = true
};
this.component.Add(new ConsoleOutSniffer(BinaryPresentationMethod.ByteCount, LineEnding.NewLine));
this.component.OnConnectionError += this.Component_OnConnectionError;
this.component.OnError += this.Component_OnError;
this.component.OnStateChanged += this.Component_OnStateChanged;
this.WaitConnected(10000);
}
private Task Client_OnStateChanged(object Sender, XmppState NewState)
{
switch (NewState)
{
case XmppState.Connected:
this.clientEx = null;
this.clientConnected.Set();
break;
case XmppState.Error:
this.clientError.Set();
break;
case XmppState.Offline:
this.clientOffline.Set();
break;
}
return Task.CompletedTask;
}
private Task Component_OnStateChanged(object Sender, XmppState NewState)
{
switch (NewState)
{
case XmppState.Connected:
this.componentConnected.Set();
break;
case XmppState.Error:
this.componentError.Set();
break;
case XmppState.Offline:
this.componentOffline.Set();
break;
}
return Task.CompletedTask;
}
Task Client_OnError(object Sender, Exception Exception)
{
this.clientEx = Exception;
return Task.CompletedTask;
}
Task Component_OnError(object Sender, Exception Exception)
{
this.componentEx = Exception;
return Task.CompletedTask;
}
Task Client_OnConnectionError(object Sender, Exception Exception)
{
this.clientEx = Exception;
return Task.CompletedTask;
}
Task Component_OnConnectionError(object Sender, Exception Exception)
{
this.componentEx = Exception;
return Task.CompletedTask;
}
private int WaitClient(int Timeout)
{
return WaitHandle.WaitAny(new WaitHandle[] { this.clientConnected, this.clientError, this.clientOffline }, Timeout);
}
private int WaitComponent(int Timeout)
{
return WaitHandle.WaitAny(new WaitHandle[] { this.componentConnected, this.componentError, this.componentOffline }, Timeout);
}
private void WaitClientConnected(int Timeout)
{
AssertWaitConnected(this.WaitClient(Timeout));
}
private void WaitComponentConnected(int Timeout)
{
AssertWaitConnected(this.WaitComponent(Timeout));
}
private void WaitConnected(int Timeout)
{
this.WaitClientConnected(Timeout);
this.WaitComponentConnected(Timeout);
}
private static void AssertWaitConnected(int Event)
{
switch (Event)
{
case -1:
Assert.Fail("Unable to connect. Timeout occurred.");
break;
case 1:
Assert.Fail("Unable to connect. Error occurred.");
break;
case 2:
Assert.Fail("Unable to connect. Client turned offline.");
break;
}
}
[TestCleanup]
public virtual async Task TearDown()
{
if (this.client is not null)
{
await this.client.OfflineAndDisposeAsync(false);
this.client = null;
}
if (this.component is not null)
{
await this.component.DisposeAsync();
this.component = null;
}
if (this.clientEx is not null)
throw new TargetInvocationException(this.clientEx);
if (this.componentEx is not null)
throw new TargetInvocationException(this.componentEx);
}
[TestMethod]
[Ignore]
public void Component_Test_01_Connect()
{
}
}
}