Skip to content

Commit 9f4779b

Browse files
authored
fix: strategy evaluation is OR based. (#370)
Due to a bug in Yggdrasil, multiple instances of the same custom strategy overwrote each other so last one won. After this bug fix, Yggdrasil again evaluates all custom strategies on an OR base, so if any are true, the flag will be true fixes: #369
1 parent e3b1bf6 commit 9f4779b

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>io.getunleash</groupId>
6363
<artifactId>yggdrasil-engine</artifactId>
64-
<version>0.4.5</version>
64+
<version>0.5.0</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>com.google.code.gson</groupId>

src/main/java/io/getunleash/impactmetrics/MetricFlagContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import io.getunleash.UnleashContext;
44
import java.util.List;
55

6-
/** @deprecated MetricFlagContext will be removed in a future release. */
6+
/**
7+
* @deprecated MetricFlagContext will be removed in a future release.
8+
*/
79
@Deprecated(since = "12.1.1", forRemoval = true)
810
public class MetricFlagContext {
911
private final List<String> flagNames;

src/main/java/io/getunleash/impactmetrics/MetricsAPI.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ public interface MetricsAPI {
1717

1818
public void incrementCounter(String name, long value);
1919

20-
/** @deprecated MetricFlagContext will be removed in a future release. */
20+
/**
21+
* @deprecated MetricFlagContext will be removed in a future release.
22+
*/
2123
@Deprecated(since = "12.1.1", forRemoval = true)
2224
public void incrementCounter(
2325
String name, @Nullable Long value, @Nullable MetricFlagContext flagContext);
2426

2527
public void updateGauge(String name, double value);
2628

27-
/** @deprecated MetricFlagContext will be removed in a future release. */
29+
/**
30+
* @deprecated MetricFlagContext will be removed in a future release.
31+
*/
2832
@Deprecated(since = "12.1.1", forRemoval = true)
2933
public void updateGauge(String name, double value, @Nullable MetricFlagContext flagContext);
3034

@@ -36,7 +40,9 @@ public void incrementCounter(
3640

3741
public void observeHistogram(String name, double value);
3842

39-
/** @deprecated MetricFlagContext will be removed in a future release. */
43+
/**
44+
* @deprecated MetricFlagContext will be removed in a future release.
45+
*/
4046
@Deprecated(since = "12.1.1", forRemoval = true)
4147
public void observeHistogram(
4248
String name, double value, @Nullable MetricFlagContext flagContext);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.getunleash.strategies.custom;
2+
3+
import io.getunleash.UnleashContext;
4+
import io.getunleash.strategy.Strategy;
5+
import java.util.Map;
6+
7+
public class ConstraintEvaluatorCustom implements Strategy {
8+
@Override
9+
public String getName() {
10+
return "repeated";
11+
}
12+
13+
@Override
14+
public boolean isEnabled(Map<String, String> parameters, UnleashContext context) {
15+
return context.getProperties().get("myFancy").equals(parameters.get("myFancy"));
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.getunleash.strategies.custom;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.getunleash.DefaultUnleash;
6+
import io.getunleash.UnleashContext;
7+
import io.getunleash.util.ResourceReader;
8+
import io.getunleash.util.UnleashConfig;
9+
import java.util.Optional;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class RepeatedCustomStrategyTest {
13+
private String loadMockFeatures(String path) {
14+
return ResourceReader.readResourceAsString(path);
15+
}
16+
17+
@Test
18+
public void
19+
repeated_custom_strategy_evaluates_to_true_if_any_custom_strategy_evaluates_to_true() {
20+
UnleashConfig config =
21+
UnleashConfig.builder()
22+
.unleashAPI("http://test:4242")
23+
.appName("multiple_connection")
24+
.instanceId("repeated_custom_strategy")
25+
.toggleBootstrapProvider(
26+
() ->
27+
Optional.of(
28+
loadMockFeatures("repeated_custom_strategy.json")))
29+
.build();
30+
var myUnleash = new DefaultUnleash(config, new ConstraintEvaluatorCustom());
31+
UnleashContext context = UnleashContext.builder().addProperty("myFancy", "one").build();
32+
assertThat(myUnleash.isEnabled("repeated.custom", context)).isTrue();
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": 2,
3+
"features": [
4+
{
5+
"name": "repeated.custom",
6+
"enabled": true,
7+
"strategies": [
8+
{
9+
"name": "repeated",
10+
"parameters": {
11+
"myFancy": "one"
12+
}
13+
14+
},
15+
{
16+
"name": "repeated",
17+
"parameters": {
18+
"myFancy": "two"
19+
}
20+
}
21+
]
22+
}
23+
]
24+
}

0 commit comments

Comments
 (0)