Esempio n. 1
0
    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
Esempio n. 2
0
    def test_nowait_function_exceptions(self):
        pipe = LocalPipe(layout="float32_3")
        subscriber = LocalPipe(layout="float32_3", close_cb=mock.Mock())
        fd_subscriber = OutputPipe()
        # cannot close_nowait if there is a callback
        with self.assertRaises(PipeError):
            subscriber.close_nowait()

        # can write_nowait if only localpipe subscribers
        pipe.subscribe(subscriber)
        pipe.write_nowait(np.ones((100, 4)))
        # cannot if any subscribers are not localpipes
        pipe.subscribe(fd_subscriber)
        with self.assertRaises(PipeError):
            pipe.write_nowait(np.ones((100, 4)))

        # cannot close_nowait if there are subscribers
        pipe.subscribe(subscriber)
        with self.assertRaises(PipeError):
            pipe.close_nowait()
Esempio n. 3
0
    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)