summaryrefslogtreecommitdiff
path: root/swift/test/TestDriver/iOS/ControllerI.swift
blob: eae08d6fde87b87c78b43fb0f7864574ec0ccbb3 (plain)
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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

import Foundation
import Ice
import TestCommon

class ProcessI: CommonProcess {
    var _helper: ControllerHelperI

    init(helper: ControllerHelperI) {
        _helper = helper
    }

    func waitReady(timeout: Int32, current _: Ice.Current) throws {
        try _helper.waitReady(timeout: timeout)
    }

    func waitSuccess(timeout: Int32, current _: Ice.Current) throws -> Int32 {
        return try _helper.waitSuccess(timeout: timeout)
    }

    func terminate(current: Ice.Current) throws -> String {
        _helper.shutdown()
        guard let adapter = current.adapter else {
            precondition(false)
        }
        try adapter.remove(current.id)
        return _helper.getOutput()
    }
}

class ProcessControllerI: CommonProcessController {
    var _view: ViewController
    var _ipv4: String
    var _ipv6: String

    var _bundleName: String = ""

    //
    // Run each process in its own queue
    //
    var _serverDispatchQueue: DispatchQueue
    var _clientDispatchQueue: DispatchQueue

    init(view: ViewController, ipv4: String, ipv6: String) {
        _view = view
        _ipv4 = ipv4
        _ipv6 = ipv6

        _serverDispatchQueue = DispatchQueue(label: "Server", qos: .userInitiated)
        _clientDispatchQueue = DispatchQueue(label: "Client", qos: .userInitiated)
    }

    func start(testsuite: String,
               exe: String,
               args: CommonStringSeq,
               current: Ice.Current) throws -> CommonProcessPrx? {
        guard let adapter = current.adapter else {
            throw Ice.RuntimeError("Error")
        }
        _view.println("starting \(testsuite) \(exe)... ")

        //
        // For a Client test reuse the Server or ServerAMD bundle
        // if it has been loaded.
        //
        _bundleName = testsuite.split(separator: "/").map {
            if let c = $0.first {
                return c.uppercased() + $0.dropFirst()
            } else {
                return String($0)
            }
        }.joined(separator: "")

        if exe == "ServerAMD" {
            _bundleName += "AMD"
        }

        let helper = ControllerHelperI(view: _view,
                                       bundleName: _bundleName,
                                       args: args,
                                       exe: exe,
                                       queue: (exe == "Server" ||
                                           exe == "ServerAMD") ? _serverDispatchQueue : _clientDispatchQueue)
        helper.run()
        return try uncheckedCast(prx: adapter.addWithUUID(
            CommonProcessDisp(ProcessI(helper: helper))), type: CommonProcessPrx.self)
    }

    func getHost(protocol _: String,
                 ipv6: Bool,
                 current _: Ice.Current) throws -> String {
        return ipv6 ? _ipv6 : _ipv4
    }
}

class ControllerI {
    var _communicator: Ice.Communicator!
    static var _controller: ControllerI!

    init(view: ViewController, ipv4: String, ipv6: String) throws {
        let properties = Ice.createProperties()
        properties.setProperty(key: "Ice.Plugin.IceDiscovery", value: "1")
        properties.setProperty(key: "Ice.ThreadPool.Server.SizeMax", value: "10")
        properties.setProperty(key: "IceDiscovery.DomainId", value: "TestController")
        properties.setProperty(key: "ControllerAdapter.Endpoints", value: "tcp")
        properties.setProperty(key: "ControllerAdapter.AdapterId", value: UUID().uuidString)
        // properties.setProperty(key: "Ice.Trace.Protocol", value: "2")
        // properties.setProperty(key: "Ice.Trace.Network", value: "3")

        var initData = Ice.InitializationData()
        initData.properties = properties
        _communicator = try Ice.initialize(initData)

        let adapter = try _communicator.createObjectAdapter("ControllerAdapter")
        var ident = Ice.Identity()
        #if targetEnvironment(simulator)
            ident.category = "iPhoneSimulator"
        #else
            ident.category = "iPhoneOS"
        #endif
        ident.name = "com.zeroc.Swift-Test-Controller"

        try adapter.add(servant: CommonProcessControllerDisp(
            ProcessControllerI(view: view, ipv4: ipv4, ipv6: ipv6)), id: ident)
        try adapter.activate()
    }

