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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
import Foundation
import Ice
public func withLock<T>(_ lock: os_unfair_lock_t, block: () throws -> T) rethrows -> T {
os_unfair_lock_lock(lock)
defer {
os_unfair_lock_unlock(lock)
}
return try block()
}
public protocol TextWriter {
func write(_ data: String)
func writeLine(_ data: String)
}
public protocol ControllerHelper {
func loggerPrefix() -> String
func serverReady()
func communicatorInitialized(communicator: Ice.Communicator)
}
class StdoutWriter: TextWriter {
public func write(_ data: String) {
fputs(data, stdout)
fflush(stdout)
}
public func writeLine(_ data: String) {
print(data)
fflush(stdout)
}
}
public enum TestFailed: Error {
case testFailed
}
public protocol TestHelper {
init()
func run(args: [String]) throws
func getTestEndpoint(num: Int32, prot: String) -> String
func getTestEndpoint(properties: Ice.Properties, num: Int32, prot: String) -> String
func getTestHost() -> String
func getTestHost(properties: Ice.Properties) -> String
func getTestProtocol() -> String
func getTestProtocol(properties: Ice.Properties) -> String
func getTestPort(num: Int32) -> Int32
func getTestPort(properties: Ice.Properties, num: Int32) -> Int32
func createTestProperties(_ args: [String]) throws -> Ice.Properties
func createTestProperties(_ args: inout [String]) throws -> Ice.Properties
func initialize(_ args: [String]) throws -> Ice.Communicator
func initialize(_ properties: Ice.Properties) throws -> Ice.Communicator
func initialize(_ initData: Ice.InitializationData) throws -> Ice.Communicator
func test(_ value: Bool, file: String, line: Int) throws
func getWriter() -> TextWriter
func setWriter(writer: TextWriter)
func communicator() -> Ice.Communicator
func setControllerHelper(controllerHelper: ControllerHelper)
}
public extension TestHelper {
func getTestEndpoint(num: Int32 = 0, prot: String = "") -> String {
return getTestEndpoint(properties: communicator().getProperties(),
num: num,
prot: prot)
}
func test(_ value: Bool, file: String = #file, line: Int = #line) throws {
if !value {
let writer = getWriter()
writer.writeLine("Test failed File: \(file): Line: \(line)")
throw TestFailed.testFailed
}
}
}
open class TestHelperI: TestHelper {
private var _controllerHelper: ControllerHelper!
private var _communicator: Ice.Communicator!
private var _writer: TextWriter!
public required init() {}
open func run(args _: [String]) throws {
print("Subclass has not implemented abstract method `run`!")
abort()
}
public func getTestEndpoint(properties: Ice.Properties, num: Int32 = 0, prot: String = "") -> String {
var s = ""
s += (prot == "") ? properties.getPropertyWithDefault(key: "Ice.Default.Protocol", value: "default") : prot
s += " -p "
let port = properties.getPropertyAsIntWithDefault(key: "Test.BasePort", value: 12010) + num
s += String(port)
return s
}
public func getTestHost() -> String {
return getTestHost(properties: _communicator!.getProperties())
}
public func getTestHost(properties: Ice.Properties) -> String {
return properties.getPropertyWithDefault(key: "Ice.Default.Host", value: "127.0.0.1")
}
public func getTestProtocol() -> String {
return getTestProtocol(properties: _communicator!.getProperties())
}
public func getTestProtocol(properties: Ice.Properties) -> String {
return properties.getPropertyWithDefault(key: "Ice.Default.Protocol", value: "tcp")
}
public func getTestPort(num: Int32) -> Int32 {
return getTestPort(properties: _communicator.getProperties(), num: num)
}
public func getTestPort(properties: Ice.Properties, num: Int32) -> Int32 {
return properties.getPropertyAsIntWithDefault(key: "Test.BasePort", value: 12010) + num
}
public func createTestProperties(_ args: [String]) throws -> Ice.Properties {
var remainingArgs = args
let properties = try Ice.createProperties(&remainingArgs)
remainingArgs = try properties.parseCommandLineOptions(prefix: "Test", options: remainingArgs)
return properties
}
public func createTestProperties(_ args: inout [String]) throws -> Ice.Properties {
let properties = try Ice.createProperties(&args)
args = try properties.parseCommandLineOptions(prefix: "Test", options: args)
return properties
}
public func initialize(_ args: [String]) throws -> Ice.Communicator {
var initData = Ice.InitializationData()
let props = try createTestProperties(args)
initData.properties = props
let communicator = try initialize(initData)
return communicator
}
public func initialize(_ properties: Ice.Properties) throws -> Ice.Communicator {
var initData = Ice.InitializationData()
initData.properties = properties
return try initialize(initData)
}
public func initialize(_ initData: Ice.InitializationData) throws -> Ice.Communicator {
let communicator = try Ice.initialize(initData)
if _communicator == nil {
_communicator = communicator
}
if _controllerHelper != nil {
_controllerHelper.communicatorInitialized(communicator: _communicator)
}
return communicator
}
public func getWriter() -> TextWriter {
if _writer == nil {
_writer = StdoutWriter()
}
return _writer
}
public func setWriter(writer: TextWriter) {
_writer = writer
}
public func communicator() -> Communicator {
return _communicator
}
public func serverReady() {
if _controllerHelper != nil {
_controllerHelper.serverReady()
}
}
public func setControllerHelper(controllerHelper: ControllerHelper) {
precondition(_controllerHelper == nil)
_controllerHelper = controllerHelper
}
}
|