def test_run_working_block(self): client = mock.MagicMock(spec=DownstreamClient) client.get_total_size.return_value = 0 client.desired_size = 100 client.api = API() contract = mock.MagicMock(spec=DownstreamContract) contract.time_remaining.return_value = 1 contract.hash = '1' contract.api = client.api def patch_get_chunk(size): client.get_total_size.return_value = \ client.get_total_size.return_value + \ size client.contracts = [contract] client.get_chunk.side_effect = patch_get_chunk client.get_next_contract.return_value = contract with mock.patch('time.sleep') as a: DownstreamClient.run(client, 1) self.assertEqual(client.get_chunk.call_count, 1) self.assertEqual(contract.update_challenge.call_count, 1) self.assertEqual(contract.answer_challenge.call_count, 1) self.assertEqual(a.call_count, 1)
def test_run_obtain_contract_fail(self): client = mock.MagicMock(spec=DownstreamClient) client.get_total_size.return_value = 0 client.desired_size = 100 client.api = API() client.get_chunk.side_effect = DownstreamError('test error') client.contracts = [] with self.assertRaises(DownstreamError) as ex: DownstreamClient.run(client, 1) self.assertEqual( str(ex.exception), 'Unable to obtain a contract: test error')
def test_run_answer_failed(self): client = mock.MagicMock(spec=DownstreamClient) client.get_total_size.return_value = 100 client.desired_size = 100 client.api = API() contract = mock.MagicMock(spec=DownstreamContract) contract.time_remaining.return_value = 0 contract.hash = '1' contract.api = client.api client.contracts = mock.MagicMock() client.contracts.remove = mock.MagicMock() contract.answer_challenge.side_effect = DownstreamError('test error') client.get_next_contract.return_value = contract DownstreamClient.run(client, 1) self.assertTrue(client.contracts.remove.called)