Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

MiddleMan: test_middleman.py

Line 
1 import Pyro.core
2 Pyro.config.PYRO_DETAILED_TRACEBACK = 1
3 Pyro.config.PYRO_TRACELEVEL = 1
4 Pyro.config.PYRO_USER_TRACELEVEL = 1
5
6 import middleman
7
8
9 class TestPyroService(Pyro.core.ObjBase):
10    
11     def bar(self, bacon):
12         """Return the 'bacon' arg multiplied by 10."""
13         return bacon * 10
14
15
16 class TestPyroServer(object):
17    
18     port = None
19     service = None
20     daemon = None
21     running = False
22    
23     def __init__(self, port):
24         self.port = port
25         self.service = TestPyroService()
26         self.running = False
27    
28     def start(self):
29         self.thread = threading.Thread(target=self._run)
30         self.thread.start()
31    
32     def _run(self):
33         self.daemon = Pyro.core.Daemon(port=self.port)
34         self.daemon.connect(self.service, 'foo')
35         self.running = True
36         while self.running:
37             self.daemon.handleRequests()
38         self.thread = None
39    
40     def stop(self):
41         self.running = False
42
43
44 class TestMiddleman(object):
45    
46     def test_transparency(self):
47         # Start up the Pyro server
48         server = TestPyroServer(port=6000)
49         try:
50             server.start()
51            
52             # Start up the proxy
53             tp = middleman.TransparentProxy(public=('localhost', 6500),
54                                             private=('localhost', 6000))
55             try:
56                 tp.start()
57                
58                 # open a connection
59                 get_conn = Pyro.core.getProxyForURI(
60                     "PYROLOC://%s:%s/foo" % (tp.public.host, tp.public.port))
61                 conn = get_conn()
62                
63                 # Call the method
64                 result = conn.bar(3)
65                 assert result == 30
66             finally:
67                 tp.stop()
68         finally:
69             server.stop()
70    
71     def test_no_route_to_host(self):
72         # Start up the Pyro server
73         server = TestPyroServer(port=6000)
74         try:
75             server.start()
76            
77             # Start up the proxy
78             tp = middleman.TransparentProxy(
79                 ['MySQL', ('localhost', 6500), ('localhost', 6000)]
80                 )
81             try:
82                 tp.start()
83                 tp.freeze('MySQL')
84                
85                 # Open a connection
86                 get_conn = Pyro.core.getProxyForURI(
87                     "PYROLOC://%s:%s/foo" % (tp.public.host, tp.public.port))
88                 try:
89                     conn = get_conn()
90                    
91                     # Call the method
92                     result = conn.bar(7)
93                 except RuntimeError, exc:
94                     if exc.args != ["oops"]:
95                         raise AssertionError("get_conn failure args %s != ['oops']" %
96                                              repr(exc.args))
97                     pass
98                 else:
99                     raise AssertionError("get_conn didn't fail like it should have.")
100             finally:
101                 tp.stop()
102         finally:
103             server.stop()
104
105