Skip to content

Commit

Permalink
fix: disabling cache if scenario-scoped glue is present for #2971
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Kronegg committed Feb 3, 2025
1 parent ff2468c commit f3efd5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ final class CachingGlue implements Glue {
private Locale locale = null;
private StepExpressionFactory stepExpressionFactory = null;
private boolean dirtyCache = false;
private boolean hasScenarioScopedGlue = false;

CachingGlue(EventBus bus) {
this.bus = bus;
Expand All @@ -108,42 +109,47 @@ public void addAfterAllHook(StaticHookDefinition afterAllHook) {
public void addStepDefinition(StepDefinition stepDefinition) {
stepDefinitions.add(stepDefinition);
dirtyCache = true;
hasScenarioScopedGlue |= stepDefinition instanceof ScenarioScoped;
}

@Override
public void addBeforeHook(HookDefinition hookDefinition) {
beforeHooks.add(CoreHookDefinition.create(hookDefinition, bus::generateId));
beforeHooks.sort(HOOK_ORDER_ASCENDING);
hasScenarioScopedGlue |= hookDefinition instanceof ScenarioScoped;
}

@Override
public void addAfterHook(HookDefinition hookDefinition) {
afterHooks.add(CoreHookDefinition.create(hookDefinition, bus::generateId));
afterHooks.sort(HOOK_ORDER_ASCENDING);
hasScenarioScopedGlue |= hookDefinition instanceof ScenarioScoped;
}

@Override
public void addBeforeStepHook(HookDefinition hookDefinition) {
beforeStepHooks.add(CoreHookDefinition.create(hookDefinition, bus::generateId));
beforeStepHooks.sort(HOOK_ORDER_ASCENDING);
hasScenarioScopedGlue |= hookDefinition instanceof ScenarioScoped;
}

@Override
public void addAfterStepHook(HookDefinition hookDefinition) {
afterStepHooks.add(CoreHookDefinition.create(hookDefinition, bus::generateId));
afterStepHooks.sort(HOOK_ORDER_ASCENDING);
hasScenarioScopedGlue |= hookDefinition instanceof ScenarioScoped;
}

@Override
public void addParameterType(ParameterTypeDefinition parameterType) {
parameterTypeDefinitions.add(parameterType);
dirtyCache = true;
hasScenarioScopedGlue |= parameterType instanceof ScenarioScoped;
}

@Override
public void addDataTableType(DataTableTypeDefinition dataTableType) {
dataTableTypeDefinitions.add(dataTableType);
dirtyCache = true;
}

@Override
Expand All @@ -169,7 +175,6 @@ public void addDefaultDataTableCellTransformer(
@Override
public void addDocStringType(DocStringTypeDefinition docStringType) {
docStringTypeDefinitions.add(docStringType);
dirtyCache = true;
}

List<StaticHookDefinition> getBeforeAllHooks() {
Expand Down Expand Up @@ -246,8 +251,10 @@ void prepareGlue(Locale locale) throws DuplicateStepDefinitionException {
boolean firstTime = stepTypeRegistry == null;
boolean languageChanged = !locale.equals(this.locale);
boolean mustRebuildCache = false;
if (firstTime || languageChanged || dirtyCache) {
if (firstTime || languageChanged || dirtyCache || hasScenarioScopedGlue) {
// conditions changed => invalidate the glue cache
// Note: we have a prudent approach of avoiding caching if
// scenario-scoped glue exist (e.g. cucumber-java8).
this.locale = locale;
stepTypeRegistry = new StepTypeRegistry(locale);
stepExpressionFactory = new StepExpressionFactory(stepTypeRegistry, bus);
Expand Down Expand Up @@ -483,14 +490,8 @@ void removeScenarioScopedGlue() {
dirty = true;
dirtyCache = true;
}
if (removeScenarioScopedGlue(dataTableTypeDefinitions)) {
dirty = true;
dirtyCache = true;
}
if (removeScenarioScopedGlue(docStringTypeDefinitions)) {
dirty = true;
dirtyCache = true;
}
dirty |= removeScenarioScopedGlue(dataTableTypeDefinitions);
dirty |= removeScenarioScopedGlue(docStringTypeDefinitions);
if (removeScenarioScopedGlue(parameterTypeDefinitions)) {
dirty = true;
dirtyCache = true;
Expand All @@ -501,6 +502,7 @@ void removeScenarioScopedGlue() {
if (dirty) {
stepDefinitionsByPattern.clear();
}
hasScenarioScopedGlue = false;
}

private boolean removeScenarioScopedGlue(Iterable<?> glues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ void prepareGlue_cache_evicted_when_dataTableType_added() {
StepTypeRegistry stepTypeRegistry2 = glue.getStepTypeRegistry();

// Then
assertThat(stepTypeRegistry1 != stepTypeRegistry2, is(true));
assertThat(stepTypeRegistry1 == stepTypeRegistry2, is(true));
}

@Test
Expand All @@ -614,7 +614,7 @@ void prepareGlue_cache_evicted_when_docString_added() {
StepTypeRegistry stepTypeRegistry2 = glue.getStepTypeRegistry();

// Then
assertThat(stepTypeRegistry1 != stepTypeRegistry2, is(true));
assertThat(stepTypeRegistry1 == stepTypeRegistry2, is(true));
}

private static class MockedScenarioScopedStepDefinition extends StubStepDefinition implements ScenarioScoped {
Expand Down

0 comments on commit f3efd5d

Please sign in to comment.