Example #1
0
    def test_keyboard_interrupt(self):
        bus = ProcessBus()

        Handler.bus = bus
        service = WebService(address=('127.0.0.1', 38002),
                             handler_class=Handler)
        adapter = servers.ServerPlugin(bus, service, service.address)
        adapter.subscribe()

        # Raise a keyboard interrupt in the HTTP server's main thread.
        bus.transition('RUN')
        resp = service.do_GET('/ctrlc')
        assertEqual(resp.status, 200)
        bus.block()
        assertEqual(bus.state, 'EXITED')
Example #2
0
    def test_block(self):
        b = ProcessBus()
        self.log(b)

        def f():
            time.sleep(0.2)
            b.transition('EXITED')

        def g():
            time.sleep(0.4)

        def main_listener():
            main_calls.append(1)
        main_calls = []
        b.subscribe("main", main_listener)

        f_thread = threading.Thread(target=f, name='f')
        f_thread.start()
        threading.Thread(target=g, name='g').start()
        threads = [t for t in threading.enumerate() if not t.daemon]
        self.assertEqual(len(threads), 3)

        b.block()
        f_thread.join()

        # The block method MUST wait for the EXITED state.
        self.assertEqual(b.state, 'EXITED')
        # The block method MUST wait for ALL non-main, non-daemon threads to
        # finish.
        threads = [t for t in threading.enumerate() if not t.daemon]
        self.assertEqual(len(threads), 1)
        # The last message will mention an indeterminable thread name; ignore
        # it
        self.assertEqual(
            [entry for entry in self._log_entries
             if not entry.startswith('Publishing')
             and not entry.startswith('Waiting')],
            [
                'Bus state: ENTER',
                'Bus state: IDLE',
                'Bus state: EXIT',
                'Bus state: EXITED'
            ]
        )

        # While the bus was blocked, it should have published periodically
        # to the "main" channel.
        self.assertGreater(len(main_calls), 0)