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

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

Issue 952223002: Add Python util to read from a data pipe into a bytearray (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Move to AsyncWait, add tests Created 5 years, 9 months 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
OLDNEW
(Empty)
1 # Copyright 2015 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 random
6
7 import mojo_unittest
8 from mojo_bindings import promise
9
10 # pylint: disable=F0401
11 import mojo_system as system
12
13 # pylint: disable=F0401
14 from mojo_utils import data_pipe_utils
15
16
17 def _GetRandomBuffer(size):
18 random.seed(size)
19 return bytearray(''.join(chr(random.randint(0, 255)) for i in xrange(size)))
20
21
22 class DataPipeCopyTest(mojo_unittest.MojoTestCase):
23 def setUp(self):
24 super(DataPipeCopyTest, self).setUp()
25 self.handles = system.DataPipe()
26 self.error = None
27
28 def tearDown(self):
29 self.handles = None
30 super(DataPipeCopyTest, self).tearDown()
31
32 def _writeDataAndClose(self, handle, data):
33 status, num_bytes_written = handle.WriteData(data)
34 handle.Close()
35 self.assertEquals(system.RESULT_OK, status)
36 self.assertEquals(len(data), num_bytes_written)
37
38 def _copyDataFromPipe(self, handle, expected_data,
39 deadline=system.DEADLINE_INDEFINITE):
40 self._VerifyDataCopied(data_pipe_utils.CopyFromDataPipe(
41 handle, deadline), expected_data).Catch(self._CatchError)
42
43 def _CatchError(self, error):
44 if self.loop:
45 self.loop.Quit()
46 self.error = error
47
48 @promise.async
49 def _VerifyDataCopied(self, data, expected_data):
50 self.handles.consumer_handle.Close()
qsr 2015/03/03 17:10:37 I think you do not need to handle this close yours
blundell 2015/03/04 10:28:52 Done.
51 self.assertEquals(expected_data, data)
52 self.loop.Quit()
53
54 def _runAndCheckError(self):
55 self.loop.Run()
56 if self.error:
57 # pylint: disable=E0702
58 raise self.error
59
60 def _testEagerWrite(self, data):
61 self._writeDataAndClose(self.handles.producer_handle, data)
62 self._copyDataFromPipe(self.handles.consumer_handle, data)
63 self._runAndCheckError()
64
65 def _testDelayedWrite(self, data):
66 self._copyDataFromPipe(self.handles.consumer_handle, data)
67 self._writeDataAndClose(self.handles.producer_handle, data)
68 self._runAndCheckError()
69
70 def testTimeout(self):
71 self._copyDataFromPipe(self.handles.consumer_handle, bytearray(),
72 deadline=100)
73 try:
74 self._runAndCheckError()
qsr 2015/03/03 17:10:36 You have construction in python unittest when you
blundell 2015/03/04 10:28:52 Done.
75 except data_pipe_utils.DataPipeCopyException:
76 pass
77 else:
78 assert 0
79
80 def testCloseProducerWithoutWriting(self):
81 self._copyDataFromPipe(self.handles.consumer_handle, bytearray())
82 self.handles.producer_handle.Close()
83 self._runAndCheckError()
84
85 def testCloseConsumerWithoutWriting(self):
qsr 2015/03/03 17:10:37 You should not have this test. This is kind of an
blundell 2015/03/04 10:28:52 Done.
86 self._copyDataFromPipe(self.handles.consumer_handle, bytearray())
87 self.handles.consumer_handle.Close()
88 try:
89 self._runAndCheckError()
90 except data_pipe_utils.DataPipeCopyException:
91 pass
92 else:
93 assert 0
94
95 def testEagerWriteOfEmptyData(self):
96 self._testEagerWrite(bytearray())
97
98 def testDelayedWriteOfEmptyData(self):
99 self._testDelayedWrite(bytearray())
100
101 def testEagerWriteOfNonEmptyData(self):
102 self._testEagerWrite(_GetRandomBuffer(1024))
103
104 def testDelayedWriteOfNonEmptyData(self):
105 self._testDelayedWrite(_GetRandomBuffer(1024))
106
107 def testEagerWriteOfLargeBuffer(self):
108 self._testEagerWrite(_GetRandomBuffer(32 * 1024))
109
110 def testDelayedWriteOfLargeBuffer(self):
111 self._testDelayedWrite(_GetRandomBuffer(32 * 1024))
OLDNEW
« mojo/python/mojo_utils/data_pipe_utils.py ('K') | « mojo/python/mojo_utils/data_pipe_utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698