def test_submit_continue_then_ok_reply(self):
        """Handle polling for a complete problem."""
        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda a, _: choose_reply(
                a, {
                    'endpoint/problems/': '[%s]' % continue_reply(
                        '123', 'abc123')
                })
            client.session.get = lambda a: choose_reply(
                a, {
                    'endpoint/problems/?id=123':
                    '[%s]' % complete_no_answer_reply('123', 'abc123'),
                    'endpoint/problems/123/':
                    complete_reply('123', 'abc123')
                })
            solver = Solver(client, solver_data('abc123'))

            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            results = solver.sample_ising(linear, quad, num_reads=100)

            #
            self._check(results, linear, quad, 100)
    def test_submit_continue_then_ok_reply(self):
        """Handle polling for a complete problem."""
        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 a, _: choose_reply(a, {
                'endpoint/problems/':
                '[%s]' % continue_reply(
                    '123', 'abc123', eta_min=eta_min, eta_max=eta_max, now=now)
            },
                                                            date=now)
            client.session.get = lambda a: choose_reply(a, {
                'endpoint/problems/?id=123':
                '[%s]' % complete_no_answer_reply('123', 'abc123'),
                'endpoint/problems/123/':
                complete_reply('123', 'abc123')
            },
                                                        date=now)
            solver = Solver(client, solver_data('abc123'))

            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            results = solver.sample_ising(linear, quad, num_reads=100)

            self._check(results, linear, quad, 100)

            # test future has eta_min and eta_max parsed correctly
            self.assertEqual(results.eta_min, eta_min)
            self.assertEqual(results.eta_max, eta_max)
Esempio n. 3
0
    def test_submit_continue_then_ok_and_error_reply(self):
        """Handle polling for the status of multiple problems."""

        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()

            # on first status poll, return pending for both problems
            # on second status poll, return error for first problem and complete for second
            def continue_then_complete(path, state={'count': 0}):
                state['count'] += 1
                if state['count'] < 2:
                    return choose_reply(path, {
                        'endpoint/problems/?id=1': '[{}]'.format(continue_reply('1', 'abc123')),
                        'endpoint/problems/?id=2': '[{}]'.format(continue_reply('2', 'abc123')),
                        'endpoint/problems/1/': continue_reply('1', 'abc123'),
                        'endpoint/problems/2/': continue_reply('2', 'abc123'),
                        'endpoint/problems/?id=1,2': '[{},{}]'.format(continue_reply('1', 'abc123'),
                                                                      continue_reply('2', 'abc123')),
                        'endpoint/problems/?id=2,1': '[{},{}]'.format(continue_reply('2', 'abc123'),
                                                                      continue_reply('1', 'abc123'))
                    })
                else:
                    return choose_reply(path, {
                        'endpoint/problems/?id=1': '[{}]'.format(error_reply('1', 'abc123', 'error')),
                        'endpoint/problems/?id=2': '[{}]'.format(complete_no_answer_reply('2', 'abc123')),
                        'endpoint/problems/1/': error_reply('1', 'abc123', 'error'),
                        'endpoint/problems/2/': complete_reply('2', 'abc123'),
                        'endpoint/problems/?id=1,2': '[{},{}]'.format(error_reply('1', 'abc123', 'error'),
                                                                      complete_no_answer_reply('2', 'abc123')),
                        'endpoint/problems/?id=2,1': '[{},{}]'.format(complete_no_answer_reply('2', 'abc123'),
                                                                      error_reply('1', 'abc123', 'error'))
                    })

            client.session.get = continue_then_complete

            def accept_problems_with_continue_reply(path, body, ids=iter('12')):
                problems = json.loads(body)
                return choose_reply(path, {
                    'endpoint/problems/': json.dumps(
                        [json.loads(continue_reply(next(ids), 'abc123')) for _ in problems])
                })

            client.session.post = accept_problems_with_continue_reply

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

            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}

            results1 = solver.sample_ising(linear, quad, num_reads=100)
            results2 = solver.sample_ising(linear, quad, num_reads=100)

            with self.assertRaises(SolverFailureError):
                self._check(results1, linear, quad, 100)
            self._check(results2, linear, quad, 100)
Esempio n. 4
0
    def test_submit_cancel_reply(self):
        """Handle a response for a canceled job."""
        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda a, _: choose_reply(a, {'endpoint/problems/': '[%s]' % cancel_reply('123', 'abc123')})
            solver = Solver(client, solver_data('abc123'))

            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            results = solver.sample_ising(linear, quad, num_reads=100)

            with self.assertRaises(CanceledFutureError):
                results.samples
Esempio n. 5
0
    def test_submit_null_reply(self):
        """Get an error when the server's response is incomplete."""
        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda a, _: choose_reply(a, {'endpoint/problems/': ''})
            solver = Solver(client, solver_data('abc123'))

            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            results = solver.sample_ising(linear, quad, num_reads=100)

            with self.assertRaises(ValueError):
                results.samples
