Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: mojo/python/tests/messaging_unittest.py

Issue 675563002: Remove mojo/python and gyp targets (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/python/tests/bindings_structs_unittest.py ('k') | mojo/python/tests/mojo_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import unittest
6
7 import mojo_unittest
8 from mojo.bindings import messaging
9
10 # pylint: disable=E0611
11 from mojo import system
12
13
14 class _ForwardingConnectionErrorHandler(messaging.ConnectionErrorHandler):
15
16 def __init__(self, callback):
17 messaging.ConnectionErrorHandler.__init__(self)
18 self._callback = callback
19
20 def OnError(self, result):
21 self._callback(result)
22
23
24 class ConnectorTest(mojo_unittest.MojoTestCase):
25
26 def setUp(self):
27 super(ConnectorTest, self).setUp()
28 self.received_messages = []
29 self.received_errors = []
30 def _OnMessage(message):
31 self.received_messages.append(message)
32 return True
33 def _OnError(result):
34 self.received_errors.append(result)
35 handles = system.MessagePipe()
36 self.connector = messaging.Connector(handles.handle1)
37 self.connector.SetIncomingMessageReceiver(
38 messaging.ForwardingMessageReceiver(_OnMessage))
39 self.connector.SetErrorHandler(
40 _ForwardingConnectionErrorHandler(_OnError))
41 self.connector.Start()
42 self.handle = handles.handle0
43
44
45 def tearDown(self):
46 self.connector = None
47 self.handle = None
48 super(ConnectorTest, self).tearDown()
49
50 def testConnectorRead(self):
51 self.handle.WriteMessage()
52 self.loop.RunUntilIdle()
53 self.assertTrue(self.received_messages)
54 self.assertFalse(self.received_errors)
55
56 def testConnectorWrite(self):
57 self.connector.Accept(messaging.Message())
58 (result, _, _) = self.handle.ReadMessage()
59 self.assertEquals(result, system.RESULT_OK)
60 self.assertFalse(self.received_errors)
61
62 def testConnectorCloseRemoteHandle(self):
63 self.handle.Close()
64 self.loop.RunUntilIdle()
65 self.assertFalse(self.received_messages)
66 self.assertTrue(self.received_errors)
67 self.assertEquals(self.received_errors[0],
68 system.RESULT_FAILED_PRECONDITION)
69
70 def testConnectorDeleteConnector(self):
71 self.connector = None
72 (result, _, _) = self.handle.ReadMessage()
73 self.assertEquals(result, system.RESULT_FAILED_PRECONDITION)
74
75
76 class HeaderTest(unittest.TestCase):
77
78 def testSimpleMessageHeader(self):
79 header = messaging.MessageHeader(0xdeadbeaf, messaging.NO_FLAG)
80 self.assertEqual(header.message_type, 0xdeadbeaf)
81 self.assertFalse(header.has_request_id)
82 self.assertFalse(header.expects_response)
83 self.assertFalse(header.is_response)
84 data = header.Serialize()
85 other_header = messaging.MessageHeader.Deserialize(data)
86 self.assertEqual(other_header.message_type, 0xdeadbeaf)
87 self.assertFalse(other_header.has_request_id)
88 self.assertFalse(other_header.expects_response)
89 self.assertFalse(other_header.is_response)
90
91 def testMessageHeaderWithRequestID(self):
92 # Request message.
93 header = messaging.MessageHeader(0xdeadbeaf,
94 messaging.MESSAGE_EXPECTS_RESPONSE_FLAG)
95
96 self.assertEqual(header.message_type, 0xdeadbeaf)
97 self.assertTrue(header.has_request_id)
98 self.assertTrue(header.expects_response)
99 self.assertFalse(header.is_response)
100 self.assertEqual(header.request_id, 0)
101
102 data = header.Serialize()
103 other_header = messaging.MessageHeader.Deserialize(data)
104
105 self.assertEqual(other_header.message_type, 0xdeadbeaf)
106 self.assertTrue(other_header.has_request_id)
107 self.assertTrue(other_header.expects_response)
108 self.assertFalse(other_header.is_response)
109 self.assertEqual(other_header.request_id, 0)
110
111 header.request_id = 0xdeadbeafdeadbeaf
112 data = header.Serialize()
113 other_header = messaging.MessageHeader.Deserialize(data)
114
115 self.assertEqual(other_header.request_id, 0xdeadbeafdeadbeaf)
116
117 # Response message.
118 header = messaging.MessageHeader(0xdeadbeaf,
119 messaging.MESSAGE_IS_RESPONSE_FLAG,
120 0xdeadbeafdeadbeaf)
121
122 self.assertEqual(header.message_type, 0xdeadbeaf)
123 self.assertTrue(header.has_request_id)
124 self.assertFalse(header.expects_response)
125 self.assertTrue(header.is_response)
126 self.assertEqual(header.request_id, 0xdeadbeafdeadbeaf)
127
128 data = header.Serialize()
129 other_header = messaging.MessageHeader.Deserialize(data)
130
131 self.assertEqual(other_header.message_type, 0xdeadbeaf)
132 self.assertTrue(other_header.has_request_id)
133 self.assertFalse(other_header.expects_response)
134 self.assertTrue(other_header.is_response)
135 self.assertEqual(other_header.request_id, 0xdeadbeafdeadbeaf)
136
137
138 class RouterTest(mojo_unittest.MojoTestCase):
139
140 def setUp(self):
141 super(RouterTest, self).setUp()
142 self.received_messages = []
143 self.received_errors = []
144 def _OnMessage(message):
145 self.received_messages.append(message)
146 return True
147 def _OnError(result):
148 self.received_errors.append(result)
149 handles = system.MessagePipe()
150 self.router = messaging.Router(handles.handle1)
151 self.router.SetIncomingMessageReceiver(
152 messaging.ForwardingMessageReceiver(_OnMessage))
153 self.router.SetErrorHandler(
154 _ForwardingConnectionErrorHandler(_OnError))
155 self.router.Start()
156 self.handle = handles.handle0
157
158 def tearDown(self):
159 self.router = None
160 self.handle = None
161 super(RouterTest, self).tearDown()
162
163 def testSimpleMessage(self):
164 header_data = messaging.MessageHeader(0, messaging.NO_FLAG).Serialize()
165 message = messaging.Message(header_data)
166 self.router.Accept(message)
167 self.loop.RunUntilIdle()
168 self.assertFalse(self.received_errors)
169 self.assertFalse(self.received_messages)
170 (res, data, _) = self.handle.ReadMessage(bytearray(len(header_data)))
171 self.assertEquals(system.RESULT_OK, res)
172 self.assertEquals(data[0], header_data)
173
174 def testSimpleReception(self):
175 header_data = messaging.MessageHeader(0, messaging.NO_FLAG).Serialize()
176 self.handle.WriteMessage(header_data)
177 self.loop.RunUntilIdle()
178 self.assertFalse(self.received_errors)
179 self.assertEquals(len(self.received_messages), 1)
180 self.assertEquals(self.received_messages[0].data, header_data)
181
182 def testRequestResponse(self):
183 header_data = messaging.MessageHeader(
184 0, messaging.MESSAGE_EXPECTS_RESPONSE_FLAG).Serialize()
185 message = messaging.Message(header_data)
186 back_messages = []
187 def OnBackMessage(message):
188 back_messages.append(message)
189 self.router.AcceptWithResponder(message,
190 messaging.ForwardingMessageReceiver(
191 OnBackMessage))
192 self.loop.RunUntilIdle()
193 self.assertFalse(self.received_errors)
194 self.assertFalse(self.received_messages)
195 (res, data, _) = self.handle.ReadMessage(bytearray(len(header_data)))
196 self.assertEquals(system.RESULT_OK, res)
197 message_header = messaging.MessageHeader.Deserialize(data[0])
198 self.assertNotEquals(message_header.request_id, 0)
199 response_header_data = messaging.MessageHeader(
200 0,
201 messaging.MESSAGE_IS_RESPONSE_FLAG,
202 message_header.request_id).Serialize()
203 self.handle.WriteMessage(response_header_data)
204 self.loop.RunUntilIdle()
205 self.assertFalse(self.received_errors)
206 self.assertEquals(len(back_messages), 1)
207 self.assertEquals(back_messages[0].data, response_header_data)
OLDNEW
« no previous file with comments | « mojo/python/tests/bindings_structs_unittest.py ('k') | mojo/python/tests/mojo_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698