Exemplo n.º 1
0
    def test_should_not_call_shutdown_until_every_event_has_finished(
            self, with_web3):
        # given
        self.event1_finished = False
        self.event2_finished = False

        def shutdown_callback():
            assert self.event1_finished
            assert self.event2_finished

        def event_callback_1():
            time.sleep(1)
            lifecycle.terminate("Unit test is over")
            time.sleep(4)
            self.event1_finished = True

        def event_callback_2():
            time.sleep(2)
            self.event2_finished = True

        # expect
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.on_event(Event(), 1, event_callback_1)
                lifecycle.on_event(Event(), 1, event_callback_2)
                lifecycle.on_shutdown(
                    shutdown_callback)  # assertions are in `shutdown_callback`
Exemplo n.º 2
0
    def test_should_call_startup_callback(self, with_web3):
        # given
        startup_mock = MagicMock()

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.on_startup(startup_mock)

        # then
        startup_mock.assert_called()
Exemplo n.º 3
0
    def test_should_start_instantly_if_no_initial_delay(self, with_web3):
        # given
        start_time = int(time.time())

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                pass

        # then
        end_time = int(time.time())
        assert end_time - start_time <= 2
Exemplo n.º 4
0
    def test_should_obey_initial_delay(self, with_web3):
        # given
        start_time = int(time.time())

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.initial_delay(5)

        # then
        end_time = int(time.time())
        assert end_time - start_time >= 4
Exemplo n.º 5
0
    def main(self):
        """ Initialize the lifecycle and enter into the Keeper Lifecycle controller

        Each function supplied by the lifecycle will accept a callback function that will be executed.
        The lifecycle.on_block() function will enter into an infinite loop, but will gracefully shutdown
        if it recieves a SIGINT/SIGTERM signal.

        """
        with Lifecycle(self.web3) as lifecycle:
            self.lifecycle = lifecycle
            lifecycle.on_startup(self.check_deployment)
            lifecycle.on_block(self.process_block)
Exemplo n.º 6
0
    def test_should_time_out_initial_checks_even_if_they_constantly_return_false(
            self, with_web3):
        # given
        start_time = int(time.time())

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.wait_for(lambda: False, 5)

        # then
        end_time = int(time.time())
        assert end_time - start_time >= 4
Exemplo n.º 7
0
    def test_should_check_initial_checks(self, with_web3):
        # given
        check_1 = Mock(return_value=True)
        check_2 = Mock(return_value=True)

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.wait_for(check_1, 5)
                lifecycle.wait_for(check_2, 5)

        # then
        assert check_1.call_count == 1
        assert check_2.call_count == 1
Exemplo n.º 8
0
    def test_should_call_shutdown_callback(self, with_web3):
        # given
        ordering = []
        startup_mock = MagicMock(
            side_effect=lambda: ordering.append('STARTUP'))
        shutdown_mock = MagicMock(
            side_effect=lambda: ordering.append('SHUTDOWN'))

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.on_startup(startup_mock)
                lifecycle.on_shutdown(shutdown_mock)

        # then
        assert ordering == ['STARTUP', 'SHUTDOWN']
Exemplo n.º 9
0
    def test_every(self, with_web3):
        self.counter = 0

        def callback():
            self.counter = self.counter + 1
            if self.counter >= 2:
                lifecycle.terminate("Unit test is over")

        # given
        mock = MagicMock(side_effect=callback)

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.every(1, mock)

        # then
        assert mock.call_count >= 2
        assert lifecycle.terminated_internally
Exemplo n.º 10
0
    def test_events_should_not_fire_when_keeper_is_already_terminating(
            self, with_web3):
        # given
        self.event_counter = 0

        def shutdown_callback():
            time.sleep(5)

        def event_callback():
            self.event_counter = self.event_counter + 1
            lifecycle.terminate("Unit test is over")

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.on_event(Event(), 1, event_callback)
                lifecycle.on_shutdown(shutdown_callback)

        # then
        assert self.event_counter <= 2
Exemplo n.º 11
0
    def test_on_event_fires_every_min_frequency_if_event_not_triggered(
            self, with_web3):
        self.counter = 0

        def callback():
            self.counter = self.counter + 1
            if self.counter >= 2:
                lifecycle.terminate("Unit test is over")

        # given
        mock = MagicMock(side_effect=callback)

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.on_event(Event(), 1, mock)

        # then
        assert mock.call_count >= 2
        assert lifecycle.terminated_internally
Exemplo n.º 12
0
    def test_event_does_not_start_operating_until_startup_callback_is_finished(
            self, with_web3):
        # given
        self.event_triggered = False

        def startup_callback():
            time.sleep(3)
            assert not self.event_triggered

        def event_callback():
            self.event_triggered = True
            lifecycle.terminate("Unit test is over")

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.on_startup(startup_callback)
                lifecycle.on_event(Event(), 1, event_callback)

        # then
        assert self.event_triggered
Exemplo n.º 13
0
    def test_on_event_fires_whenever_event_triggered(self, with_web3):
        event = Event()
        self.counter = 0

        def every_callback():
            self.counter = self.counter + 1
            trigger_event(event)
            if self.counter >= 2:
                time.sleep(1)
                lifecycle.terminate("Unit test is over")

        # given
        mock = Mock()

        # when
        with pytest.raises(SystemExit):
            with Lifecycle(self.use_web3(with_web3)) as lifecycle:
                lifecycle.every(1, every_callback)
                lifecycle.on_event(event, 9999, mock)

        # then
        assert mock.call_count >= 2
        assert lifecycle.terminated_internally
Exemplo n.º 14
0
 def main(self):
     with Lifecycle(web3) as lifecycle:
         lifecycle.on_shutdown(self.on_shutdown)
         lifecycle.on_block(self.on_block)
Exemplo n.º 15
0
 def test_should_fail_to_register_two_shutdown_callbacks(self, with_web3):
     # expect
     with pytest.raises(BaseException):
         with Lifecycle(self.use_web3(with_web3)) as lifecycle:
             lifecycle.on_shutdown(lambda: 1)
             lifecycle.on_shutdown(lambda: 2)
Exemplo n.º 16
0
 def test_should_fail_to_register_two_block_callbacks(self):
     # expect
     with pytest.raises(BaseException):
         with Lifecycle(self.web3) as lifecycle:
             lifecycle.on_block(lambda: 1)
             lifecycle.on_block(lambda: 2)
Exemplo n.º 17
0
 def test_should_fail_to_register_block_callback_if_no_web3(self):
     # expect
     with pytest.raises(BaseException):
         with Lifecycle() as lifecycle:
             lifecycle.on_block(lambda: 1)
Exemplo n.º 18
0
 def test_should_always_exit(self, with_web3):
     with pytest.raises(SystemExit):
         with Lifecycle(self.use_web3(with_web3)):
             pass