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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
import Ice
import PromiseKit
import TestCommon
enum InterceptorError: Error {
case retry
}
class InterceptorI: Disp {
public private(set) var servantDisp: Disp
public private(set) var lastOperation: String?
public private(set) var lastStatus: Bool = false
init(_ servantDisp: Disp) {
self.servantDisp = servantDisp
}
func dispatch(request: Request, current: Current) throws -> Promise<Ice.OutputStream>? {
if let context = current.ctx["raiseBeforeDispatch"] {
if context == "user" {
throw InvalidInputException()
} else if context == "notExist" {
throw ObjectNotExistException()
}
}
lastOperation = current.operation
if lastOperation == "addWithRetry" || lastOperation == "amdAddWithRetry" {
for _ in 0 ..< 10 {
do {
if let p = try servantDisp.dispatch(request: request, current: current) {
guard let error = p.error,
let errorType = error as? InterceptorError,
errorType == .retry
else {
fatalError("Expected an error")
}
} else {
fatalError("Expected an error")
}
} catch InterceptorError.retry {
//
// Expected, retry
//
}
}
current.ctx["retry"] = "no"
} else if current.ctx["retry"] == "yes" {
_ = try servantDisp.dispatch(request: request, current: current)
_ = try servantDisp.dispatch(request: request, current: current)
}
let p = try servantDisp.dispatch(request: request, current: current)
lastStatus = p != nil
if let context = current.ctx["raiseAfterDispatch"] {
if context == "user" {
throw InvalidInputException()
} else if context == "notExist" {
throw ObjectNotExistException()
}
}
return p
}
public func clear() {
lastOperation = nil
lastStatus = false
}
}
class MyObjectI: MyObject {
func add(x: Int32, y: Int32, current _: Current) throws -> Int32 {
return x + y
}
func addWithRetry(x: Int32, y: Int32, current: Current) throws -> Int32 {
guard current.ctx["retry"] == "no" else {
throw InterceptorError.retry
}
return x + y
}
func badAdd(x _: Int32, y _: Int32, current _: Current) throws -> Int32 {
throw InvalidInputException()
}
func notExistAdd(x _: Int32, y _: Int32, current _: Current) throws -> Int32 {
throw ObjectNotExistException()
}
func amdAddAsync(x: Int32, y: Int32, current _: Current) -> Promise<Int32> {
return Promise<Int32> { seal in
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(1000)) {
seal.fulfill(x + y)
}
}
}
func amdAddWithRetryAsync(x: Int32, y: Int32, current: Current) -> Promise<Int32> {
guard current.ctx["retry"] == "no" else {
return Promise(error: InterceptorError.retry)
}
return Promise<Int32> { seal in
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(1000)) {
seal.fulfill(x + y)
}
}
}
func amdBadAddAsync(x _: Int32, y _: Int32, current _: Current) -> Promise<Int32> {
return Promise<Int32> { seal in
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(1000)) {
seal.reject(InvalidInputException())
}
}
}
func amdNotExistAddAsync(x _: Int32, y _: Int32, current _: Current) -> Promise<Int32> {
return Promise<Int32> { seal in
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(1000)) {
seal.reject(ObjectNotExistException())
}
}
}
}
public class Client: TestHelperI {
private func runTest(prx: MyObjectPrx, interceptor: InterceptorI) throws {
do {
let out = getWriter()
out.write("testing simple interceptor... ")
try test(interceptor.lastOperation == nil)
try test(!interceptor.lastStatus)
try prx.ice_ping()
try test(interceptor.lastOperation == "ice_ping")
try test(!interceptor.lastStatus)
let typeId = try prx.ice_id()
try test(interceptor.lastOperation == "ice_id")
try test(!interceptor.lastStatus)
try test(prx.ice_isA(id: typeId))
try test(interceptor.lastOperation == "ice_isA")
try test(!interceptor.lastStatus)
try test(prx.add(x: 33, y: 12) == 45)
try test(interceptor.lastOperation == "add")
try test(!interceptor.lastStatus)
out.writeLine("ok")
out.write("testing retry... ")
try test(prx.addWithRetry(x: 33, y: 12) == 45)
try test(interceptor.lastOperation == "addWithRetry")
try test(!interceptor.lastStatus)
out.writeLine("ok")
out.write("testing user exception... ")
do {
_ = try prx.badAdd(x: 33, y: 12)
try test(false)
} catch is InvalidInputException {
// Expected
}
try test(interceptor.lastOperation == "badAdd")
try test(!interceptor.lastStatus)
out.writeLine("ok")
out.write("testing ONE... ")
do {
_ = try prx.notExistAdd(x: 33, y: 12)
try test(false)
} catch is ObjectNotExistException {
// Expected
}
try test(interceptor.lastOperation == "notExistAdd")
try test(!interceptor.lastStatus)
out.writeLine("ok")
out.write("testing exceptions raised by the interceptor... ")
try testInterceptorExceptions(prx)
out.writeLine("ok")
}
}
private func runAmdTest(prx: MyObjectPrx, interceptor: InterceptorI, out: TextWriter) throws {
do {
out.write("testing simple interceptor... ")
try test(interceptor.lastOperation == nil)
try test(!interceptor.lastStatus)
try test(prx.amdAdd(x: 33, y: 12) == 45)
try test(interceptor.lastOperation == "amdAdd")
try test(interceptor.lastStatus)
out.writeLine("ok")
out.write("testing retry... ")
try test(prx.amdAddWithRetry(x: 33, y: 12) == 45)
try test(interceptor.lastOperation == "amdAddWithRetry")
try test(interceptor.lastStatus)
let ctx = ["retry": "yes"]
for _ in 0 ..< 10 {
try test(prx.amdAdd(x: 33, y: 12, context: ctx) == 45)
try test(interceptor.lastOperation == "amdAdd")
try test(interceptor.lastStatus)
}
out.writeLine("ok")
out.write("testing user exception...")
do {
_ = try prx.amdBadAdd(x: 33, y: 12)
try test(false)
} catch is InvalidInputException {
// Expected
}
try test(interceptor.lastOperation == "amdBadAdd")
try test(interceptor.lastStatus)
out.writeLine("ok")
out.write("testing ONE... ")
interceptor.clear()
do {
_ = try prx.amdNotExistAdd(x: 33, y: 12)
try test(false)
} catch is ObjectNotExistException {
// Expected
}
try test(interceptor.lastOperation == "amdNotExistAdd")
try test(interceptor.lastStatus)
out.writeLine("ok")
out.write("testing exceptions raised by the interceptor... ")
try testInterceptorExceptions(prx)
out.writeLine("ok")
}
}
public override func run(args: [String]) throws {
let properties = try createTestProperties(args)
properties.setProperty(key: "Ice.Warn.Dispatch", value: "0")
let communicator = try initialize(properties)
defer {
communicator.destroy()
}
communicator.getProperties().setProperty(key: "MyOA.AdapterId", value: "myOA")
let oa = try communicator.createObjectAdapterWithEndpoints(name: "MyOA2", endpoints: "tcp -h localhost")
let interceptor = InterceptorI(MyObjectDisp(MyObjectI()))
var prx = uncheckedCast(prx: try oa.addWithUUID(interceptor), type: MyObjectPrx.self)
let out = getWriter()
out.writeLine("Collocation optimization on")
try runTest(prx: prx, interceptor: interceptor)
out.writeLine("Now with AMD")
interceptor.clear()
try runAmdTest(prx: prx, interceptor: interceptor, out: out)
try oa.activate() // Only necessary for non-collocation optimized tests
out.writeLine("Collocation optimization off")
interceptor.clear()
prx = uncheckedCast(prx: prx.ice_collocationOptimized(false), type: MyObjectPrx.self)
try runTest(prx: prx, interceptor: interceptor)
out.writeLine("Now with AMD")
interceptor.clear()
try runAmdTest(prx: prx, interceptor: interceptor, out: out)
}
private func testInterceptorExceptions(_ prx: MyObjectPrx) throws {
let exceptions: [(point: String, exception: String)] = [
("raiseBeforeDispatch", "user"),
("raiseBeforeDispatch", "notExist"),
("raiseAfterDispatch", "user"),
("raiseAfterDispatch", "notExist")
]
for e in exceptions {
let ctx: Context = [e.point: e.exception]
do {
try prx.ice_ping(context: ctx)
try test(false)
} catch is UnknownUserException {
try test(e.exception == "user")
} catch is ObjectNotExistException {
try test(e.exception == "notExist")
}
let batch = prx.ice_batchOneway()
try batch.ice_ping(context: ctx)
try batch.ice_ping()
try batch.ice_flushBatchRequests()
// Force the last batch request to be dispatched by the server thread using invocation timeouts
// This is required to prevent threading issue with the test interceptor implementation which
// isn't thread safe
try prx.ice_invocationTimeout(10000).ice_ping()
}
}
}
|