-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest_udp.py
60 lines (50 loc) · 2.16 KB
/
test_udp.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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from unittest import *
from BTL.translation import _
from BitTorrent.RawServer_twisted import RawServer
from BitTorrent.defaultargs import common_options, rare_options
from threading import Event
if __name__ =="__main__":
tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])
result = TextTestRunner().run(tests)
class SimpleTests(TestCase):
def setUp(self):
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(d)
self.a = self.r.create_udpsocket(8051, '127.0.0.1')
self.b = self.r.create_udpsocket(8052, '127.0.0.1')
def tearDown(self):
self.a.close()
self.b.close()
def Handler(self, expected):
class h(object):
def __init__(self, expected, a=self.assertEqual):
self.expected = expected
self.a = a
def data_came_in(self, connection, data):
self.a(self.expected, data)
return h(expected)
def testFoo(self):
self.r.start_listening_udp(self.a, self.Handler(''))
self.r.start_listening_udp(self.b, self.Handler('foo'))
self.a.sendto("foo", 0, ('127.0.0.1', 8052))
self.r.listen_once()
def testBackForth(self):
self.r.start_listening_udp(self.a, self.Handler('bar'))
self.r.start_listening_udp(self.b, self.Handler('foo'))
self.a.sendto("foo", 0, ('127.0.0.1', 8052))
self.r.listen_once()
self.b.sendto("bar", 0, ('127.0.0.1', 8051))
self.r.listen_once()