def test_immediate_polling_with_local_clock_unsynced(self):
        """First poll happens with minimal delay if local clock is way off from
        the remote/server clock."""

        with Client('endpoint', 'token') as client:
            badnow = datetime_in_future(100)
            client.session = mock.Mock()
            client.session.post = lambda path, _: choose_reply(
                path,
                {'endpoint/problems/': '[%s]' % continue_reply('1', 'abc123')},
                date=badnow)
            client.session.get = lambda path: choose_reply(path, {
                'endpoint/problems/?id=1':
                '[%s]' % complete_no_answer_reply('1', 'abc123'),
                'endpoint/problems/1/':
                complete_reply('1', 'abc123')
            },
                                                           date=badnow)

            solver = Solver(client, solver_data('abc123'))

            def assert_no_delay(s):
                s and self.assertTrue(
                    abs(s - client._POLL_BACKOFF_MIN) <
                    client._POLL_BACKOFF_MIN / 10.0)

            with mock.patch('time.sleep', assert_no_delay):
                future = solver.sample_qubo({})
                future.result()
    def test_eta_min_is_respected_on_first_poll(self):
        "eta_min/earliest_estimated_completion should be respected if present in response"

        with Client('endpoint', 'token') as client:
            now = datetime_in_future(0)
            eta_min, eta_max = datetime_in_future(10), datetime_in_future(30)
            client.session = mock.Mock()
            client.session.post = lambda path, _: choose_reply(path, {
                'endpoint/problems/':
                '[%s]' % continue_reply(
                    '1', 'abc123', eta_min=eta_min, eta_max=eta_max, now=now)
            },
                                                               date=now)
            client.session.get = lambda path: choose_reply(path, {
                'endpoint/problems/?id=1':
                '[%s]' % complete_no_answer_reply('1', 'abc123'),
                'endpoint/problems/1/':
                complete_reply('1', 'abc123')
            },
                                                           date=now)

            solver = Solver(client, solver_data('abc123'))

            def assert_min_eta(s):
                s and self.assertTrue(abs(s - 10) < 1)

            with mock.patch('time.sleep', assert_min_eta):
                future = solver.sample_qubo({})
                future.result()
Esempio n. 3
0
    def test_exponential_backoff_polling(self):
        "After each poll, back-off should double"

        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            # on submit, return status pending
            client.session.post = lambda path, _: choose_reply(path, {
                'endpoint/problems/': '[%s]' % continue_reply('123', 'abc123')
            })
            # on first and second status poll, return pending
            # on third status poll, return completed
            def continue_then_complete(path, state={'count': 0}):
                state['count'] += 1
                if state['count'] < 3:
                    return choose_reply(path, {
                        'endpoint/problems/?id=123': '[%s]' % continue_reply('123', 'abc123'),
                        'endpoint/problems/123/': continue_reply('123', 'abc123')
                    })
                else:
                    return choose_reply(path, {
                        'endpoint/problems/?id=123': '[%s]' % complete_no_answer_reply('123', 'abc123'),
                        'endpoint/problems/123/': complete_reply('123', 'abc123')
                    })

            client.session.get = continue_then_complete

            solver = Solver(client, solver_data('abc123'))

            future = solver.sample_qubo({})
            future.result()

            # after third poll, back-off interval should be 4 x initial back-off
            self.assertEqual(future._poll_backoff, Client._POLL_BACKOFF_MIN * 2**2)
Esempio n. 4
0
    def test_immediate_polling_without_eta_min(self):
        "First poll happens with minimal delay if eta_min missing"

        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda path, _: choose_reply(path, {
                'endpoint/problems/': '[%s]' % continue_reply('1', 'abc123')
            })
            client.session.get = lambda path: choose_reply(path, {
                'endpoint/problems/?id=1': '[%s]' % complete_no_answer_reply('1', 'abc123'),
                'endpoint/problems/1/': complete_reply('1', 'abc123')
            })

            solver = Solver(client, solver_data('abc123'))

            def assert_no_delay(s):
                s and self.assertTrue(
                    abs(s - client._POLL_BACKOFF_MIN) < client._POLL_BACKOFF_MIN / 10.0)

            with mock.patch('time.sleep', assert_no_delay):
                future = solver.sample_qubo({})
                future.result()