Skip to content

Commit

Permalink
feat: yggdrasil feature enabled (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Jan 14, 2025
1 parent 76b99fa commit 937b29c
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 592 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
version: [ 8, 11, 17 ]
version: [8, 11, 17]
os: ["ubuntu", "windows", "macos"]
exclude:
- version: 8
os: macos
- version: 8
os: windows
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<version.junit5>5.10.3</version.junit5>
<version.okhttp>4.12.0</version.okhttp>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.unleash.specification>5.1.5</version.unleash.specification>
<version.unleash.specification>5.1.7</version.unleash.specification>
<arguments />
<version.jackson>2.17.2</version.jackson>
<version.logback>1.3.14</version.logback>
Expand Down Expand Up @@ -59,8 +59,8 @@
<dependency>
<groupId>io.getunleash</groupId>
<artifactId>yggdrasil-engine</artifactId>
<version>0.1.0-alpha.9</version>
<classifier>x86_64-linux</classifier>
<version>0.1.0-alpha.12</version>
<classifier>${os.detected.classifier}</classifier>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down Expand Up @@ -162,6 +162,13 @@
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>
<resources>
<resource>
<directory>src/main/resources</directory>
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/io/getunleash/DefaultUnleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,16 @@ public Variant getVariant(String toggleName, UnleashContext context, Variant def
this.unleashEngine.getVariant(toggleName, adaptedContext),
defaultValue);

Boolean enabled = this.unleashEngine.isEnabled(toggleName, adaptedContext);

// TODO: Swap this for feature enabled
if (enabled == null) {
enabled = false;
}

this.unleashEngine.countToggle(toggleName, enabled);
this.unleashEngine.countToggle(toggleName, variant.isFeatureEnabled());
this.unleashEngine.countVariant(toggleName, variant.getName());
eventDispatcher.dispatch(new ToggleEvaluated(toggleName, variant.isEnabled()));
if (unleashEngine.shouldEmitImpressionEvent(toggleName)) {
eventDispatcher.dispatch(
new VariantImpressionEvent(
toggleName, enabled, context, variant.getName()));
toggleName,
variant.isFeatureEnabled(),
context,
variant.getName()));
}
return variant;
} catch (YggdrasilInvalidInputException | YggdrasilError e) {
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/io/getunleash/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,41 @@ public class Variant {
@Nullable private final Payload payload;
private final boolean enabled;
@Nullable private final String stickiness;
private final boolean feature_enabled;

public Variant(String name, @Nullable Payload payload, boolean enabled) {
this(name, payload, enabled, null);
public Variant(
String name, @Nullable Payload payload, boolean enabled, boolean feature_enabled) {
this(name, payload, enabled, null, feature_enabled);
}

public Variant(String name, @Nullable Payload payload, boolean enabled, String stickiness) {
public Variant(
String name,
@Nullable Payload payload,
boolean enabled,
String stickiness,
boolean feature_enabled) {
this.name = name;
this.payload = payload;
this.enabled = enabled;
this.stickiness = stickiness;
this.feature_enabled = feature_enabled;
}

public Variant(String name, @Nullable String payload, boolean enabled) {
this(name, payload, enabled, null);
this(name, payload, enabled, null, false);
}

public Variant(String name, @Nullable String payload, boolean enabled, String stickiness) {
public Variant(
String name,
@Nullable String payload,
boolean enabled,
String stickiness,
boolean feature_enabled) {
this.name = name;
this.payload = new Payload("string", payload);
this.enabled = enabled;
this.stickiness = stickiness;
this.feature_enabled = feature_enabled;
}

public String getName() {
Expand All @@ -47,6 +61,10 @@ public boolean isEnabled() {
return enabled;
}

public boolean isFeatureEnabled() {
return feature_enabled;
}

@Nullable
public String getStickiness() {
return stickiness;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/getunleash/YggdrasilAdapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static UnleashContext adapt(Context context) {
try {
currentTime = ZonedDateTime.parse(context.getCurrentTime());
} catch (DateTimeParseException e) {
LOGGER.warn("Could not parse current time from context, falling back to system time: ", context.getCurrentTime());
LOGGER.warn(
"Could not parse current time from context, falling back to system time: ",
context.getCurrentTime());
currentTime = ZonedDateTime.now();
}
}
Expand Down Expand Up @@ -73,7 +75,11 @@ public static Variant adapt(VariantDef variant, Variant defaultValue) {
if (variant == null) {
return defaultValue;
}
return new Variant(variant.getName(), adapt(variant.getPayload()), variant.isEnabled());
return new Variant(
variant.getName(),
adapt(variant.getPayload()),
variant.isEnabled(),
variant.isFeatureEnabled());
}

public static @Nullable io.getunleash.variant.Payload adapt(@Nullable Payload payload) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/getunleash/variant/VariantDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ List<VariantOverride> getOverrides() {
}

Variant toVariant() {
return new Variant(name, payload, true, stickiness);
return new Variant(name, payload, true, stickiness, false);
}
}
21 changes: 21 additions & 0 deletions src/test/java/io/getunleash/UnleashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,27 @@ public void should_be_disabled_with_strategy_variants() {
assertThat(unleash.isEnabled("test")).isFalse();
}

@Test
public void empty_variants_returns_disabled_variant() {
UnleashContext context = UnleashContext.builder().build();

Map<String, String> params = new HashMap<>();
params.put("rollout", "100");
params.put("stickiness", "default");
params.put("groupId", "test");

ActivationStrategy strategy = new ActivationStrategy("flexibleRollout", params);
FeatureToggle featureToggle =
new FeatureToggle("test", true, asList(strategy), Collections.emptyList());

stateHandler.setState(featureToggle);
final Variant result = unleash.getVariant("test", context);

assertThat(result.getName()).isEqualTo("disabled");
assertThat(result.isEnabled()).isFalse();
assertThat(result.isFeatureEnabled()).isTrue();
}

private List<VariantDefinition> getTestVariants() {
return asList(
new VariantDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ private List<DynamicTest> createVariantTests(String fileName)
test.getExpectedResult().getPayload(),
result.getPayload(),
test.getDescription());
assertEquals(
test.getExpectedResult().isFeatureEnabled(),
result.isFeatureEnabled(),
test.getDescription());
}))
.collect(Collectors.toList());
}
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/io/getunleash/integration/TestCaseVariant.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ public String getToggleName() {

public Variant getExpectedResult() {
if (expectedResult.getName().equals("disabled")) {
return Variant.DISABLED_VARIANT;
Variant clone =
new Variant(
Variant.DISABLED_VARIANT.getName(),
Variant.DISABLED_VARIANT.getPayload().orElse(null),
Variant.DISABLED_VARIANT.isEnabled(),
Variant.DISABLED_VARIANT.getStickiness(),
expectedResult.isFeatureEnabled());
return clone;
}

return expectedResult;
Expand Down
128 changes: 0 additions & 128 deletions src/test/java/io/getunleash/strategy/StrategyVariantTest.java

This file was deleted.

Loading

0 comments on commit 937b29c

Please sign in to comment.