def test_handles_interval_breaks(self): LAYOUT = "int32_3" LENGTH = 1000 my_pipe = LocalPipe(LAYOUT, name="pipe") test_data1 = helpers.create_data(LAYOUT, length=LENGTH) test_data2 = helpers.create_data(LAYOUT, length=LENGTH) my_pipe.write_nowait(test_data1) my_pipe.close_interval_nowait() my_pipe.write_nowait(test_data2) async def reader(): # read the first interval read_data = await my_pipe.read() self.assertTrue(my_pipe.end_of_interval) my_pipe.consume(len(read_data) - 20) np.testing.assert_array_equal(test_data1, read_data) # read the second interval read_data = await my_pipe.read() self.assertFalse(my_pipe.end_of_interval) self.assertEqual(len(read_data), len(test_data2) + 20) my_pipe.consume(len(read_data)) np.testing.assert_array_equal(test_data2, read_data[20:]) loop = asyncio.get_event_loop() loop.run_until_complete(reader())
def subscribe(self, stream: DataStream, pipe: pipes.LocalPipe): if self.raise_error: raise SubscriptionError() self.subscribed_stream = stream while True: try: data = self.subscription_pipe.read_nowait() self.subscription_pipe.consume(len(data)) pipe.write_nowait(data) except pipes.EmptyPipe: if not self.hang_pipe: pipe.close_nowait() break if self.subscription_pipe.end_of_interval: pipe.close_interval_nowait() # return a mock as the unsubscribe callback return self.unsubscribe
def test_read_all_no_flatten(self): LAYOUT = "int32_3" LENGTH = 1000 loop = asyncio.get_event_loop() # raises exception if the pipe is empty my_pipe = LocalPipe(LAYOUT, name="pipe") my_pipe.close_nowait() with self.assertRaises(PipeError): loop.run_until_complete(my_pipe.read_all()) # read_all empties pipe and closes it, regardless of intervals my_pipe = LocalPipe(LAYOUT, name="pipe") test_data1 = helpers.create_data(LAYOUT, length=LENGTH) test_data2 = helpers.create_data(LAYOUT, length=LENGTH) my_pipe.write_nowait(test_data1) my_pipe.close_interval_nowait() my_pipe.write_nowait(test_data2) my_pipe.close_nowait() actual_data = loop.run_until_complete(my_pipe.read_all()) expected_data = np.hstack((test_data1, test_data2)) np.testing.assert_array_equal(actual_data, expected_data) self.assertTrue(my_pipe.closed) # read_all only add maxrows to the pipe # (less than one read) my_pipe = LocalPipe(LAYOUT, name="pipe") test_data1 = helpers.create_data(LAYOUT, length=LENGTH) test_data2 = helpers.create_data(LAYOUT, length=LENGTH) my_pipe.write_nowait(test_data1) my_pipe.close_interval_nowait() my_pipe.write_nowait(test_data2) actual_data = loop.run_until_complete(my_pipe.read_all(maxrows=103)) expected_data = test_data1[:103] np.testing.assert_array_equal(actual_data, expected_data) self.assertTrue(my_pipe.closed) # (more than one read) my_pipe = LocalPipe(LAYOUT, name="pipe") test_data1 = helpers.create_data(LAYOUT, length=LENGTH) test_data2 = helpers.create_data(LAYOUT, length=LENGTH) my_pipe.write_nowait(test_data1) my_pipe.close_interval_nowait() my_pipe.write_nowait(test_data2) actual_data = loop.run_until_complete( my_pipe.read_all(maxrows=LENGTH + 101)) expected_data = np.hstack((test_data1, test_data2[:101])) np.testing.assert_array_equal(expected_data, actual_data) self.assertTrue(my_pipe.closed) # read_all raises an exception if the pipe has more than maxrows # (less than one read) my_pipe = LocalPipe(LAYOUT, name="pipe") test_data1 = helpers.create_data(LAYOUT, length=LENGTH) test_data2 = helpers.create_data(LAYOUT, length=LENGTH) my_pipe.write_nowait(test_data1) my_pipe.close_interval_nowait() my_pipe.write_nowait(test_data2) with self.assertRaises(PipeError): loop.run_until_complete( my_pipe.read_all(maxrows=LENGTH + 101, error_on_overflow=True)) self.assertTrue(my_pipe.closed) # (more than one read) my_pipe = LocalPipe(LAYOUT, name="pipe") test_data1 = helpers.create_data(LAYOUT, length=LENGTH) test_data2 = helpers.create_data(LAYOUT, length=LENGTH) my_pipe.write_nowait(test_data1) my_pipe.close_interval_nowait() my_pipe.write_nowait(test_data2) my_pipe.close_nowait() with self.assertRaises(PipeError): loop.run_until_complete( my_pipe.read_all(maxrows=LENGTH + 101, error_on_overflow=True)) self.assertTrue(my_pipe.closed)