Exemple #1
0
    def test_start_stop(self):
        """Test ScheduleBroadcasts() start/stop."""

        # Create objects for scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        scheduler = ScheduleBroadcasts(data.queue)

        # Start scheduling data.
        self.assertTrue(scheduler.start())
        self.assertTrue(scheduler.is_alive())
        self.assertFalse(scheduler.start())

        # Stop scheduling data.
        self.assertTrue(scheduler.stop())
        self.assertFalse(scheduler.is_alive())
        self.assertFalse(scheduler.stop())

        # Allow threads to fully shut down.
        time.sleep(0.1)
Exemple #2
0
    def test_start_stop(self):
        """Test ScheduleBroadcasts() start/stop."""

        # Create objects for scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        scheduler = ScheduleBroadcasts(data.queue)

        # Start scheduling data.
        self.assertTrue(scheduler.start())
        self.assertTrue(scheduler.is_alive())
        self.assertFalse(scheduler.start())

        # Stop scheduling data.
        self.assertTrue(scheduler.stop())
        self.assertFalse(scheduler.is_alive())
        self.assertFalse(scheduler.stop())

        # Allow threads to fully shut down.
        time.sleep(0.1)
Exemple #3
0
    def schedule(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data):
            buffer_A.append(data)

        def callback_B(data):
            buffer_B.append(data)

        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for reading and scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        if not speed:
            speed = 1.0
            scheduler = ScheduleBroadcasts(data.queue)
        else:
            scheduler = ScheduleBroadcasts(data.queue, speed=speed)
        scheduler.start()

        # Wait for broadcasts to end.
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if scheduler.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.1)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)
Exemple #4
0
    def schedule(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data): buffer_A.append(data)
        def callback_B(data): buffer_B.append(data)
        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for reading and scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        if not speed:
            speed = 1.0
            scheduler = ScheduleBroadcasts(data.queue)
        else:
            scheduler = ScheduleBroadcasts(data.queue, speed=speed)
        scheduler.start()

        # Wait for broadcasts to end.
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if scheduler.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.1)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)