def test_fails_active_reconnect_op(self, stage, create_transport, callback, fake_exception): op = pipeline_ops_base.ReconnectOperation(callback=callback) callback.reset_mock() stage.run_op(op) assert callback.call_count == 0 stage.transport.on_mqtt_connection_failure_handler(fake_exception) assert_callback_failed(op=op, error=fake_exception)
def test_calls_callback_with_error_if_third_op_and_finally_op_both_fail( self, stage, op, op2, op3, finally_op, callback ): op3.action = "fail" finally_op.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert_callback_failed(callback=callback, op=finally_op, error=op3.error)
def test_calls_callbacK_with_error_when_first_op_and_finally_op_both_fail( self, stage, op, op2, op3, finally_op, callback ): op.action = "fail" finally_op.action = "fail" run_ops_in_serial(stage, op, op2, op3, finally_op=finally_op, callback=callback) assert_callback_failed(callback=callback, op=finally_op, error=finally_op.error)
def test_calls_callback_on_timeout(self, stage, mock_timer, yes_timeout_op): stage.run_op(yes_timeout_op) timer_callback = mock_timer.call_args[0][1] timer_callback() assert_callback_failed(op=yes_timeout_op, error=pipeline_exceptions.PipelineTimeoutError)
def test_set_connection_args_raises_exception(self, stage, mocker, arbitrary_exception, set_security_client_args): stage.next._execute_op = mocker.Mock(side_effect=arbitrary_exception) stage.run_op(set_security_client_args) assert_callback_failed(op=set_security_client_args, error=arbitrary_exception)
def test_set_security_client_raises_exception(self, mocker, security_stage, fake_exception, set_security_client): security_stage.next._execute_op = mocker.Mock( side_effect=fake_exception) security_stage.run_op(set_security_client) assert_callback_failed(op=set_security_client, error=fake_exception)
def test_set_connection_args_raises_exception(self, mock_stage, mocker, some_exception, set_security_client_args): mock_stage.next._run_op = mocker.Mock(side_effect=some_exception) mock_stage.run_op(set_security_client_args) assert_callback_failed(op=set_security_client_args, error=some_exception)
def test_next_stage_returns_error(self, stage, op, arbitrary_exception): def next_stage_run_op(self, op): op.callback(op, error=arbitrary_exception) stage.next.run_op = functools.partial(next_stage_run_op, (stage.next,)) stage.run_op(op) assert_callback_failed(op=op, error=arbitrary_exception)
def test_completes_op_with_error(self, mocker, stage, op, fake_exception, callback): op.error = fake_exception op.callback = callback pass_op_to_next_stage(stage, op) assert_callback_failed(op=op, error=fake_exception) assert stage.next.run_op.call_count == 0
def test_next_stage_raises_exception(self, stage, op, arbitrary_exception, mocker): # Although stage.next.run_op is already a mocker.spy (i.e. a MagicMock) as a result of the # fixture config, in Python 3.4 setting the side effect directly results in a TypeError # (it is unclear as to why at this time) stage.next.run_op = mocker.MagicMock(side_effect=arbitrary_exception) stage.run_op(op) assert_callback_failed(op=op, error=arbitrary_exception)
def test_new_op_raises_exception(self, params, mocker, mock_stage, stages_configured, op, arbitrary_exception): mock_stage.next._execute_op = mocker.Mock( side_effect=arbitrary_exception) mock_stage.run_op(op) assert_callback_failed(op=op, error=arbitrary_exception)
def test_fails_operation( self, mocker, stage, create_transport, op_disconnect, arbitrary_exception ): stage.transport.disconnect.side_effect = arbitrary_exception stage.run_op(op_disconnect) assert_callback_failed(op=op_disconnect, error=arbitrary_exception) assert stage._pending_connection_op is None
def test_next_stage_returns_status_over_300(self, stage, op): def next_stage_run_op(self, op): op.status_code = 400 op.callback(op, error=None) stage.next.run_op = functools.partial(next_stage_run_op, (stage.next,)) stage.run_op(op) assert_callback_failed(op=op, error=ServiceError)
def test_fails_pending_reconnect_op(self, mocker, stage, create_transport, arbitrary_exception): op = pipeline_ops_base.ReconnectOperation(callback=mocker.MagicMock()) stage.run_op(op) assert op.callback.call_count == 0 assert stage._pending_connection_op is op stage.transport.on_mqtt_connection_failure_handler(arbitrary_exception) assert_callback_failed(op=op, error=arbitrary_exception) assert stage._pending_connection_op is None
def test_connect_failure(self, params, op, stage, arbitrary_exception): stage.pipeline_root.connected = False stage.run_op(op) connect_op = stage.next.run_op.call_args[0][0] stage.next._complete_op(connect_op, error=arbitrary_exception) assert_callback_failed(op=op, error=arbitrary_exception)
def test_connect_failure(self, params, op, stage, fake_exception): stage.pipeline_root.connected = False stage.run_op(op) connect_op = stage.next.run_op.call_args[0][0] connect_op.error = fake_exception operation_flow.complete_op(stage=stage.next, op=connect_op) assert_callback_failed(op=op, error=fake_exception)
def test_fails_blocked_op_if_serialized_op_fails(self, params, stage, connection_op, fake_op, arbitrary_exception): stage.pipeline_root.connected = params[ "connected_flag_required_to_run"] stage.run_op(connection_op) stage.run_op(fake_op) stage.next._complete_op(connection_op, error=arbitrary_exception) assert_callback_failed(op=fake_op, error=arbitrary_exception)
def test_next_stage_returns_status_over_300(self, stage, op): def next_stage_run_op(self, op): op.status_code = 400 # TODO: should this have a body? Should with/without be a separate test? op.response_body = json.dumps("").encode("utf-8") op.callback(op, error=None) stage.next.run_op = functools.partial(next_stage_run_op, (stage.next,)) stage.run_op(op) assert_callback_failed(op=op, error=ServiceError)
def test_returns_worker_op_failure_in_original_op( self, stage, arbitrary_op, arbitrary_worker_op, arbitrary_exception, next_stage_raises_arbitrary_exception, ): stage._send_worker_op_down(worker_op=arbitrary_worker_op, op=arbitrary_op) assert_callback_failed(arbitrary_op, error=arbitrary_exception)
def test_calls_callback_on_arbitrary_exception( self, stage, mock_timer, yes_timeout_op, next_stage_raises_arbitrary_exception, arbitrary_exception, ): stage.run_op(yes_timeout_op) assert_callback_failed(op=yes_timeout_op, error=arbitrary_exception)
def test_next_stage_returns_error(self, stage, op): error = Exception() def next_stage_run_op(self, op): op.error = error op.callback(op) stage.next.run_op = functools.partial(next_stage_run_op, (stage.next,)) stage.run_op(op) assert_callback_failed(op=op, error=error)
def test_fails_blocked_op_if_serialized_op_fails(self, params, stage, connection_op, fake_op, fake_exception): stage.pipeline_root.connected = params[ "connected_flag_required_to_run"] stage.run_op(connection_op) stage.run_op(fake_op) connection_op.error = fake_exception operation_flow.complete_op(stage=stage.next, op=connection_op) assert_callback_failed(op=fake_op, error=fake_exception)
def test_calls_callback_on_retried_op_arbitrary_exception( self, stage, yes_retry_op, retry_error, mock_timer, arbitrary_exception, mocker): stage.run_op(yes_retry_op) stage.next.complete_op(op=yes_retry_op, error=retry_error) timer_callback = mock_timer.call_args[0][1] timer_callback() stage.next.complete_op(op=yes_retry_op, error=arbitrary_exception) assert_callback_failed(op=yes_retry_op, error=arbitrary_exception)
def test_completes_pending_disconnect_op_with_error( self, mocker, stage, create_transport, fake_exception): op = pipeline_ops_base.DisconnectOperation(callback=mocker.MagicMock()) stage.run_op(op) assert op.callback.call_count == 0 assert stage._pending_connection_op is op stage.transport.on_mqtt_disconnected_handler(fake_exception) assert_callback_failed(op=op, error=errors.ConnectionDroppedError) assert stage._pending_connection_op is None if six.PY3: assert op.error.__cause__ is fake_exception
def test_transport_function_throws_exception( self, stage, create_transport, params, fake_exception, op, transport_function_throws_exception, ): op.callback.reset_mock() stage.run_op(op) assert_callback_failed(op=op, error=fake_exception)
def test_completes_active_disconnect_op_when_error( self, stage, create_transport, callback, fake_exception ): op = pipeline_ops_base.DisconnectOperation(callback=callback) callback.reset_mock() stage.run_op(op) assert callback.call_count == 0 stage.transport.on_mqtt_disconnected_handler(fake_exception) assert_callback_failed(op=op) assert isinstance(op.error, errors.ConnectionDroppedError) if six.PY3: assert op.error.__cause__ == fake_exception
def test_fails_multiple_ops(self, params, stage, connection_op, fake_ops, arbitrary_exception): stage.pipeline_root.connected = params[ "connected_flag_required_to_run"] stage.run_op(connection_op) for op in fake_ops: stage.run_op(op) stage.next._complete_op(connection_op, error=arbitrary_exception) for op in fake_ops: assert_callback_failed(op=op, error=arbitrary_exception)
def test_returns_set_sas_token_or_set_client_certificate_failure( self, fake_exception, stage, set_auth_provider): def next_run_op(self, op): if isinstance(op, pipeline_ops_iothub.SetAuthProviderArgsOperation): op.callback(op) else: raise fake_exception stage.next._run_op = functools.partial(next_run_op, stage) stage.run_op(set_auth_provider) assert_callback_failed(op=set_auth_provider, error=fake_exception)
def test_pending_operation_cancelled(self, mocker, stage, create_transport, op_disconnect, pending_connection_op): pending_connection_op.callback = mocker.MagicMock() stage._pending_connection_op = pending_connection_op stage.run_op(op_disconnect) # Callback has been completed, with an OperationCancelled exception set indicating early cancellation assert_callback_failed(op=pending_connection_op, error=pipeline_exceptions.OperationCancelled) # New operation is now the pending operation assert stage._pending_connection_op is op_disconnect
def test_set_sas_token_or_set_client_certificate_raises_exception( self, mocker, arbitrary_exception, stage, set_auth_provider, params_auth_provider_ops): if params_auth_provider_ops["name"] == "sas_token_auth": set_auth_provider.auth_provider.get_current_sas_token = mocker.Mock( side_effect=arbitrary_exception) elif "x509_auth" in params_auth_provider_ops["name"]: set_auth_provider.auth_provider.get_x509_certificate = mocker.Mock( side_effect=arbitrary_exception) stage.run_op(set_auth_provider) assert_callback_failed(op=set_auth_provider, error=arbitrary_exception)