summaryrefslogtreecommitdiff
path: root/swift/test/Ice/hold/AllTests.swift
blob: a937dd7dc67d26b1275661a4e5783a45fae7df93 (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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

import Foundation
import Ice
import PromiseKit
import TestCommon

class Condition {
    var _lock = os_unfair_lock()
    var _value: Bool

    init(value: Bool) {
        _value = value
    }

    func set(value: Bool) {
        withLock(&_lock) {
            self._value = value
        }
    }

    func value() -> Bool {
        return withLock(&_lock) {
            self._value
        }
    }
}

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 output = helper.getWriter()

    let communicator = helper.communicator()

    output.write("testing stringToProxy... ")
    let base = try communicator.stringToProxy("hold:\(helper.getTestEndpoint(num: 0))")!
    let baseSerialized = try communicator.stringToProxy("hold:\(helper.getTestEndpoint(num: 1))")!
    output.writeLine("ok")

    output.write("testing checked cast... ")
    let hold = try checkedCast(prx: base, type: HoldPrx.self)!
    let holdOneway = uncheckedCast(prx: base.ice_oneway(), type: HoldPrx.self)
    try test(hold == base)
    let holdSerialized = try checkedCast(prx: baseSerialized, type: HoldPrx.self)!
    let holdSerializedOneway = uncheckedCast(prx: baseSerialized.ice_oneway(), type: HoldPrx.self)
    try test(holdSerialized == baseSerialized)
    output.writeLine("ok")

    output.write("changing state between active and hold rapidly... ")
    for _ in 0 ..< 100 {
        try hold.putOnHold(0)
    }

    for _ in 0 ..< 100 {
        try holdOneway.putOnHold(0)
    }

    for _ in 0 ..< 100 {
        try holdSerialized.putOnHold(0)
    }

    for _ in 0 ..< 100 {
        try holdSerializedOneway.putOnHold(0)
    }
    output.writeLine("ok")

    output.write("testing without serialize mode... ")
    do {
        let cond = Condition(value: true)
        var value: Int32 = 0

        var completed: Promise<Int32>!
        var sent: Promise<Bool>!
        while cond.value() {
            let expected = value
            sent = Promise<Bool> { seal in
                completed = hold.setAsync(value: value + 1,
                                          delay: Int32.random(in: 0 ..< 5)) {
                    seal.fulfill($0)
                }
            }

            _ = completed!.done { (v: Int32) throws -> Void in
                if v != expected {
                    cond.set(value: false)
                }
            }

            value += 1
            if value % 100 == 0 {
                _ = try sent.wait()
            }

            if value > 100_000 {
                // Don't continue, it's possible that out-of-order dispatch doesn't occur
                // after 100000 iterations and we don't want the test to last for too long
                // when this occurs.
                break
            }
        }
        try test(value > 100_000 || !cond.value())
        _ = try sent.wait()
    }
    output.writeLine("ok")

    output.write("testing with serialize mode... ")
    do {
        let cond = Condition(value: true)
        var value: Int32 = 0

        var completed: Promise<Int32>?
        while value < 3000, cond.value() {
            let expected = value
            let sent = Promise<Bool> { seal in
                completed = holdSerialized.setAsync(value: value + 1, delay: 0) {
                    seal.fulfill($0)
                }
            }

            _ = completed!.done { (v: Int32) throws -> Void in
                if v != expected {
                    cond.set(value: false)
                }
            }
            value += 1
            if value % 100 == 0 {
                _ = try sent.wait()
            }
        }
        _ = try completed!.wait()
        try test(cond.value())

        for i in 0 ..< 10000 {
            try holdSerializedOneway.setOneway(value: value + 1, expected: value)
            value += 1
            if i % 100 == 0 {
                try holdSerializedOneway.putOnHold(1)
            }
        }
    }
    output.writeLine("ok")

    output.write("testing serialization... ")
    do {
        var value: Int32 = 0
        _ = try holdSerialized.set(value: value, delay: 0)
        var completed: Promise<Void>!
        for i in 0 ..< 10000 {
            // Create a new proxy for each request
            completed = holdSerialized.ice_oneway().setOnewayAsync(value: value + 1, expected: value)
            value += 1
            if (i % 100) == 0 {
                try completed.wait()
                try holdSerialized.ice_ping() // Ensure everything's dispatched.
                try holdSerialized.ice_getConnection()!.close(.GracefullyWithWait)
            }
        }
        try completed.wait()
    }
    output.writeLine("ok")

    output.write("testing waitForHold... ")
    do {
        try hold.waitForHold()
        try hold.waitForHold()
        for i in 0 ..< 1000 {
            try holdOneway.ice_ping()
            if (i % 20) == 0 {
                try hold.putOnHold(0)
            }
        }
        try hold.putOnHold(-1)
        try hold.ice_ping()
        try hold.putOnHold(-1)
        try hold.ice_ping()
    }
    output.writeLine("ok")

    output.write("changing state to hold and shutting down server... ")
    try hold.shutdown()
    output.writeLine("ok")
}