Skip to content

Commit 29e6342

Browse files
authored
Reliability: Add feature flag (#159)
* Add feature flag * Add URL
1 parent be78b4c commit 29e6342

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.mincong.reliability.featureflag;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* @author Mincong Huang
7+
* @blog https://mincong.io/2020/11/11/feature-flag/
8+
*/
9+
public class MyJob {
10+
11+
private final Set<String> values;
12+
13+
public MyJob(Set<String> values) {
14+
this.values = values;
15+
}
16+
17+
public void run() {
18+
var isNewFeatureEnabled = Boolean.getBoolean("NEW_FEATURE_ENABLED");
19+
if (isNewFeatureEnabled) {
20+
values.add("new");
21+
} else {
22+
values.add("old");
23+
}
24+
}
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.mincong.reliability.featureflag;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.HashSet;
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* @author Mincong Huang
11+
* @blog https://mincong.io/2020/11/11/feature-flag/
12+
*/
13+
class MyJobTest {
14+
15+
@AfterEach
16+
void tearDown() {
17+
System.clearProperty("NEW_FEATURE_ENABLED");
18+
}
19+
20+
@Test
21+
void itShouldEnableNewFeature() {
22+
// Given
23+
System.setProperty("NEW_FEATURE_ENABLED", "True");
24+
var set = new HashSet<String>();
25+
var job = new MyJob(set);
26+
27+
// When
28+
job.run();
29+
30+
// Then
31+
assertThat(set).containsExactly("new");
32+
}
33+
34+
@Test
35+
void itShouldDisableNewFeature() {
36+
// Given
37+
System.setProperty("NEW_FEATURE_ENABLED", "False");
38+
var set = new HashSet<String>();
39+
var job = new MyJob(set);
40+
41+
// When
42+
job.run();
43+
44+
// Then
45+
assertThat(set).containsExactly("old");
46+
}
47+
48+
@Test
49+
void itShouldFallback() {
50+
// Given
51+
var set = new HashSet<String>();
52+
var job = new MyJob(set);
53+
54+
// When
55+
job.run();
56+
57+
// Then
58+
assertThat(set).containsExactly("old");
59+
}
60+
}

0 commit comments

Comments
 (0)