aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/test_wildcard.py
blob: d2c68dc147ecc21df3251164bbc0620d8dab05e0 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
## www.pubnub.com - PubNub Real-time push service in the cloud.
# coding=utf8

## PubNub Real-time Push APIs and Notifications Framework
## Copyright (c) 2010 Stephen Blum
## http://www.pubnub.com/


from inspect import currentframe, getframeinfo, getouterframes

def get_line():
	#print getouterframes(currentframe())[3]
	return getouterframes(currentframe())[2][2]


from gevent import monkey; monkey.patch_all()

import time
import random

DELAY = 5

RESULTS = {}

def check_tests():
	for i in RESULTS:
		test = RESULTS[i]
		if test['done'] is False:
			if test['expect'] == test['passed']:
				green(i + " PASSED " + str(test['passed']))
			else:
				red(i + " FAILED " + str(test['failed']))
			test['done'] = True



def init(conf, name, expect):
	#check_tests()
	print("\n\n")
	RESULTS[name+conf] = {'passed' : 0, 'failed' : 0, 'expect' : 0, 'done' : False, 'conf' : conf}
	RESULTS[name+conf]['expect'] = expect

def get_random():
	return str(random.randint(1, 99999))


import sys
from pubnub import Pubnub

def red(name): print '\033[1;31m' + name +  '\033[1;m'

def green(name): print '\033[1;92m' + name +  '\033[1;m'




def test(cond, desc=None, test_name=None, conf=None):
	if (cond):
		green("["+ test_name + " " + conf   +" ""][" + str(get_line()) + "] PASS : " + str(desc))
		RESULTS[test_name+conf]['passed'] = RESULTS[test_name+conf]['passed'] + 1
	else:
		red("["+ test_name + " " + conf +  " ""][" + str(get_line()) + "] FAIL : " + str(desc))
		RESULTS[test_name+conf]['failed'] = RESULTS[test_name+conf]['failed'] + 1
		#exit(0)



def run_tests(tests):

	for test in tests:
		if len(test) > 4:
			test[1](test[2], test[3], test[0], test[4])
		else:
			test[1](test[2], test[3], test[0])
		time.sleep(DELAY)
		check_tests()



def test_1(pubnub, pubnub2, conf=None, msg=None):


	init(conf, "test_1", 6)
	r = get_random()
	channel_wc 	= r + "-" + "test_1-ab.*"
	message 	= msg if msg is not None else (r + "-" + "test_1_hi")
	channel 	= r + "-" + "test_1-ab.d"

	# Asynchronous usage
	def callback(message1, channel1, real_channel=None):
		#print(str(message1) + ' : ' + str(channel1) + ' : ' + str(real_channel))
		test(message1 == unicode(message, 'utf-8'), "message received", "test_1", conf)
		test(channel1 == channel_wc, "Channel == wildcard channel", "test_1", conf)
		test(real_channel == channel, "Real channel == publish channel", "test_1", conf)


	def error(message):
	    print("ERROR : " + str(message))


	def connect(channel1=None):

		test(True, "Connect Called", "test_1", conf)
		test(channel1 == channel_wc, "Channel param in Connect same as wildcard", "test_1", conf)

		def _callback(message):
			test(message[0] == 1, "Publish Successful", "test_1", conf)

		def _error(message):
			test(False, "Publish Successful", "test_1", conf)

		pubnub.publish(channel=channel, message=message, callback=_callback, error=_error)



	pubnub.subscribe(channels=channel_wc, callback=callback, error=callback, connect=connect)



def test_2(pubnub, pubnub2, conf=None, msg=None):

	init(conf, "test_2", 8)
	r = get_random()
	channel_wc 	= r + "-" + "test_2-ab.*"
	message 	= msg if msg is not None else (r + "-" + "test_2_hi")
	channel 	= r + "-" + "test_2-ab.d"

	# Asynchronous usage
	def callback(message1, channel1, real_channel=None):
		#print(str(message1) + ' : ' + str(channel1) + ' : ' + str(real_channel))
		test(message1 == unicode(message, 'utf-8'), "message received", "test_2", conf)
		test(channel1 == channel_wc, "Channel == wildcard channel", "test_2", conf)
		test(real_channel == channel, "Real channel == publish channel", "test_2", conf)


	def error(message):
	    print("ERROR : " + str(message))

	def presence(message, channel1, real_channel=None):
		if (pubnub.uuid == message['uuid']):
			test(channel_wc == real_channel, "On pubnub subscribe presence event as wildcard as real channel", "test_2", conf)
		elif (pubnub2.uuid == message['uuid']):
			test(channel == real_channel, "On pubnub2 subscribe presence event as channel as real channel", "test_2", conf)
		else:
			test(false, "Wrong presence event", "test_2", conf)

	def connect(channel1=None):
		#print(channel1)
		test(True, "Connect Called", "test_2", conf)
		test(channel1 == channel_wc, "Channel param in Connect same as wildcard", "test_2", conf)

		def _callback(message):
			test(message[0] == 1, "Publish Successful", "test_2", conf)

		def _error(message):
			test(False, "Publish Successful", "test_2", conf)

		pubnub.publish(channel=channel, message=message, callback=_callback, error=_error)


		def _callback(message, channel1):
			pass

		def _error(message):
			test(False, "Error in subscribe", "test_2", conf)


		pubnub2.subscribe(channels=channel, callback=_callback, error=_error)

	pubnub.subscribe(channels=channel_wc, callback=callback, error=callback, connect=connect, presence=presence)



