summaryrefslogtreecommitdiff
path: root/cpp/test/IceStorm/rep1/test.py
blob: 3ecf655909e376c03441c031a6bdec0449734f3c (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
285
# -*- coding: utf-8 -*-
#
# Copyright (c) ZeroC, Inc. All rights reserved.
#

#
# Make sure IceStorm and the subscriber use the same buffer size for
# sending/receiving datagrams. This ensures the test works with bogus
# OS configurations where the reicever buffer size is smaller than the
# send buffer size (causing the received messages to be
# truncated). See also bug #6070.
#
props = {
    "IceStorm.Election.MasterTimeout" : 2,
    "IceStorm.Election.ElectionTimeout" : 2,
    "IceStorm.Election.ResponseTimeout" : 2
}

icestorm = [ IceStorm(replica=i, nreplicas=3, props = props) for i in range(0,3) ]

class IceStormRep1TestCase(IceStormTestCase):

    def runClientSide(self, current):

        def checkExpect(output, expect):
            if not expect:
                return

            if type(expect) == str:
                expect = [expect]

            for e in expect:
                if output.find(e) >= 0:
                    return
            else:
                raise RuntimeError("unexected output `{0}' (expected `{1}')".format(output, expect))

        def adminForReplica(replica, cmd, expect):
            checkExpect(self.runadmin(current, cmd, instance=self.icestorm[replica], quiet=True, exitstatus=1),
                        expect)

        def stopReplica(num):
            self.icestorm[num].shutdown(current)
            self.icestorm[num].stop(current, True)

        def startReplica(num):
            self.icestorm[num].start(current)

        def runtest(s="", p=""):
            ClientServerTestCase(client=Publisher(args=p.split(" ")),
                                 server=Subscriber(args=s.split(" "))).run(current)

        def runsub2(replica=None, expect=None):
            subscriber = Subscriber(exe="sub",
                                    instance=None if replica is None else self.icestorm[replica],
                                    args=["--id", "foo"],
                                    readyCount=0,
                                    quiet=True)
            subscriber.run(current, exitstatus=1 if expect else 0)
            checkExpect(subscriber.getOutput(current), expect)

        def rununsub2(replica=None, expect=None):
            sub = Subscriber(exe="sub",
                             instance=None if replica is None else self.icestorm[replica],
                             args=["--id", "foo"],
                             readyCount=0,
                             quiet=True)

            if replica is None:
                sub.run(current, args=["--unsub"])
            # Else we first subscribe to this replica, then unsub. We
            # shouldn't get an AlreadySubscribedException.
            sub.run(current, exitstatus=1 if expect else 0)
            if expect:
                checkExpect(sub.getOutput(current), expect)
                return

            sub.run(current, args=["--unsub"])

        current.write("testing topic creation across replicas... ")
        self.runadmin(current, "create single")
        for replica in range(0, 3):
            adminForReplica(replica, "create single", "error: topic `single' exists")
        current.writeln("ok")

        current.write("testing topic destruction across replicas... ")
        sys.stdout.flush()
        self.runadmin(current, "destroy single")

        for replica in range(0, 3):
            adminForReplica(replica, "destroy single", "error: couldn't find topic `single'")
        current.writeln("ok")

        current.write("testing topic creation without replica... ")

        stopReplica(0)

        self.runadmin(current, "create single")

        for replica in range(1, 3):
            adminForReplica(replica, "create single", "error: topic `single' exists")

        adminForReplica(0, "create single", ["ConnectionRefused", "ConnectFailed"])

        startReplica(0)

        adminForReplica(0, "create single", "error: topic `single' exists")
        current.writeln("ok")

        self.runadmin(current, "destroy single")

        current.write("testing topic creation without master... ")
        sys.stdout.flush()

        stopReplica(2)

        self.runadmin(current, "create single")

        for replica in range(0, 2):
            adminForReplica(replica, "create single", "error: topic `single' exists")

        adminForReplica(2, "create single", ["ConnectionRefused", "ConnectFailed"])

        startReplica(2)

        adminForReplica(2, "create single", "error: topic `single' exists")
        current.writeln("ok")

        # All replicas are running

        current.write("testing topic destruction without replica... ")

        stopReplica(0)

        self.runadmin(current, "destroy single")

        for replica in range(1, 3):
            adminForReplica(replica, "destroy single", "error: couldn't find topic `single'")

        adminForReplica(0, "destroy single", ["ConnectionRefused", "ConnectFailed"])

        startReplica(0)

        adminForReplica(0, "destroy single", "error: couldn't find topic `single'")
        current.writeln("ok")

        current.write("testing topic destruction without master... ")
        sys.stdout.flush()

        self.runadmin(current, "create single")
        stopReplica(2)

        self.runadmin(current, "destroy single")

        for replica in range(0, 2):
            adminForReplica(replica, "destroy single", "error: couldn't find topic `single'")

        adminForReplica(2, "destroy single", ["ConnectionRefused", "ConnectFailed"])

        startReplica(2)

        adminForReplica(2, "destroy single", "error: couldn't find topic `single'")
        current.writeln("ok")

        # Now test subscription/unsubscription on all replicas.

        self.runadmin(current, "create single")

        current.write("testing subscription across replicas... ")
        sys.stdout.flush()
        runsub2()

        for replica in range(0, 3):
            runsub2(replica, "IceStorm::AlreadySubscribed")
        current.writeln("ok")

        current.write("testing unsubscription across replicas... ")
        sys.stdout.flush()
        rununsub2()

        for replica in range(0, 3):
            rununsub2(replica)
        current.writeln("ok")

        current.write("testing subscription without master... ")
        sys.stdout.flush()
        stopReplica(2)

        runsub2()

        for replica in range(0, 2):
            runsub2(replica, "IceStorm::AlreadySubscribed")

        runsub2(2, ["ConnectionRefused", "ConnectFailed"])

        startReplica(2)

        runsub2(2, "IceStorm::AlreadySubscribed")
        current.writeln("ok")

        current.write("testing unsubscription without master... ")
        sys.stdout.flush()
        stopReplica(2)

        rununsub2()

        for replica in range(0, 2):
            rununsub2(replica)

        rununsub2(2, ["ConnectionRefused", "ConnectFailed"])

        startReplica(2)

        rununsub2(2)
        current.writeln("ok")

        current.write("testing subscription without replica... ")
        sys.stdout.flush()
        stopReplica(0)

        runsub2()

        for replica in range(1, 3):
            runsub2(replica, "IceStorm::AlreadySubscribed")

        runsub2(0, ["ConnectionRefused", "ConnectFailed"])

        startReplica(0)

        runsub2(0, "IceStorm::AlreadySubscribed")
        current.writeln("ok")

        current.write("testing unsubscription without replica... ")
        stopReplica(0)

        rununsub2()

        for replica in range(1, 3):
            rununsub2(replica)

        rununsub2(0, ["ConnectionRefused", "ConnectFailed"])

        startReplica(0)

        rununsub2(0)
        current.writeln("ok")

        # All replicas are running

        current.write("running twoway subscription test... ")
        runtest("--twoway")
        current.writeln("ok")

        current.write("running ordered subscription test... ")
        runtest("--ordered")
        current.writeln("ok")

        stopReplica(2)

        current.write("running twoway, ordered subscription test without master... ")
        runtest("--twoway")
        runtest("--ordered")
        current.writeln("ok")

        startReplica(2)
        stopReplica(0)

        current.write("running twoway, ordered subscription test without replica... ")
        runtest("--twoway")
        runtest("--ordered")
        current.writeln("ok")

        startReplica(0)

        current.write("running cycle publishing test... ")
        sys.stdout.flush()
        runtest("--twoway", "--cycle")
        current.writeln("ok")

        current.write("stopping replicas... ")
        sys.stdout.flush()
        self.stopIceStorm(current)
        current.writeln("ok")

TestSuite(__file__, [ IceStormRep1TestCase("replicated", icestorm=icestorm) ], multihost=False)