Skip to content

Commit f100e13

Browse files
committed
Fixes SwitcherTest when using with concurrent testing strategy
1 parent 28227c9 commit f100e13

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
uses: actions/setup-java@v4
4949
with:
5050
distribution: 'temurin'
51-
java-version: '11'
51+
java-version: '8'
5252
server-id: central
5353
server-username: CENTRAL_USERNAME
5454
server-password: CENTRAL_TOKEN

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.github.switcherapi</groupId>
99
<artifactId>switcher-client</artifactId>
1010
<packaging>jar</packaging>
11-
<version>1.6.4</version>
11+
<version>1.6.4-SNAPSHOT</version>
1212

1313
<name>Switcher Client</name>
1414
<description>Switcher Client SDK for working with Switcher API</description>

src/main/java/com/github/switcherapi/client/test/SwitcherTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
import org.junit.jupiter.api.TestTemplate;
44
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.junit.jupiter.api.parallel.Execution;
6+
import org.junit.jupiter.api.parallel.ExecutionMode;
57

68
import java.lang.annotation.ElementType;
79
import java.lang.annotation.Retention;
810
import java.lang.annotation.RetentionPolicy;
911
import java.lang.annotation.Target;
1012

1113
/**
12-
* Annotate test cases with the Switcher Key and the expected result.
13-
*
14+
* Annotate test cases with the Switcher Key and the expected result.<p>
15+
* It uses "@Execution(ExecutionMode.SAME_THREAD)" to prevent Switcher race condition when setting up the mock.
16+
*
1417
* <p>
1518
* <b>Requires JUnit 5 Jupiter</b>
16-
*
19+
*
1720
* @author Roger Floriano (petruki)
1821
*/
1922
@Retention(RetentionPolicy.RUNTIME)
2023
@Target(ElementType.METHOD)
24+
@Execution(ExecutionMode.SAME_THREAD)
2125
@ExtendWith(SwitcherTestExtension.class)
2226
@TestTemplate
2327
public @interface SwitcherTest {

src/test/java/com/github/switcherapi/playground/ClientPlayground.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.concurrent.ScheduledExecutorService;
99
import java.util.concurrent.TimeUnit;
1010

11-
import static com.github.switcherapi.playground.Features.MY_SWITCHER;
11+
import static com.github.switcherapi.playground.Features.CLIENT_JAVA_FEATURE;
1212
import static com.github.switcherapi.playground.Features.getSwitcher;
1313

1414
public class ClientPlayground {
@@ -19,14 +19,18 @@ public class ClientPlayground {
1919

2020
public static void test() {
2121
new Features().configureClient();
22-
Switcher switcher = getSwitcher(MY_SWITCHER)
22+
Switcher switcher = getSwitcher(CLIENT_JAVA_FEATURE)
2323
.bypassMetrics()
2424
.build();
2525

2626
scheduler.scheduleAtFixedRate(() -> {
27-
long time = System.currentTimeMillis();
28-
logger.info("Switcher is on: {}", switcher.isItOn());
29-
logger.info("Time elapsed: {}", System.currentTimeMillis() - time);
27+
try {
28+
long time = System.currentTimeMillis();
29+
logger.info("Switcher is on: {}", switcher.isItOn());
30+
logger.info("Time elapsed: {}", System.currentTimeMillis() - time);
31+
} catch (Exception e) {
32+
logger.error(e.getMessage(), e);
33+
}
3034
}, 0, 5, TimeUnit.SECONDS);
3135
}
3236

src/test/java/com/github/switcherapi/playground/Features.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
import com.github.switcherapi.client.SwitcherKey;
66

77
public class Features extends SwitcherContextBase {
8-
8+
99
@SwitcherKey
10-
public static final String MY_SWITCHER = "MY_SWITCHER";
10+
public static final String CLIENT_JAVA_FEATURE = "CLIENT_JAVA_FEATURE";
1111

1212
@Override
1313
protected void configureClient() {
1414
configure(ContextBuilder.builder()
1515
.context(Features.class.getCanonicalName())
1616
.url("https://api.switcherapi.com")
17-
.apiKey("[API_KEY]")
18-
.component("switcher-playground")
19-
.domain("Playground")
17+
.apiKey(System.getenv("switcher.api.key"))
18+
.component(System.getenv("switcher.component"))
19+
.domain(System.getenv("switcher.domain"))
2020
.local(true)
21-
.snapshotLocation("/snapshot/playground"));
21+
.snapshotLocation("./src/test/resources/snapshot/playground"));
2222

2323
initializeClient();
2424
}
25-
}
25+
}

src/test/resources/snapshot/playground/default.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
{
22
"data": {
33
"domain": {
4-
"name": "Playground",
4+
"name": "Switcher API",
55
"version": 0,
6-
"description": "My playground",
6+
"description": "Feature Flag - Toggle - Cloud-based",
77
"activated": true,
88
"group": [
99
{
10-
"name": "Experimental",
11-
"description": "Open feature flags for experimentation",
10+
"name": "Test Project",
11+
"description": "Test",
1212
"activated": true,
1313
"config": [
1414
{
15-
"key": "MY_SWITCHER",
16-
"description": "My first switcher",
15+
"key": "CLIENT_JAVA_FEATURE",
16+
"description": "Used for Integration test for the Switcher Client Java",
1717
"activated": true,
1818
"strategies": [
1919
{
2020
"strategy": "VALUE_VALIDATION",
2121
"operation": "EXIST",
22-
"activated": false,
22+
"activated": true,
2323
"values": [
2424
"user_1"
2525
]
2626
}
27+
],
28+
"components": [
29+
"switcher-client-java"
2730
]
2831
}
2932
]

0 commit comments

Comments
 (0)