Skip to content

Commit

Permalink
[Core] Support custom UUID generators in test runners
Browse files Browse the repository at this point in the history
With #2703 a faster UUID generator was introduced. And while the
configuration options were added, they were not actually used by
`cucumber-junit`, `cucumber-junit-platform-engine` and
`cucumber-testng`.
  • Loading branch information
mpkorstanje committed Sep 21, 2024
1 parent 493220d commit f580b71
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
[JUnit Platform Engine] Enable use of custom UUID generators ([#2887](https://github.com/cucumber/cucumber-jvm/pull/2926) M.P. Korstanje)
[JUnit] Enable use of custom UUID generators ([#2887](https://github.com/cucumber/cucumber-jvm/pull/2926) M.P. Korstanje)
[TestNG] Enable use of custom UUID generators ([#2887](https://github.com/cucumber/cucumber-jvm/pull/2926) M.P. Korstanje)


## [7.19.0] - 2024-09-19
### Changed
Expand Down
8 changes: 5 additions & 3 deletions cucumber-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ cucumber.plugin= # comma separated plugin strings.
cucumber.object-factory= # object factory class name.
# example: com.example.MyObjectFactory
cucumber.uuid-generator= # UUID generator class name.
cucumber.uuid-generator # uuid generator class name of a registered service provider.
# default: io.cucumber.core.eventbus.RandomUuidGenerator
# example: com.example.MyUuidGenerator
cucumber.publish.enabled # true or false. default: false
Expand Down Expand Up @@ -89,12 +90,13 @@ Cucumber emits events on an event bus in many cases:
- during the feature file parsing
- when the test scenarios are executed

An event has a UUID. The UUID generator can be configured using the `cucumber.uuid-generator` property:
An event has a UUID. The UUID generator can be configured using the
`cucumber.uuid-generator` property:

| UUID generator | Features | Performance [Millions UUID/second] | Typical usage example |
|-----------------------------------------------------|-----------------------------------------|------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| io.cucumber.core.eventbus.RandomUuidGenerator | Thread-safe, collision-free, multi-jvm | ~1 | Reports may be generated on different JVMs at the same time. A typical example would be one suite that tests against Firefox and another against Safari. The exact browser is configured through a property. These are then executed concurrently on different Gitlab runners. |
| io.cucumber.core.eventbus.IncrementingUuidGenerator | Thread-safe, collision-free, single-jvm | ~130 | Reports are generated on a single JVM |
| io.cucumber.core.eventbus.IncrementingUuidGenerator | Thread-safe, collision-free, single-jvm | ~130 | Reports are generated on a single JVM in a single execution of Cucumber. |

The performance gain on real projects depends on the feature size.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
* Thread-safe and collision-free UUID generator for single JVM. This is a
* sequence generator and each instance has its own counter. This generator is
* about 100 times faster than #RandomUuidGenerator.
*
* Properties:
* - thread-safe
* - collision-free in the same classloader
* - almost collision-free in different classloaders / JVMs
* - UUIDs generated using the instances from the same classloader are sortable
*
* UUID version 8 (custom) / variant 2 <a href=
* "https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-uuid-version-8">...</a>
* <!-- @formatter:off -->
* <p>
* Properties: - thread-safe - collision-free in the same classloader - almost
* collision-free in different classloaders / JVMs - UUIDs generated using the
* instances from the same classloader are sortable
* <p>
* <a href=
* "https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-uuid-version-8">UUID
* version 8 (custom) / variant 2 </a>
*
* <pre>
* | 40 bits | 8 bits | 4 bits | 12 bits | 2 bits | 62 bits |
* | -------------------| -------------- | ------- | ------------- | ------- | ------- |
* | LSBs of epoch-time | sessionCounter | version | classloaderId | variant | counter |
* <!-- @formatter:on -->
* </pre>
*/
public class IncrementingUuidGenerator implements UuidGenerator {
/**
Expand Down Expand Up @@ -84,7 +84,7 @@ public class IncrementingUuidGenerator implements UuidGenerator {
* classloaderId which produces about 1% collision rate on the
* classloaderId, and thus can have UUID collision if the epoch-time,
* session counter and counter have the same values).
*
*
* @param classloaderId the new classloaderId (only the least significant 12
* bits are used)
* @see IncrementingUuidGenerator#classloaderId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public UuidGeneratorServiceLoader(Supplier<ClassLoader> classLoaderSupplier, Opt
this.options = requireNonNull(options);
}

UuidGenerator loadUuidGenerator() {
public UuidGenerator loadUuidGenerator() {
Class<? extends UuidGenerator> objectFactoryClass = options.getUuidGeneratorClass();
ClassLoader classLoader = classLoaderSupplier.get();
ServiceLoader<UuidGenerator> loader = ServiceLoader.load(UuidGenerator.class, classLoader);
Expand Down
4 changes: 4 additions & 0 deletions cucumber-junit-platform-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ cucumber.junit-platform.naming-strategy.long.example-name= # number or pickl
cucumber.plugin= # comma separated plugin strings.
# example: pretty, json:path/to/report.json
cucumber.uuid-generator # uuid generator class name of a registered service provider.
# default: io.cucumber.core.eventbus.RandomUuidGenerator
# example: com.example.MyUuidGenerator
cucumber.object-factory= # object factory class name.
# example: com.example.MyObjectFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import io.cucumber.core.runtime.ThreadLocalObjectFactorySupplier;
import io.cucumber.core.runtime.ThreadLocalRunnerSupplier;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.core.runtime.UuidGeneratorServiceLoader;
import org.apiguardian.api.API;
import org.junit.platform.engine.ConfigurationParameters;
import org.junit.platform.engine.support.hierarchical.EngineExecutionContext;

import java.time.Clock;
import java.util.UUID;
import java.util.function.Supplier;

import static io.cucumber.core.runtime.SynchronizedEventBus.synchronize;
Expand All @@ -48,8 +48,10 @@ CucumberEngineOptions getOptions() {

private CucumberExecutionContext createCucumberExecutionContext() {
Supplier<ClassLoader> classLoader = CucumberEngineExecutionContext.class::getClassLoader;
UuidGeneratorServiceLoader uuidGeneratorServiceLoader = new UuidGeneratorServiceLoader(classLoader, options);
EventBus bus = synchronize(
new TimeServiceEventBus(Clock.systemUTC(), uuidGeneratorServiceLoader.loadUuidGenerator()));
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(classLoader, options);
EventBus bus = synchronize(new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID));
Plugins plugins = new Plugins(new PluginFactory(), options);
ExitStatus exitStatus = new ExitStatus(options);
plugins.addPlugin(exitStatus);
Expand Down
9 changes: 6 additions & 3 deletions cucumber-junit/src/main/java/io/cucumber/junit/Cucumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.cucumber.core.runtime.ThreadLocalObjectFactorySupplier;
import io.cucumber.core.runtime.ThreadLocalRunnerSupplier;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.core.runtime.UuidGeneratorServiceLoader;
import org.apiguardian.api.API;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -38,7 +39,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.function.Supplier;

Expand Down Expand Up @@ -146,11 +146,14 @@ public Cucumber(Class<?> clazz) throws InitializationError {
.parse(CucumberProperties.fromSystemProperties())
.build(junitEnvironmentOptions);

this.bus = synchronize(new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID));
Supplier<ClassLoader> classLoader = ClassLoaders::getDefaultClassLoader;
UuidGeneratorServiceLoader uuidGeneratorServiceLoader = new UuidGeneratorServiceLoader(classLoader,
runtimeOptions);
this.bus = synchronize(
new TimeServiceEventBus(Clock.systemUTC(), uuidGeneratorServiceLoader.loadUuidGenerator()));

// Parse the features early. Don't proceed when there are lexer errors
FeatureParser parser = new FeatureParser(bus::generateId);
Supplier<ClassLoader> classLoader = ClassLoaders::getDefaultClassLoader;
FeaturePathFeatureSupplier featureSupplier = new FeaturePathFeatureSupplier(classLoader, runtimeOptions,
parser);
List<Feature> features = featureSupplier.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import io.cucumber.core.runtime.ThreadLocalObjectFactorySupplier;
import io.cucumber.core.runtime.ThreadLocalRunnerSupplier;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.core.runtime.UuidGeneratorServiceLoader;
import org.apiguardian.api.API;

import java.time.Clock;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.function.Supplier;

Expand Down Expand Up @@ -98,9 +98,10 @@ public TestNGCucumberRunner(Class<?> clazz, CucumberPropertiesProvider propertie
.enablePublishPlugin()
.build(environmentOptions);

EventBus bus = synchronize(new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID));

Supplier<ClassLoader> classLoader = ClassLoaders::getDefaultClassLoader;
UuidGeneratorServiceLoader uuidGeneratorServiceLoader = new UuidGeneratorServiceLoader(classLoader, runtimeOptions);
EventBus bus = synchronize(new TimeServiceEventBus(Clock.systemUTC(), uuidGeneratorServiceLoader.loadUuidGenerator()));

FeatureParser parser = new FeatureParser(bus::generateId);
FeaturePathFeatureSupplier featureSupplier = new FeaturePathFeatureSupplier(classLoader, runtimeOptions,
parser);
Expand Down

0 comments on commit f580b71

Please sign in to comment.