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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
using Test;
public class AllTests : Test.AllTests
{
public class Progress : IProgress<bool>
{
public Progress()
{
}
public bool getResult()
{
return _sentSynchronously;
}
public void Report(bool sentSynchronously)
{
_sentSynchronously = sentSynchronously;
}
bool _sentSynchronously = false;
}
private class Callback
{
internal Callback(System.IO.TextWriter output)
{
_called = false;
_output = output;
}
public void check()
{
lock(this)
{
while(!_called)
{
System.Threading.Monitor.Wait(this);
}
_called = false;
}
}
public void response()
{
test(Dispatcher.isDispatcherThread());
called();
}
public void exception(Ice.Exception ex)
{
if(!(ex is Ice.NoEndpointException))
{
_output.WriteLine(ex.ToString());
test(false);
}
test(Dispatcher.isDispatcherThread());
called();
}
public void payload()
{
test(Dispatcher.isDispatcherThread());
}
public void ignoreEx(Ice.Exception ex)
{
if(!(ex is Ice.CommunicatorDestroyedException))
{
_output.WriteLine(ex.ToString());
test(false);
}
}
public void sent(bool sentSynchronously)
{
test(sentSynchronously || Dispatcher.isDispatcherThread());
}
public void called()
{
lock(this)
{
Debug.Assert(!_called);
_called = true;
System.Threading.Monitor.Pulse(this);
}
}
private bool _called;
private System.IO.TextWriter _output;
}
public static void allTests(Test.TestHelper helper)
{
var output = helper.getWriter();
Ice.Communicator communicator = helper.communicator();
string sref = "test:" + helper.getTestEndpoint(0);
Ice.ObjectPrx obj = communicator.stringToProxy(sref);
test(obj != null);
Test.TestIntfPrx p = Test.TestIntfPrxHelper.uncheckedCast(obj);
sref = "testController:" + helper.getTestEndpoint(1);
obj = communicator.stringToProxy(sref);
test(obj != null);
Test.TestIntfControllerPrx testController = Test.TestIntfControllerPrxHelper.uncheckedCast(obj);
output.Write("testing dispatcher... ");
output.Flush();
{
p.op();
Callback cb = new Callback(output);
p.begin_op().whenCompleted(cb.response, cb.exception);
cb.check();
TestIntfPrx i = (TestIntfPrx)p.ice_adapterId("dummy");
i.begin_op().whenCompleted(cb.exception);
cb.check();
//
// Expect InvocationTimeoutException.
//
{
Test.TestIntfPrx to = Test.TestIntfPrxHelper.uncheckedCast(p.ice_invocationTimeout(10));
to.begin_sleep(500).whenCompleted(
() =>
{
test(false);
},
(Ice.Exception ex) => {
test(ex is Ice.InvocationTimeoutException);
test(Dispatcher.isDispatcherThread());
cb.called();
});
cb.check();
}
testController.holdAdapter();
Test.Callback_TestIntf_opWithPayload resp = cb.payload;
Ice.ExceptionCallback excb = cb.ignoreEx;
Ice.SentCallback scb = cb.sent;
byte[] seq = new byte[10 * 1024];
(new System.Random()).NextBytes(seq);
Ice.AsyncResult r;
while((r = p.begin_opWithPayload(seq).whenCompleted(resp, excb).whenSent(scb)).sentSynchronously());
testController.resumeAdapter();
r.waitForCompleted();
}
output.WriteLine("ok");
output.Write("testing dispatcher with continuations... ");
output.Flush();
{
p.op();
Callback cb = new Callback(output);
System.Action<Task> continuation = (Task previous) =>
{
try
{
previous.Wait();
cb.response();
}
catch(System.AggregateException ex)
{
cb.exception((Ice.Exception)ex.InnerException);
}
};
// We use sleepAsync instead of opAsync to ensure the response isn't received before
// we setup the continuation
var t = p.sleepAsync(500).ContinueWith(continuation, TaskContinuationOptions.ExecuteSynchronously);
t.Wait();
cb.check();
var i = (TestIntfPrx)p.ice_adapterId("dummy");
//
// sleepAsync doesn't help here as the test will fail with Ice.NoEndpointException and sleepAsync
// will not be called.
//
//i.sleepAsync(500).ContinueWith(continuation, TaskContinuationOptions.ExecuteSynchronously).Wait();
//cb.check();
//
// Expect InvocationTimeoutException.
//
{
// The continuation might be (rarely) executed on the current thread if the setup of the
// continuation occurs after the invocation timeout.
var thread = Thread.CurrentThread;
Test.TestIntfPrx to = Test.TestIntfPrxHelper.uncheckedCast(p.ice_invocationTimeout(20));
to.sleepAsync(500).ContinueWith(
previous =>
{
try
{
previous.Wait();
test(false);
}
catch(System.AggregateException ex)
{
test(ex.InnerException is Ice.InvocationTimeoutException);
test(Dispatcher.isDispatcherThread() || thread == Thread.CurrentThread);
}
}, TaskContinuationOptions.ExecuteSynchronously).Wait();
}
//
// Repeat using the proxy scheduler in this case we don't need to call sleepAsync, continuations
// are waranted to run with the dispatcher even if not executed synchronously.
//
t = p.opAsync().ContinueWith(continuation, p.ice_scheduler());
t.Wait();
cb.check();
i.opAsync().ContinueWith(continuation, i.ice_scheduler()).Wait();
cb.check();
//
// Expect InvocationTimeoutException.
//
{
Test.TestIntfPrx to = Test.TestIntfPrxHelper.uncheckedCast(p.ice_invocationTimeout(10));
to.sleepAsync(500).ContinueWith(
previous =>
{
try
{
previous.Wait();
test(false);
}
catch(System.AggregateException ex)
{
test(ex.InnerException is Ice.InvocationTimeoutException);
test(Dispatcher.isDispatcherThread());
}
}, p.ice_scheduler()).Wait();
}
//
// Hold adapter to ensure the invocations don't complete synchronously
// Also disable collocation optimization on p
//
testController.holdAdapter();
var p2 = Test.TestIntfPrxHelper.uncheckedCast(p.ice_collocationOptimized(false));
System.Action<Task> continuation2 = (Task previous) =>
{
test(Dispatcher.isDispatcherThread());
try
{
previous.Wait();
}
catch(System.AggregateException ex)
{
test(ex.InnerException is Ice.CommunicatorDestroyedException);
}
};
byte[] seq = new byte[10 * 1024];
(new System.Random()).NextBytes(seq);
Progress sentSynchronously;
do
{
sentSynchronously = new Progress();
t = p2.opWithPayloadAsync(seq, progress: sentSynchronously).ContinueWith(
continuation2,
TaskContinuationOptions.ExecuteSynchronously);
}
while(sentSynchronously.getResult());
testController.resumeAdapter();
t.Wait();
}
output.WriteLine("ok");
output.Write("testing dispatcher with async/await... ");
output.Flush();
{
TaskCompletionSource<object> t = new TaskCompletionSource<object>();
p.opAsync().ContinueWith(async previous => // Execute the code below from the Ice client thread pool
{
try
{
await p.opAsync();
test(Dispatcher.isDispatcherThread());
try
{
TestIntfPrx i = (TestIntfPrx)p.ice_adapterId("dummy");
await i.opAsync();
test(false);
}
catch(Exception)
{
test(Dispatcher.isDispatcherThread());
}
Test.TestIntfPrx to = Test.TestIntfPrxHelper.uncheckedCast(p.ice_invocationTimeout(10));
try
{
await to.sleepAsync(500);
test(false);
}
catch(Ice.InvocationTimeoutException)
{
test(Dispatcher.isDispatcherThread());
}
t.SetResult(null);
}
catch(Exception ex)
{
t.SetException(ex);
}
}, p.ice_scheduler());
t.Task.Wait();
}
output.WriteLine("ok");
p.shutdown();
}
}
|