-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ObjectAdapter use to install middleware in Swift
- Loading branch information
1 parent
5d5b650
commit 43757b2
Showing
7 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) ZeroC, Inc. | ||
|
||
import Ice | ||
import PromiseKit | ||
import TestCommon | ||
|
||
func allTests(_ helper: TestHelper) 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 output = helper.getWriter() | ||
|
||
try testMiddlewareExecutionOrder(communicator, output) | ||
|
||
// Verifies the middleware execute in installation order. | ||
func testMiddlewareExecutionOrder(_ communicator: Communicator, _ output: TextWriter) throws { | ||
output.write("testing middleware execution order... ") | ||
|
||
// Arrange | ||
let oa = try communicator.createObjectAdapterWithEndpoints(name: "MyOA", endpoints: "tcp -h 127.0.0.1 -p 0") | ||
let log = MiddlewareLog() | ||
|
||
let objPrx = try oa.add(servant: MyObjectDisp(MyObjectI()), id: Ice.stringToIdentity("test")) | ||
try oa.use { next in | ||
Middleware(next, "A", log) | ||
}.use { next in | ||
Middleware(next, "B", log) | ||
}.use { next in | ||
Middleware(next, "C", log) | ||
} | ||
|
||
// We're actually using colloc so no need to activate oa. | ||
|
||
let p = uncheckedCast(prx: objPrx, type: MyObjectPrx.self) | ||
|
||
// Act | ||
try p.ice_ping() | ||
|
||
// Assert | ||
try test(log.inLog == ["A", "B", "C"]) | ||
try test(log.outLog == ["C", "B", "A"]) | ||
|
||
output.writeLine("ok") | ||
oa.deactivate() | ||
} | ||
|
||
final class Middleware: Dispatcher { | ||
private let next: Dispatcher | ||
private let name: String | ||
private var log: MiddlewareLog | ||
|
||
func dispatch(_ request: IncomingRequest) -> Promise<OutgoingResponse> { | ||
log.inLog.append(name) | ||
|
||
return next.dispatch(request).map { response in | ||
self.log.outLog.append(self.name) | ||
return response | ||
} | ||
} | ||
|
||
init(_ next: Dispatcher, _ name: String, _ log: MiddlewareLog) { | ||
self.next = next | ||
self.name = name | ||
self.log = log | ||
} | ||
} | ||
|
||
final class MiddlewareLog { | ||
var inLog: [String] | ||
var outLog: [String] | ||
|
||
init() { | ||
inLog = [] | ||
outLog = [] | ||
} | ||
} | ||
|
||
final class MyObjectI: MyObject { | ||
func getName(current: Ice.Current) throws -> String { | ||
"Foo" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) ZeroC, Inc. | ||
|
||
import Ice | ||
import TestCommon | ||
|
||
class Client: TestHelperI { | ||
override public func run(args: [String]) throws { | ||
let communicator = try initialize(args) | ||
defer { | ||
communicator.destroy() | ||
} | ||
try allTests(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) ZeroC, Inc. | ||
#pragma once | ||
|
||
module Test | ||
{ | ||
interface MyObject | ||
{ | ||
string getName(); | ||
} | ||
} |