Exemple #1
0
    def test(self):
        handler = lambda_event.LambdaHandler()
        run = mock.Mock()

        self.assertFalse(handler.typecheck(base.Event(None)))

        event = lambda_event.LambdaEvent(run=run)
        self.assertTrue(handler.typecheck(event))
        self.assertTrue(handler.check(event))
        handler.run(event)
        run.assert_called()
Exemple #2
0
    def run_as_lambda(self) -> None:
        def run() -> None:
            # This runs on the event thread. Double-check the cancel flag, in case we got canceled
            # while this was queued.
            if self.cancelled.is_set():
                return
            self.run()

        # This runs on the timer thread, so it isn't delayed by running the actual lambda. Enqueue
        # the lambda, then immediately repeat if appropriate.
        self.timer_conn.on_event(lambda_event.LambdaEvent(run))
        if not self.cancelled.is_set():
            self.end_time += self.interval
            self.start_timer()
Exemple #3
0
    def run_as_lambda(self) -> None:
        def run() -> None:
            # This runs on the event thread. Double-check the cancel flag, in case we got canceled
            # while this was queued.
            if self.cancelled.is_set():
                return
            if self.end_time > datetime.datetime.now():
                # The time has been extended, so wait again.
                self.start_timer()
                return
            self.timer_conn.remove(self)
            try:
                self.run()
            finally:
                self.finished.set()

        self.timer_conn.on_event(lambda_event.LambdaEvent(run))
Exemple #4
0
    def test_repeating(self):
        def side_effect():
            self.counter += 1
            if self.counter == 3:
                self.timer.cancel()

        run = mock.Mock(side_effect=side_effect)

        def start_timer():
            with mock.patch('threading.Timer', new=self.new_mock_timer):
                self.timer = self.timer_conn.start_repeating(
                    datetime.timedelta(minutes=1), run)

        self.queue.put(lambda_event.LambdaEvent(start_timer))
        self.queue.join()
        self.timer.cancelled.wait()
        run.assert_has_calls([mock.call()] * 3)
Exemple #5
0
    def dispatch_request(self, *args: Any, **kwargs: Any) -> ViewResponse:
        q: queue.Queue[Union[ViewResponse, Exception]] = queue.Queue(maxsize=1)

        @flask.copy_current_request_context
        def run():
            try:
                q.put(self.subview(*args, **kwargs))
            except Exception as e:
                # If the subview raised an exception, we want to reraise it from dispatch_request,
                # so pass it over there via the queue.
                q.put(e)

        event = lambda_event.LambdaEvent(run)
        # We can cast away the Optional from on_event because it's set in the connection's run(),
        # before the Flask server is started.
        cast(base.EventCallback, self.connection.on_event)(event)
        result = q.get()
        if isinstance(result, Exception):
            raise RuntimeError from result
        return result