def test_3(pubnub, pubnub2, conf=None, msg=None):

	init(conf, "test_3", 6)

	r = get_random()
	channel_wc 	= r + "-" + "test_3-ab.*"
	message 	= msg if msg is not None else (r + "-" + "test_3_hi")
	channel 	= r + "-" + "test_3-ab.d"

	# Asynchronous usage
	def callback(message1, channel1, real_channel=None):
		if 'action' in message1:
			test(False, "Presence event received without presence callback", "test_3", conf)
			return

		test(message1 == unicode(message, 'utf-8'), "message received", "test_3", conf)
		test(channel1 == channel_wc, "Channel == wildcard channel", "test_3", conf)
		test(real_channel == channel, "Real channel == publish channel", "test_3", conf)


	def error(message):
	    print("ERROR : " + str(message))

	def connect(channel1=None):
		#print(channel1)
		test(True, "Connect Called", "test_3", conf)
		test(channel1 == channel_wc, "Channel param in Connect same as wildcard", "test_3", conf)

		def _callback(message):
			test(message[0] == 1, "Publish Successful", "test_3", conf)

		def _error(message):
			test(False, "Publish Successful", "test_3", conf)

		pubnub.publish(channel=channel, message=message, callback=_callback, error=_error)


		def _callback(message, channel1):
			pass

		def _error(message):
			test(False, "Error in subscribe", "test_3", conf)


		pubnub2.subscribe(channels=channel, callback=_callback, error=_error)

	pubnub.subscribe(channels=channel_wc, callback=callback, error=callback, connect=connect)


def test_4(pubnub, pubnub2, conf=None, msg=None):

	init(conf, "test_4", 18)

	r = get_random()
	channel_wc 	= r + "-" + "test_4-ab.*"
	channel_group = r + "-" + "test_4_group"
	channel_g 	= r + "-" + "test_4_group_channel"
	message 	= msg if msg is not None else (r + "-" + "test_4_hi")
	channel 	= r + "-" + "test_4-ab.d"
	channel_n   = r + "-" + "test_4-a"



	def _callback(resp):
		# Asynchronous usage
		def callback_wc(message1, channel1, real_channel=None):
			test(message1 == unicode(message, 'utf-8'), "message received", "test_4", conf)
			test(channel1 == channel_wc, "Channel == wildcard channel", "test_4", conf)
			test(real_channel == channel, "Real channel == publish channel", "test_4", conf)

		def callback_group(message1, channel1, real_channel=None):
			test(message1 == unicode(message, 'utf-8'), "message received", "test_4", conf)
			test(channel1 == channel_group, "Channel == group", "test_4", conf)
			test(real_channel == channel_g, "Real channel == publish channel", "test_4", conf)

		def callback_n(message1, channel1, real_channel=None):
			test(message1 == unicode(message, 'utf-8'), "message received", "test_4", conf)
			test(channel1 == channel_n, "Channel ==  channel", "test_4", conf)
			test(real_channel == channel_n, "Real channel == publish channel", "test_4", conf)

		def error(message):
		    print("ERROR : " + str(message))

		def connect_wc(channel1=None):

			test(True, "Connect Called", "test_4", conf)
			test(channel1 == channel_wc, "Channel param in Connect same as wildcard", "test_4", conf)

			def _callback(message):
				test(message[0] == 1, "Publish Successful", "test_4", conf)

			def _error(message):
				test(False, "Publish Successful", "test_4", conf)

			pubnub.publish(channel=channel, message=message, callback=_callback, error=_error)

		def connect_group(channel1=None):
			#print(channel1)
			test(True, "Connect Called", "test_4", conf)
			test(channel1 == channel_group, "Channel param in Connect same as channel_group", "test_4", conf)

			def _callback(message):
				test(message[0] == 1, "Publish Successful", "test_4", conf)

			def _error(message):
				test(False, "Publish Successful", "test_4", conf)

			pubnub.publish(channel=channel_g, message=message, callback=_callback, error=_error)

		def connect_n(channel1=None):
			#print(channel1)
			test(True, "Connect Called", "test_4", conf)
			test(channel1 == channel_n, "Channel param in Connect same as channel_n", "test_4", conf)

			def _callback(message):
				test(message[0] == 1, "Publish Successful", "test_4", conf)

			def _error(message):
				test(False, "Publish Successful", "test_4", conf)


			pubnub.publish(channel=channel_n, message=message, callback=_callback, error=_error)

		pubnub.subscribe(channels=channel_wc, callback=callback_wc, error=error, connect=connect_wc)

		pubnub.subscribe(channels=channel_n, callback=callback_n, error=error, connect=connect_n)

		pubnub.subscribe_group(channel_groups=[channel_group], callback=callback_group, error=error, connect=connect_group)

	def _error(message):
		test(False, "Channel Group Creation failed")

	pubnub.channel_group_add_channel(channel_group=channel_group, channel=channel_g, callback=_callback, error=_error)


