Skip to content

@Singleton instances are broken in parallel executionΒ #2961

Open
@loveleif

Description

@loveleif

πŸ‘“ What did you see?

@Singleton instances do not work in parallel execution, instead one instance per thread is created.

βœ… What did you expect to see?

I expected only one instance of @Singleton bound classes to be created:

#### Singleton scope
Cucumber will create just one instance of a class bound in singleton scope
that will last for the lifetime of all test scenarios in the test run. You
should use singleton scope if your classes are stateless. You can also use
singleton scope when your classes contain state but with caution. You should
be absolutely sure that a state change in one scenario could not possibly
influence the success or failure of a subsequent scenario. As an example of
when you might use a singleton, imagine you have an http client that is
expensive to create. By holding a reference to the client in a class bound in
singleton scope, you can reuse the client in multiple scenarios.

πŸ“¦ Which tool/library version are you using?

cucumber-jvm (core+guice) 7.20.1

πŸ”¬ How could we reproduce it?

Test case that reproduce the bug: loveleif@1a52642.

  1. Bind a class in Singleton scope.
  2. Inject the singleton class in a scenario.
  3. Start a test suite with parallel execution.
  4. There will be multiple instances created, depending on how many threads are used.

πŸ“š Any additional context?

My use case is query language testing for a database. Starting and stopping the database is time consuming, so we re-use the dbms between scenarios to drastically reduce execution time. A custom backend registers before/after all hooks to the glue that starts/stops the database (since regular before/after all hooks can't use dependency injection, but backends have access to Lookup).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions