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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
import Foundation
import Ice
import TestCommon
func connect(_ prx: Ice.ObjectPrx) throws -> Ice.Connection {
for _ in 0 ..< 10 {
do {
_ = try prx.ice_getConnection()
break
} catch is Ice.ConnectTimeoutException {
// Can sporadically occur with slow machines
}
}
return try prx.ice_getConnection()!
}
public func allTests(helper: TestHelper) throws {
let controller = try checkedCast(
prx: helper.communicator().stringToProxy("controller:\(helper.getTestEndpoint(num: 1))")!,
type: ControllerPrx.self)!
do {
try allTestsWithController(helper: helper, controller: controller)
} catch {
// Ensure the adapter is not in the holding state when an unexpected exception occurs to prevent
// the test from hanging on exit in case a connection which disables timeouts is still opened.
try controller.resumeAdapter()
throw error
}
}
public func allTestsWithController(helper: TestHelper, controller: ControllerPrx) throws {
func test(_ value: Bool, file: String = #file, line: Int = #line) throws {
try helper.test(value, file: file, line: line)
}
let communicator = helper.communicator()
let sref = "timeout:\(helper.getTestEndpoint(num: 0))"
let obj = try communicator.stringToProxy(sref)!
let timeout = try checkedCast(prx: obj, type: TimeoutPrx.self)!
let output = helper.getWriter()
output.write("testing connect timeout... ")
do {
//
// Expect ConnectTimeoutException.
//
let to = timeout.ice_timeout(100)
try controller.holdAdapter(-1)
do {
try to.op()
try test(false)
} catch is Ice.ConnectTimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
}
do {
//
// Expect success.
//
let to = timeout.ice_timeout(-1)
try controller.holdAdapter(100)
do {
try to.op()
} catch is Ice.ConnectTimeoutException {
try test(false)
}
}
output.writeLine("ok")
// The sequence needs to be large enough to fill the write/recv buffers
let seq = ByteSeq(repeating: 0, count: 2_000_000)
output.write("testing connection timeout... ")
do {
//
// Expect TimeoutException.
//
let to = timeout.ice_timeout(250)
_ = try connect(to)
try controller.holdAdapter(-1)
do {
try to.sendData(seq)
try test(false)
} catch is Ice.TimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
}
do {
//
// Expect success.
//
let to = timeout.ice_timeout(2000)
try controller.holdAdapter(100)
do {
try to.sendData(ByteSeq(repeating: 0, count: 1_000_000))
} catch is Ice.TimeoutException {
try test(false)
}
}
output.writeLine("ok")
output.write("testing invocation timeout... ")
do {
let connection = try obj.ice_getConnection()
var to = timeout.ice_invocationTimeout(100)
try test(connection === to.ice_getConnection())
do {
try to.sleep(1000)
try test(false)
} catch is Ice.InvocationTimeoutException {}
try obj.ice_ping()
to = timeout.ice_invocationTimeout(1000)
try test(connection === to.ice_getConnection())
do {
try to.sleep(100)
} catch is Ice.InvocationTimeoutException {
try test(false)
}
try test(connection === to.ice_getConnection())
}
do {
//
// Expect InvocationTimeoutException.
//
let to = timeout.ice_invocationTimeout(100)
do {
try to.sleepAsync(500).wait()
try test(false)
} catch is Ice.InvocationTimeoutException {}
try timeout.ice_ping()
}
do {
//
// Expect success.
//
let to = timeout.ice_invocationTimeout(1000)
do {
try to.sleepAsync(100).wait()
} catch {
try test(false)
}
}
do {
//
// Backward compatible connection timeouts
//
let to = timeout.ice_invocationTimeout(-2).ice_timeout(250)
var con = try connect(to)
do {
try to.sleep(750)
try test(false)
} catch is Ice.TimeoutException {
do {
_ = try con.getInfo()
try test(false)
} catch is Ice.TimeoutException {
// Connection got closed as well.
}
}
try timeout.ice_ping()
do {
con = try connect(to)
try to.sleepAsync(750).wait()
try test(false)
} catch is Ice.TimeoutException {
do {
_ = try con.getInfo()
try test(false)
} catch is Ice.TimeoutException {
// Connection got closed as well.
}
}
try obj.ice_ping()
}
output.writeLine("ok")
output.write("testing close timeout... ")
do {
let to = timeout.ice_timeout(250)
let connection = try connect(to)
try controller.holdAdapter(-1)
try connection.close(.GracefullyWithWait)
do {
_ = try connection.getInfo() // getInfo() doesn't throw in the closing state.
} catch is Ice.LocalException {
try test(false)
}
while true {
do {
_ = try connection.getInfo()
Thread.sleep(forTimeInterval: 0.01)
} catch let ex as Ice.ConnectionManuallyClosedException {
// Expected.
try test(ex.graceful)
// swiftlint:disable unneeded_break_in_switch
break
}
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
}
output.writeLine("ok")
output.write("testing timeout overrides... ")
do {
//
// Test Ice.Override.Timeout. This property overrides all
// endpoint timeouts.
//
let properties = communicator.getProperties().clone()
properties.setProperty(key: "Ice.Override.ConnectTimeout", value: "250")
properties.setProperty(key: "Ice.Override.Timeout", value: "100")
var initData = Ice.InitializationData()
initData.properties = properties
let comm = try helper.initialize(initData)
var to = try uncheckedCast(prx: comm.stringToProxy(sref)!, type: TimeoutPrx.self)
_ = try connect(to)
try controller.holdAdapter(-1)
do {
try to.sendData(seq)
try test(false)
} catch is Ice.TimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
//
// Calling ice_timeout() should have no effect.
//
to = to.ice_timeout(1000)
_ = try connect(to)
try controller.holdAdapter(-1)
do {
try to.sendData(seq)
try test(false)
} catch is Ice.TimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
comm.destroy()
}
do {
//
// Test Ice.Override.ConnectTimeout.
//
let properties = communicator.getProperties().clone()
properties.setProperty(key: "Ice.Override.ConnectTimeout", value: "250")
var initData = Ice.InitializationData()
initData.properties = properties
let comm = try helper.initialize(initData)
try controller.holdAdapter(-1)
var to = try uncheckedCast(prx: comm.stringToProxy(sref)!, type: TimeoutPrx.self)
do {
try to.op()
try test(false)
} catch is Ice.ConnectTimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
//
// Calling ice_timeout() should have no effect on the connect timeout.
//
try controller.holdAdapter(-1)
to = to.ice_timeout(1000)
do {
try to.op()
try test(false)
} catch is Ice.ConnectTimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
//
// Verify that timeout set via ice_timeout() is still used for requests.
//
to = to.ice_timeout(250)
_ = try connect(to)
try controller.holdAdapter(-1)
do {
try to.sendData(seq)
try test(false)
} catch is Ice.TimeoutException {
// Expected.
}
try controller.resumeAdapter()
try timeout.op() // Ensure adapter is active.
comm.destroy()
}
do {
//
// Test Ice.Override.CloseTimeout.
//
let properties = communicator.getProperties().clone()
properties.setProperty(key: "Ice.Override.CloseTimeout", value: "100")
var initData = Ice.InitializationData()
initData.properties = properties
let comm = try helper.initialize(initData)
_ = try comm.stringToProxy(sref)!.ice_getConnection()
try controller.holdAdapter(-1)
let begin = DispatchTime.now()
comm.destroy()
let elapsed = DispatchTime.now().uptimeNanoseconds - begin.uptimeNanoseconds
try test((elapsed / 1_000_000) < 1000)
try controller.resumeAdapter()
}
output.writeLine("ok")
output.write("testing invocation timeouts with collocated calls... ")
do {
communicator.getProperties().setProperty(key: "TimeoutCollocated.AdapterId", value: "timeoutAdapter")
let adapter = try communicator.createObjectAdapter("TimeoutCollocated")
try adapter.activate()
let proxy = try uncheckedCast(prx: adapter.addWithUUID(TimeoutDisp(TimeoutI())),
type: TimeoutPrx.self).ice_invocationTimeout(100)
do {
try proxy.sleep(500)
try test(false)
} catch is Ice.InvocationTimeoutException {}
do {
try proxy.sleepAsync(500).wait()
try test(false)
} catch is Ice.InvocationTimeoutException {}
do {
try proxy.ice_invocationTimeout(-2).ice_ping()
try proxy.ice_invocationTimeout(-2).ice_pingAsync().wait()
} catch is Ice.Exception {
try test(false)
}
let batchTimeout = proxy.ice_batchOneway()
try batchTimeout.ice_ping()
try batchTimeout.ice_ping()
try batchTimeout.ice_ping()
_ = proxy.ice_invocationTimeout(-1).sleepAsync(500) // Keep the server thread pool busy.
do {
try batchTimeout.ice_flushBatchRequestsAsync().wait()
try test(false)
} catch is Ice.InvocationTimeoutException {}
adapter.destroy()
}
output.writeLine("ok")
try controller.shutdown()
}
|