pubnub = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", ssl_on=False)

pubnub2 = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", ssl_on=False)


pubnub_ssl = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", ssl_on=True)

pubnub_ssl_2 = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", ssl_on=True)


pubnub_enc = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", cipher_key="enigma", ssl_on=False)

pubnub_enc_2 = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", cipher_key="enigma", ssl_on=False, daemon=False)

pubnub_enc_ssl = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", cipher_key="enigma", ssl_on=False)

pubnub_enc_ssl_2 = Pubnub(publish_key="ds", subscribe_key="ds",
            secret_key="ds", cipher_key="enigma", ssl_on=False, daemon=False)

#run_tests([	("[EMOJI][ENC]", test_4, pubnub_enc, pubnub_enc_2, "😀")])


run_tests([	
			("", test_1, pubnub, pubnub2), 
			("[SSL]", test_1, pubnub_ssl, pubnub_ssl_2),
			("[ENC]", test_1, pubnub_enc, pubnub_enc_2),
			("[SSL][ENC]", test_1, pubnub_enc_ssl, pubnub_enc_ssl_2),
			("", test_2, pubnub, pubnub2), 
			("[SSL]", test_2, pubnub_ssl, pubnub_ssl_2),
			("[ENC]", test_2, pubnub_enc, pubnub_enc_2),
			("[SSL][ENC]", test_2, pubnub_enc_ssl, pubnub_enc_ssl_2),
			("", test_3, pubnub, pubnub2), 
			("[SSL]", test_3, pubnub_ssl, pubnub_ssl_2),
			("[ENC]", test_3, pubnub_enc, pubnub_enc_2),
			("[SSL][ENC]", test_3, pubnub_enc_ssl, pubnub_enc_ssl_2),
			("", test_3, pubnub, pubnub2), 
			("[SSL]", test_3, pubnub_ssl, pubnub_ssl_2),
			("[ENC]", test_3, pubnub_enc, pubnub_enc_2),
			("[SSL][ENC]", test_3, pubnub_enc_ssl, pubnub_enc_ssl_2),
			("", test_4, pubnub, pubnub2), 
			("[SSL]", test_4, pubnub_ssl, pubnub_ssl_2),
			("[ENC]", test_4, pubnub_enc, pubnub_enc_2),
			("[SSL][ENC]", test_4, pubnub_enc_ssl, pubnub_enc_ssl_2),
			("[EMOJI]", test_1, pubnub, pubnub2, "😀"), 
			("[SSL][EMOJI]", test_1, pubnub_ssl, pubnub_ssl_2, "😀"),
			("[ENC][EMOJI]", test_1, pubnub_enc, pubnub_enc_2, "😀"),
			("[SSL][ENC][EMOJI]", test_1, pubnub_enc_ssl, pubnub_enc_ssl_2, "😀"),
			("[EMOJI]", test_2, pubnub, pubnub2, "😀"), 
			("[SSL][EMOJI]", test_2, pubnub_ssl, pubnub_ssl_2, "😀"),
			("[ENC][EMOJI]", test_2, pubnub_enc, pubnub_enc_2, "😀"),
			("[SSL][ENC][EMOJI]", test_2, pubnub_enc_ssl, pubnub_enc_ssl_2, "😀"),
			("[EMOJI]", test_3, pubnub, pubnub2, "😀"), 
			("[SSL][EMOJI]", test_3, pubnub_ssl, pubnub_ssl_2, "😀"),
			("[ENC][EMOJI]", test_3, pubnub_enc, pubnub_enc_2, "😀"),
			("[SSL][ENC][EMOJI]", test_3, pubnub_enc_ssl, pubnub_enc_ssl_2, "😀"),
			("[EMOJI]", test_3, pubnub, pubnub2, "😀"), 
			("[SSL][EMOJI]", test_3, pubnub_ssl, pubnub_ssl_2, "😀"),
			("[ENC][EMOJI]", test_3, pubnub_enc, pubnub_enc_2, "😀"),
			("[SSL][ENC][EMOJI]", test_3, pubnub_enc_ssl, pubnub_enc_ssl_2, "😀"),
			("[EMOJI]", test_3, pubnub, pubnub2, "😀"), 
			("[SSL][EMOJI]", test_3, pubnub_ssl, pubnub_ssl_2, "😀"),
			("[ENC][EMOJI]", test_3, pubnub_enc, pubnub_enc_2, "😀"),
			("[SSL][ENC][EMOJI]", test_3, pubnub_enc_ssl, pubnub_enc_ssl_2, "😀"),
			("[EMOJI]", test_4, pubnub, pubnub2, "😀"), 
			("[SSL][EMOJI]", test_4, pubnub_ssl, pubnub_ssl_2, "😀"),
			("[ENC][EMOJI]", test_4, pubnub_enc, pubnub_enc_2, "😀"),
			("[SSL][ENC][EMOJI]", test_4, pubnub_enc_ssl, pubnub_enc_ssl_2, "😀")
		])