Skip to content

Commit

Permalink
Merge pull request #1171 from hcoles/bug/duplicate_interceptors
Browse files Browse the repository at this point in the history
Prevent double instantiation of features
  • Loading branch information
hcoles authored Mar 22, 2023
2 parents 2c342b4 + bde7f73 commit e8f9f8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pitest/src/main/java/org/pitest/plugin/FeatureSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -34,15 +35,15 @@ public FeatureSetting getSettingForFeature(String feature) {
}

private List<T> selectFeatures(List<FeatureSetting> features, Collection<T> filters) {
final List<T> factories = new ArrayList<>(filters);
final Map<String, Collection<T>> featureMap = FCollection.bucket(factories, byFeatureName());
List<T> factories = new ArrayList<>(filters);
Map<String, Collection<T>> featureMap = FCollection.bucket(factories, byFeatureName());

final List<T> active = factories.stream()
Set<T> active = factories.stream()
.filter(isOnByDefault())
.collect(Collectors.toCollection(ArrayList::new));
.collect(Collectors.toSet());

for ( final FeatureSetting each : features ) {
final Collection<T> providers = featureMap.get(each.feature().toLowerCase());
for (FeatureSetting each : features) {
Collection<T> providers = featureMap.get(each.feature().toLowerCase());
if ((providers == null) || providers.isEmpty()) {
continue;
}
Expand All @@ -59,7 +60,8 @@ private List<T> selectFeatures(List<FeatureSetting> features, Collection<T> filt
}
}

return active;
return active.stream()
.collect(Collectors.toList());
}

private Predicate<T> isOnByDefault() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void featureNamesAreCaseInsensitive() {
assertThat(this.testee.getSettingForFeature("FOO")).isEqualTo(fooConfig);
}

@Test
public void doesNotDuplicateFeatures() {
final FeatureSetting fooConfig = new FeatureSetting("foo", ToggleStatus.ACTIVATE, new HashMap<>());
this.testee = new FeatureSelector<>(Arrays.asList(fooConfig), features(this.onByDefault));

assertThat(this.testee.getActiveFeatures()).hasSize(1);
}

private List<FeatureSetting> noSettings() {
return Collections.emptyList();
}
Expand Down

0 comments on commit e8f9f8c

Please sign in to comment.