Skip to content

Guard against infinite expectation loops, especially in global handlers #3

@ngsilverman

Description

@ngsilverman

Using expectations instead of handlers, especially the global one, could cause infinite loops when they fail.

expect.onGlobalFail = object : GlobalExpectationHandler() {
    override fun handleFail(exception: ExpectationException) {
        expect.fail("this will trigger an infinite loop as soon as an expectation fails")
    }
}

This is easy to avoid when the global handler logic is simple, which I expect to be the majority of cases, but it could be done inadvertently for more complex handlers who make various function calls, for example to disable parts of the application.

Since the purpose of the library is in part to be able to recover critical failures, it would be unfortunate to cause one. Ideally we would detect loops that are likely infinite and end them before a stack overflow. The approach could perhaps consist of detecting if multiple calls to handleFail are occurring without any of the calls returning. By inspecting the ExpectationException one could make an educated guess as to whether the same expectation is failing in a loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions