Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / howto / listings / trial / calculus / test / test_remote_2.py
1 from calculus.remote_1 import RemoteCalculationFactory
2 from calculus.client_2 import RemoteCalculationClient
3
4 from twisted.trial import unittest
5 from twisted.internet import reactor, protocol
6
7
8
9 class RemoteRunCalculationTestCase(unittest.TestCase):
10
11     def setUp(self):
12         factory = RemoteCalculationFactory()
13         self.port = reactor.listenTCP(0, factory, interface="127.0.0.1")
14         self.client = None
15
16
17     def tearDown(self):
18         if self.client is not None:
19             self.client.transport.loseConnection()
20         return self.port.stopListening()
21
22
23     def _test(self, op, a, b, expected):
24         creator = protocol.ClientCreator(reactor, RemoteCalculationClient)
25         def cb(client):
26             self.client = client
27             return getattr(self.client, op)(a, b
28                 ).addCallback(self.assertEqual, expected)
29         return creator.connectTCP('127.0.0.1', self.port.getHost().port
30             ).addCallback(cb)
31
32
33     def test_add(self):
34         return self._test("add", 5, 9, 14)
35
36
37     def test_subtract(self):
38         return self._test("subtract", 47, 13, 34)
39
40
41     def test_multiply(self):
42         return self._test("multiply", 7, 3, 21)
43
44
45     def test_divide(self):
46         return self._test("divide", 84, 10, 8)