scalaroid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ require "minitest/autorun"
2
+
3
+ require "scalaroid"
4
+ require "pry"
5
+
6
+ $_TEST_DATA = [
7
+ "ahz2ieSh", "wooPhu8u", "quai9ooK", "Oquae4ee", "Airier1a", "Boh3ohv5", "ahD3Saog", "EM5ooc4i",
8
+ "Epahrai8", "laVahta7", "phoo6Ahj", "Igh9eepa", "aCh4Lah6", "ooT0ath5", "uuzau4Ie", "Iup6mae6",
9
+ # "xie7iSie", "ail8yeeP", "ooZ4eesi", "Ahn7ohph", "Ohy5moo6", "xooSh9Oo", "ieb6eeS7", "Thooqu9h",
10
+ # "eideeC9u", "phois3Ie", "EimaiJ2p", "sha6ahR1", "Pheih3za", "bai4eeXe", "rai0aB7j", "xahXoox6",
11
+ # "Xah4Okeg", "cieG8Yae", "Pe9Ohwoo", "Eehig6ph", "Xe7rooy6", "waY2iifu", "kemi8AhY", "Che7ain8",
12
+ # "ohw6seiY", "aegh1oBa", "thoh9IeG", "Kee0xuwu", "Gohng8ee", "thoh9Chi", "aa4ahQuu", "Iesh5uge",
13
+ # "Ahzeil8n", "ieyep5Oh", "xah3IXee", "Eefa5qui", "kai8Muuf", "seeCe0mu", "cooqua5Y", "Ci3ahF6z",
14
+ # "ot0xaiNu", "aewael8K", "aev3feeM", "Fei7ua5t", "aeCa6oph", "ag2Aelei", "Shah1Pho", "ePhieb0N",
15
+ # "Uqu7Phup", "ahBi8voh", "oon3aeQu", "Koopa0nu", "xi0quohT", "Oog4aiph", "Aip2ag5D", "tirai7Ae",
16
+ # "gi0yoePh", "uay7yeeX", "aeb6ahC1", "OoJeic2a", "ieViom1y", "di0eeLai", "Taec2phe", "ID2cheiD",
17
+ # "oi6ahR5M", "quaiGi8W", "ne1ohLuJ", "DeD0eeng", "yah8Ahng", "ohCee2ie", "ecu1aDai", "oJeijah4",
18
+ # "Goo9Una1", "Aiph3Phi", "Ieph0ce5", "ooL6cae7", "nai0io1H", "Oop2ahn8", "ifaxae7O", "NeHai1ae",
19
+ # "Ao8ooj6a", "hi9EiPhi", "aeTh9eiP", "ao8cheiH", "Yieg3sha", "mah7cu2D", "Uo5wiegi", "Oowei0ya",
20
+ # "efeiDee7", "Oliese6y", "eiSh1hoh", "Joh6hoh9", "zib6Ooqu", "eejiJie4", "lahZ3aeg", "keiRai1d",
21
+ # "Fei0aewe", "aeS8aboh", "hae3ohKe", "Een9ohQu", "AiYeeh7o", "Yaihah4s", "ood4Giez", "Oumai7te",
22
+ # "hae2kahY", "afieGh4v", "Ush0boo0", "Ekootee5", "Ya8iz6Ie", "Poh6dich", "Eirae4Ah", "pai8Eeme",
23
+ # "uNah7dae", "yo3hahCh", "teiTh7yo", "zoMa5Cuv", "ThiQu5ax", "eChi5caa", "ii9ujoiV", "ge7Iekui",
24
+ "sai2aiTa", "ohKi9rie", "ei2ioChu", "aaNgah9y", "ooJai1Ie", "shoh0oH9", "Ool4Ahya", "poh0IeYa",
25
+ "Uquoo0Il", "eiGh4Oop", "ooMa0ufe", "zee6Zooc", "ohhao4Ah", "Uweekek5", "aePoos9I", "eiJ9noor",
26
+ "phoong1E", "ianieL2h", "An7ohs4T", "Eiwoeku3", "sheiS3ao", "nei5Thiw", "uL5iewai", "ohFoh9Ae"]
27
+
28
+ $_TOO_LARGE_REQUEST_SIZE = 1024*1024*10 # number of bytes
@@ -0,0 +1,299 @@
1
+ class TestPubSub < Minitest::Test
2
+ def setup
3
+ @testTime = (Time.now.to_f * 1000).to_i
4
+ end
5
+
6
+ # checks if there are more elements in list than in expectedElements and returns one of those elements
7
+ def self._getDiffElement(list, expectedElements)
8
+ expectedElements.each do |e|
9
+ list.delete(e)
10
+ end
11
+
12
+ if list.length > 0
13
+ return list[0]
14
+ else
15
+ return nil
16
+ end
17
+ end
18
+
19
+ # Test method for PubSub()
20
+ def test_pub_sub1()
21
+ conn = Scalaroid::PubSub.new()
22
+ conn.close_connection()
23
+ end
24
+
25
+ # Test method for PubSub(conn)
26
+ def test_pub_sub2()
27
+ conn = Scalaroid::PubSub.new(conn = Scalaroid::JSONConnection.new(url = Scalaroid::DEFAULT_URL))
28
+ conn.close_connection()
29
+ end
30
+
31
+ # Test method for PubSub.close_connection() trying to close the connection twice.
32
+ def test_double_close()
33
+ conn = Scalaroid::PubSub.new()
34
+ conn.close_connection()
35
+ conn.close_connection()
36
+ end
37
+
38
+ # Test method for PubSub.publish(topic, content) with a closed connection.
39
+ def test_publish_not_connected()
40
+ topic = "_Publish_NotConnected"
41
+ conn = Scalaroid::PubSub.new()
42
+ conn.close_connection()
43
+ #assert_raises( Scalaroid::ConnectionError ) { conn.publish(@testTime.to_s + topic, $_TEST_DATA[0]) }
44
+ conn.publish(@testTime.to_s + topic, $_TEST_DATA[0])
45
+ conn.close_connection()
46
+ end
47
+
48
+ # Test method for PubSub.publish(topic, content).
49
+ # Publishes some topics and uses a distinct key for each value.
50
+ def test_publish1()
51
+ topic = "_Publish1_"
52
+ conn = Scalaroid::PubSub.new()
53
+
54
+ (0..($_TEST_DATA.length - 1)).each do |i|
55
+ conn.publish(@testTime.to_s + topic + i.to_s, $_TEST_DATA[i])
56
+ end
57
+
58
+ conn.close_connection()
59
+ end
60
+
61
+ # Test method for PubSub.publish(topic, content).
62
+ # Publishes some topics and uses a single key for all the values.
63
+ def test_publish2()
64
+ topic = "_Publish2"
65
+ conn = Scalaroid::PubSub.new()
66
+
67
+ (0..($_TEST_DATA.length - 1)).each do |i|
68
+ conn.publish(@testTime.to_s + topic, $_TEST_DATA[i])
69
+ end
70
+
71
+ conn.close_connection()
72
+ end
73
+
74
+ # Test method for PubSub.get_subscribers(topic) with a closed connection.
75
+ def test_get_subscribers_otp_not_connected()
76
+ topic = "_GetSubscribers_NotConnected"
77
+ conn = Scalaroid::PubSub.new()
78
+ conn.close_connection()
79
+ #assert_raises( Scalaroid::ConnectionError ) { conn.get_subscribers(@testTime.to_s + topic) }
80
+ conn.get_subscribers(@testTime.to_s + topic)
81
+ conn.close_connection()
82
+ end
83
+
84
+ # Test method for PubSub.get_subscribers(topic).
85
+ # Tries to get a subscriber list from an empty topic.
86
+ def test_get_subscribers_not_existing_topic()
87
+ topic = "_GetSubscribers_NotExistingTopic"
88
+ conn = Scalaroid::PubSub.new()
89
+ subscribers = conn.get_subscribers(@testTime.to_s + topic)
90
+ assert_equal([], subscribers)
91
+ conn.close_connection()
92
+ end
93
+
94
+ # Test method for PubSub.subscribe(topic url) with a closed connection.
95
+ def test_subscribe_not_connected()
96
+ topic = "_Subscribe_NotConnected"
97
+ conn = Scalaroid::PubSub.new()
98
+ conn.close_connection()
99
+ #assert_raises( Scalaroid::ConnectionError ) { conn.subscribe(@testTime.to_s + topic, $_TEST_DATA[0]) }
100
+ conn.subscribe(@testTime.to_s + topic, $_TEST_DATA[0])
101
+ conn.close_connection()
102
+ end
103
+
104
+ # Test method for PubSub.subscribe(topic, url) and PubSub.get_subscribers(topic).
105
+ # Subscribes some arbitrary URLs to arbitrary topics and uses a distinct topic for each URL.
106
+ def test_subscribe1()
107
+ topic = "_Subscribe1_"
108
+ conn = Scalaroid::PubSub.new()
109
+
110
+ (0..($_TEST_DATA.length - 1)).each do |i|
111
+ conn.subscribe(@testTime.to_s + topic + i.to_s, $_TEST_DATA[i])
112
+ end
113
+
114
+ # check if the subscribers were successfully saved:
115
+ (0..($_TEST_DATA.length - 1)).each do |i|
116
+ topic1 = topic + i.to_s
117
+ subscribers = conn.get_subscribers(@testTime.to_s + topic1)
118
+ assert(subscribers.include?($_TEST_DATA[i]),
119
+ "Subscriber \"" + $_TEST_DATA[i] + "\" does not exist for topic \"" + topic1 + "\"")
120
+ assert_equal(1, subscribers.length,
121
+ "Subscribers of topic (" + topic1 + ") should only be [" + $_TEST_DATA[i] + "], but is: " + subscribers.to_s)
122
+ end
123
+
124
+ conn.close_connection()
125
+ end
126
+
127
+ # Test method for PubSub.subscribe(topic, url) and PubSub.get_subscribers(topic).
128
+ # Subscribes some arbitrary URLs to arbitrary topics and uses a single topic for all URLs.
129
+ def test_subscribe2()
130
+ topic = "_Subscribe2"
131
+ conn = Scalaroid::PubSub.new()
132
+
133
+ (0..($_TEST_DATA.length - 1)).each do |i|
134
+ conn.subscribe(@testTime.to_s + topic, $_TEST_DATA[i])
135
+ end
136
+
137
+ # check if the subscribers were successfully saved:
138
+ subscribers = conn.get_subscribers(@testTime.to_s + topic)
139
+ (0..($_TEST_DATA.length - 1)).each do |i|
140
+ assert(subscribers.include?($_TEST_DATA[i]),
141
+ "Subscriber \"" + $_TEST_DATA[i] + "\" does not exist for topic \"" + topic + "\"")
142
+ end
143
+ assert_equal(nil, self.class._getDiffElement(subscribers, $_TEST_DATA),
144
+ "unexpected subscriber of topic \"" + topic + "\"")
145
+
146
+ conn.close_connection()
147
+ end
148
+
149
+ # Test method for PubSub.unsubscribe(topic url) with a closed connection.
150
+ def test_unsubscribe_not_connected()
151
+ topic = "_Unsubscribe_NotConnected"
152
+ conn = Scalaroid::PubSub.new()
153
+ conn.close_connection()
154
+ #assert_raises( Scalaroid::ConnectionError ) { conn.unsubscribe(@testTime.to_s + topic, $_TEST_DATA[0]) }
155
+ assert_raises( Scalaroid::NotFoundError ) { conn.unsubscribe(@testTime.to_s + topic, $_TEST_DATA[0]) }
156
+ conn.close_connection()
157
+ end
158
+
159
+ # Test method for PubSub.unsubscribe(topic url) and PubSub.get_subscribers(topic).
160
+ # Tries to unsubscribe an URL from a non-existing topic and tries to get the subscriber list afterwards.
161
+ def test_unsubscribe_not_existing_topic()
162
+ topic = "_Unsubscribe_NotExistingTopic"
163
+ conn = Scalaroid::PubSub.new()
164
+ # unsubscribe test "url":
165
+ assert_raises( Scalaroid::NotFoundError ) { conn.unsubscribe(@testTime.to_s + topic, $_TEST_DATA[0]) }
166
+
167
+ # check whether the unsubscribed urls were unsubscribed:
168
+ subscribers = conn.get_subscribers(@testTime.to_s + topic)
169
+ assert(!(subscribers.include?($_TEST_DATA[0])),
170
+ "Subscriber \"" + $_TEST_DATA[0] + "\" should have been unsubscribed from topic \"" + topic + "\"")
171
+ assert_equal(0, subscribers.length,
172
+ "Subscribers of topic (" + topic + ") should only be [], but is: " + subscribers.to_s)
173
+
174
+ conn.close_connection()
175
+ end
176
+
177
+ # Test method for PubSub.subscribe(topic url), PubSub.unsubscribe(topic url) and PubSub.get_subscribers(topic).
178
+ # Tries to unsubscribe an unsubscribed URL from an existing topic and compares the subscriber list afterwards.
179
+ def test_unsubscribe_not_existing_url()
180
+ topic = "_Unsubscribe_NotExistingUrl"
181
+ conn = Scalaroid::PubSub.new()
182
+
183
+ # first subscribe test "urls"...
184
+ conn.subscribe(@testTime.to_s + topic, $_TEST_DATA[0])
185
+ conn.subscribe(@testTime.to_s + topic, $_TEST_DATA[1])
186
+
187
+ # then unsubscribe another "url":
188
+ assert_raises( Scalaroid::NotFoundError ) { conn.unsubscribe(@testTime.to_s + topic, $_TEST_DATA[2]) }
189
+
190
+ # check whether the subscribers were successfully saved:
191
+ subscribers = conn.get_subscribers(@testTime.to_s + topic)
192
+ assert(subscribers.include?($_TEST_DATA[0]),
193
+ "Subscriber \"" + $_TEST_DATA[0] + "\" does not exist for topic \"" + topic + "\"")
194
+ assert(subscribers.include?($_TEST_DATA[1]),
195
+ "Subscriber \"" + $_TEST_DATA[1] + "\" does not exist for topic \"" + topic + "\"")
196
+
197
+ # check whether the unsubscribed urls were unsubscribed:
198
+ assert(!(subscribers.include?($_TEST_DATA[2])),
199
+ "Subscriber \"" + $_TEST_DATA[2] + "\" should have been unsubscribed from topic \"" + topic + "\"")
200
+
201
+ assert_equal(2, subscribers.length,
202
+ "Subscribers of topic (" + topic + ") should only be [\"" + $_TEST_DATA[0] + "\", \"" + $_TEST_DATA[1] + "\"], but is: " + subscribers.to_s)
203
+
204
+ conn.close_connection()
205
+ end
206
+
207
+ # Test method for PubSub.subscribe(topic url), PubSub.unsubscribe(topic url) and PubSub.get_subscribers(topic).
208
+ # Subscribes some arbitrary URLs to arbitrary topics and uses a distinct topic for each URL.
209
+ # Unsubscribes every second subscribed URL.
210
+ def test_unsubscribe1()
211
+ topic = "_UnsubscribeString1_"
212
+ conn = Scalaroid::PubSub.new()
213
+
214
+ # first subscribe test "urls"...
215
+ (0..($_TEST_DATA.length - 1)).each do |i|
216
+ conn.subscribe(@testTime.to_s + topic + i.to_s, $_TEST_DATA[i])
217
+ end
218
+
219
+ # ... then unsubscribe every second url:
220
+ (0..($_TEST_DATA.length - 1)).step(2) do |i|
221
+ conn.unsubscribe(@testTime.to_s + topic + i.to_s, $_TEST_DATA[i])
222
+ end
223
+
224
+ # check whether the subscribers were successfully saved:
225
+ (1..($_TEST_DATA.length - 1)).step(2) do |i|
226
+ topic1 = topic + i.to_s
227
+ subscribers = conn.get_subscribers(@testTime.to_s + topic1)
228
+ assert(subscribers.include?($_TEST_DATA[i]),
229
+ "Subscriber \"" + $_TEST_DATA[i] + "\" does not exist for topic \"" + topic1 + "\"")
230
+ assert_equal(1, subscribers.length,
231
+ "Subscribers of topic (" + topic1 + ") should only be [\"" + $_TEST_DATA[i] + "\"], but is: " + subscribers.to_s)
232
+ end
233
+
234
+ # check whether the unsubscribed urls were unsubscribed:
235
+ (0..($_TEST_DATA.length - 1)).step(2) do |i|
236
+ topic1 = topic + i.to_s
237
+ subscribers = conn.get_subscribers(@testTime.to_s + topic1)
238
+ assert(!(subscribers.include?($_TEST_DATA[i])),
239
+ "Subscriber \"" + $_TEST_DATA[i] + "\" should have been unsubscribed from topic \"" + topic1 + "\"")
240
+ assert_equal(0, subscribers.length,
241
+ "Subscribers of topic (" + topic1 + ") should only be [], but is: " + subscribers.to_s)
242
+ end
243
+
244
+ conn.close_connection()
245
+ end
246
+
247
+ # Test method for PubSub.subscribe(topic url), PubSub.unsubscribe(topic url) and PubSub.get_subscribers(topic).
248
+ # Subscribes some arbitrary URLs to arbitrary topics and uses a single topic for all URLs.
249
+ # Unsubscribes every second subscribed URL.
250
+ def test_unsubscribe2()
251
+ topic = "_UnubscribeString2"
252
+ conn = Scalaroid::PubSub.new()
253
+
254
+ # first subscribe all test "urls"...
255
+ (0..($_TEST_DATA.length - 1)).each do |i|
256
+ conn.subscribe(@testTime.to_s + topic, $_TEST_DATA[i])
257
+ end
258
+
259
+ # ... then unsubscribe every second url:
260
+ (0..($_TEST_DATA.length - 1)).step(2) do |i|
261
+ conn.unsubscribe(@testTime.to_s + topic, $_TEST_DATA[i])
262
+ end
263
+
264
+ # check whether the subscribers were successfully saved:
265
+ subscribers = conn.get_subscribers(@testTime.to_s + topic)
266
+ subscribers_expected = []
267
+ (1..($_TEST_DATA.length - 1)).step(2) do |i|
268
+ subscribers_expected << $_TEST_DATA[i]
269
+ assert(subscribers.include?($_TEST_DATA[i]),
270
+ "Subscriber \"" + $_TEST_DATA[i] + "\" does not exist for topic \"" + topic + "\"")
271
+ end
272
+
273
+ # check whether the unsubscribed urls were unsubscribed:
274
+ (0..($_TEST_DATA.length - 1)).step(2) do |i|
275
+ assert(!(subscribers.include?($_TEST_DATA[i])),
276
+ "Subscriber \"" + $_TEST_DATA[i] + "\" should have been unsubscribed from topic \"" + topic + "\"")
277
+ end
278
+
279
+ assert_equal(nil, self.class._getDiffElement(subscribers, subscribers_expected),
280
+ "unexpected subscriber of topic \"" + topic + "\"")
281
+
282
+ conn.close_connection()
283
+ end
284
+
285
+ # Test method for PubSub.write(key, value=bytearray()) with a
286
+ # request that is too large.
287
+ def test_req_too_large()
288
+ conn = Scalaroid::PubSub.new()
289
+ data = (0..($_TOO_LARGE_REQUEST_SIZE)).map{0}.join()
290
+ key = "_ReqTooLarge"
291
+ begin
292
+ conn.publish(@testTime.to_s + key, data)
293
+ assert(false, 'The publish should have failed unless yaws_max_post_data was set larger than ' + $_TOO_LARGE_REQUEST_SIZE.to_s())
294
+ rescue Scalaroid::ConnectionError
295
+ end
296
+
297
+ conn.close_connection()
298
+ end
299
+ end