Esempio n. 6
0
    def test_submit_continue_then_error_reply(self):
        """Handle polling for an error message."""
        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda a, _: choose_reply(a, {'endpoint/problems/': '[%s]' % continue_reply('123', 'abc123')})
            client.session.get = lambda a: choose_reply(a, {
                'endpoint/problems/?id=123': '[%s]' % error_reply('123', 'abc123', "error message")})
            solver = Solver(client, solver_data('abc123'))

            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            results = solver.sample_ising(linear, quad, num_reads=100)

            with self.assertRaises(SolverFailureError):
                self._check(results, linear, quad, 100)
Esempio n. 7
0
    def test_submit_error_reply(self):
        """Handle an error on problem submission."""
        error_body = 'An error message'
        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda a, _: choose_reply(a, {
                'endpoint/problems/': '[%s]' % error_reply('123', 'abc123', error_body)})
            solver = Solver(client, solver_data('abc123'))

            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            results = solver.sample_ising(linear, quad, num_reads=100)

            with self.assertRaises(SolverFailureError):
                results.samples
    def test_submit_immediate_error_reply(self):
        """Handle an (obvious) error on problem submission."""
        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.post = lambda a, _: choose_reply(
                a, {
                    'endpoint/problems/':
                    '[%s]' % immediate_error_reply(
                        400, "Missing parameter 'num_reads' in problem JSON")
                })
            solver = Solver(client, solver_data('abc123'))

            linear, quad = generate_random_ising_problem(solver)
            results = solver.sample_ising(linear, quad)

            with self.assertRaises(SolverFailureError):
                results.samples
    def test_cancel_without_id(self):
        """Make sure the cancel method submits to the right endpoint.

        When cancel is called before the submission has returned the problem id.
        """
        submission_id = 'test-id'
        reply_body = '[%s]' % continue_reply(submission_id, 'solver')

        release_reply = threading.Event()

        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.get = lambda a: choose_reply(
                a,
                {'endpoint/problems/?id={}'.format(submission_id): reply_body})

            def post(a, _):
                release_reply.wait()
                return choose_reply(
                    a,
                    {'endpoint/problems/'.format(submission_id): reply_body})

            client.session.post = post
            client.session.delete = DeleteEvent.handle

            solver = Solver(client, solver_data('abc123'))
            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}
            future = solver.sample_ising(linear, quad)
            future.cancel()

            try:
                release_reply.set()
                future.samples
                self.fail()
            except DeleteEvent as event:
                if event.url == 'endpoint/problems/':
                    self.assertEqual(event.body,
                                     '["{}"]'.format(submission_id))
                else:
                    self.assertEqual(
                        event.url,
                        'endpoint/problems/{}/'.format(submission_id))
Esempio n. 10
0
    def test_submit_continue_then_ok_and_error_reply(self):
        """Handle polling for the status of multiple problems."""
        # Reduce the number of poll threads to 1 so that the system can be tested
        old_value = Client._POLL_THREAD_COUNT
        Client._POLL_THREAD_COUNT = 1

        with Client('endpoint', 'token') as client:
            client.session = mock.Mock()
            client.session.get = lambda a: choose_reply(
                a,
                {
                    # Wait until both problems are
                    'endpoint/problems/?id=1':
                    '[%s]' % continue_reply('1', 'abc123'),
                    'endpoint/problems/?id=2':
                    '[%s]' % continue_reply('2', 'abc123'),
                    'endpoint/problems/?id=2,1':
                    '[' + complete_no_answer_reply('2', 'abc123') + ',' +
                    error_reply('1', 'abc123', 'error') + ']',
                    'endpoint/problems/?id=1,2':
                    '[' + error_reply('1', 'abc123', 'error') + ',' +
                    complete_no_answer_reply('2', 'abc123') + ']',
                    'endpoint/problems/1/':
                    error_reply('1', 'abc123', 'Error message'),
                    'endpoint/problems/2/':
                    complete_reply('2', 'abc123')
                })

            def switch_post_reply(path, body):
                message = json.loads(body)
                if len(message) == 1:
                    client.session.post = lambda a, _: choose_reply(
                        a, {
                            'endpoint/problems/':
                            '[%s]' % continue_reply('2', 'abc123')
                        })
                    return choose_reply(
                        '', {'': '[%s]' % continue_reply('1', 'abc123')})
                else:
                    client.session.post = None
                    return choose_reply(
                        'endpoint/', {
                            'endpoint/':
                            '[%s, %s]' % (continue_reply(
                                '1', 'abc123'), continue_reply('2', 'abc123'))
                        })

            client.session.post = lambda a, body: switch_post_reply(a, body)

            solver = Solver(client, solver_data('abc123'))
            # Build a problem
            linear = {index: 1 for index in solver.nodes}
            quad = {key: -1 for key in solver.undirected_edges}

            results1 = solver.sample_ising(linear, quad, num_reads=100)
            results2 = solver.sample_ising(linear, quad, num_reads=100)

            with self.assertRaises(SolverFailureError):
                self._check(results1, linear, quad, 100)
            self._check(results2, linear, quad, 100)

        Client._POLL_THREAD_COUNT = old_value