    public func destroy() {
        precondition(_communicator != nil)
        _communicator.destroy()
    }

    public class func stopController() {
        if _controller != nil {
            _controller.destroy()
            _controller = nil
        }
    }

    public class func startController(view: ViewController, ipv4: String, ipv6: String) throws {
        _controller = try ControllerI(view: view, ipv4: ipv4, ipv6: ipv6)
    }
}

class ControllerHelperI: ControllerHelper, TextWriter {
    var _view: ViewController
    var _args: [String]
    var _ready: Bool
    var _completed: Bool
    var _status: Int32
    var _out: String
    var _communicator: Ice.Communicator!
    var _semaphore: DispatchSemaphore
    var _exe: String
    var _queue: DispatchQueue
    var _bundleName: String

    public init(view: ViewController, bundleName: String, args: [String], exe: String, queue: DispatchQueue) {
        _view = view
        _bundleName = bundleName
        _args = args
        _ready = false
        _completed = false
        _status = 1
        _out = ""
        _communicator = nil
        _exe = exe
        _semaphore = DispatchSemaphore(value: 0)
        _queue = queue
    }

    public func serverReady() {
        _ready = true
        _semaphore.signal()
    }

    public func communicatorInitialized(communicator: Ice.Communicator) {
        _communicator = communicator
    }

    public func loggerPrefix() -> String {
        return _bundleName
    }

    public func write(_ msg: String) {
        _out += msg
    }

    public func writeLine(_ msg: String) {
        write("\(msg)\n")
    }

    public func completed(status: Int32) {
        _completed = true
        _status = status
        _semaphore.signal()
    }

    public func run() {
        let path = "\(Bundle.main.bundlePath)/Frameworks/\(_bundleName).bundle"

        guard let bundle = Bundle(url: URL(fileURLWithPath: path)) else {
            writeLine("Bundle: `\(path)' not found")
            completed(status: 1)
            return
        }

        let className = "\(_bundleName).\(_exe)"
        guard let helperClass = bundle.classNamed(className) as? TestHelperI.Type else {
            writeLine("test: `\(className)' not found")
            completed(status: 1)
            return
        }

        _queue.async {
            do {
                let testHelper = helperClass.init()
                testHelper.setControllerHelper(controllerHelper: self)
                testHelper.setWriter(writer: self)
                try testHelper.run(args: self._args)
                self.completed(status: 0)
            } catch {
                self.writeLine("Error: \(error)")
                self.completed(status: 1)
            }
        }
    }

    public func shutdown() {
        guard let communicator = _communicator else {
            return
        }
        communicator.shutdown()
    }

    public func waitReady(timeout: Int32) throws {
        var ex: Error?
        do {
            while !_ready, !_completed {
                if _semaphore.wait(timeout: .now() + Double(timeout)) == .timedOut {
                    throw CommonProcessFailedException(reason: "Timeout waiting for the process to be ready")
                }
            }

            if _completed, _status != 0 {
                throw CommonProcessFailedException(reason: _out)
            }
        } catch {
            ex = error
        }
        if let ex = ex {
            throw ex
        }
    }

    public func waitSuccess(timeout: Int32) throws -> Int32 {
        var ex: Error?
        do {
            while !_completed {
                if _semaphore.wait(timeout: .now() + Double(timeout)) == .timedOut {
                    throw CommonProcessFailedException(reason: "Timeout waiting for the process to succeed")
                }
            }

            if _completed, _status != 0 {
                throw CommonProcessFailedException(reason: _out)
            }
        } catch {
            ex = error
        }

        if let ex = ex {
            throw ex
        }
        return _status
    }

    public func getOutput() -> String {
        return _out
    }
}