-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration_test.py
160 lines (126 loc) · 5.14 KB
/
integration_test.py
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
import unittest
import urllib
import urllib2
import subprocess
import threading
import time
import v2
import argparse
import sys
class TestRendezvous(unittest.TestCase):
serverProcess = None
serverAddress = "localhost:8080"
useLocalServer = True
@classmethod
def setUpClass(self):
if self.useLocalServer:
print "Starting local server"
self.serverProcess = subprocess.Popen(["python", "v2.py"])
time.sleep(1)
@classmethod
def tearDownClass(self):
if self.useLocalServer:
self.serverProcess.kill()
def test_new_returns_random_channel(self):
channel1 = urllib2.urlopen("http://%s/new" % self.serverAddress).read()
self.assertEqual(len(channel1), 32)
channel2 = urllib2.urlopen("http://%s/new" % self.serverAddress).read()
self.assertEqual(len(channel2), 32)
self.assertNotEqual(channel1, channel2)
def test_can_send_message_to_newly_created_channel(self):
channel = urllib2.urlopen("http://%s/new" % self.serverAddress).read()
self.assertEqual(len(channel), 32)
self.called = False
def read():
data = urllib2.urlopen("http://%s/channel/%s" % (self.serverAddress, channel)).read()
self.assertEqual(data, "a=1&b=2")
self.called = True
def write():
url = 'http://%s/channel/%s' % (self.serverAddress, channel)
data = urllib.urlencode({'a':'1', 'b':'2'})
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
readThread = threading.Thread(target=read)
readThread.start()
writeThread = threading.Thread(target=write)
writeThread.start()
readThread.join()
writeThread.join()
self.assertTrue(self.called)
def test_can_send_message_to_arbitrary_channel(self):
channel = "channel"
self.called = False
def read():
data = urllib2.urlopen("http://%s/channel/%s" % (self.serverAddress, channel)).read()
self.assertEqual(data, "a=1&b=2")
self.called = True
def write():
url = 'http://%s/channel/%s' % (self.serverAddress, channel)
data = urllib.urlencode({'a':'1', 'b':'2'})
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
readThread = threading.Thread(target=read)
readThread.start()
writeThread = threading.Thread(target=write)
writeThread.start()
readThread.join()
writeThread.join()
self.assertTrue(self.called)
def test_read_twice_from_same_channel_results_in_404_error(self):
channel = "mychannel"
self.caughtException = False
self.called = False
def read():
data = urllib2.urlopen("http://%s/channel/%s" % (self.serverAddress, channel)).read()
self.assertEqual(data, "a=555&b=555")
self.called = True
readThread = threading.Thread(target=read)
readThread.start()
time.sleep(0.5)
try:
urllib2.urlopen("http://%s/channel/%s" % (self.serverAddress, channel)).read()
except urllib2.HTTPError as e:
self.caughtException = True
self.assertEqual(e.code, 404)
self.assertTrue(self.caughtException)
## Writes to the channel just to clean the thread clean
url = 'http://%s/channel/%s' % (self.serverAddress, channel)
data = urllib.urlencode({'a':'555', 'b':'555'})
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
readThread.join()
self.assertTrue(self.called)
def test_write_twice_to_same_channel_results_in_404_error(self):
channel = "mychannelchannel"
self.caughtException = False
def write():
url = 'http://%s/channel/%s' % (self.serverAddress, channel)
data = urllib.urlencode({'a':'10', 'b':'20'})
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
writeThread = threading.Thread(target=write)
writeThread.start()
time.sleep(0.5)
url = 'http://%s/channel/%s' % (self.serverAddress, channel)
data = urllib.urlencode({'a':'1', 'b':'2'})
req = urllib2.Request(url, data)
try:
urllib2.urlopen(req)
except urllib2.HTTPError as e:
self.caughtException = True
self.assertEqual(e.code, 404)
self.assertTrue(self.caughtException)
## Reads the data just to clean the thread clean
data = urllib2.urlopen("http://%s/channel/%s" % (self.serverAddress, channel)).read()
self.assertEqual(data, "a=10&b=20")
writeThread.join()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--server')
parser.add_argument('unittest_args', nargs='*')
args = parser.parse_args()
if args.server is not None:
TestRendezvous.serverAddress = args.server
TestRendezvous.useLocalServer = False
sys.argv[1:] = args.unittest_args
unittest.main()