-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathStreamingFeatureFetchingTest.java
More file actions
163 lines (133 loc) · 6.71 KB
/
StreamingFeatureFetchingTest.java
File metadata and controls
163 lines (133 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package io.getunleash.streaming;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import io.getunleash.DefaultUnleash;
import io.getunleash.SynchronousTestExecutor;
import io.getunleash.Unleash;
import io.getunleash.event.ClientFeaturesResponse;
import io.getunleash.event.UnleashSubscriber;
import io.getunleash.util.UnleashConfig;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
public class StreamingFeatureFetchingTest {
@RegisterExtension
static WireMockExtension serverMock =
WireMockExtension.newInstance()
.configureStaticDsl(true)
.options(wireMockConfig().dynamicPort())
.build();
private UnleashConfig config;
private TestSubscriber testSubscriber;
private SynchronousTestExecutor executor;
@BeforeEach
void setUp() throws Exception {
testSubscriber = new TestSubscriber();
executor = new SynchronousTestExecutor();
URI uri = new URI("http://localhost:" + serverMock.getPort() + "/api/");
// Use unique instance ID to avoid conflicts between tests
String instanceId = "test-instance-" + System.currentTimeMillis();
config =
UnleashConfig.builder()
.appName("streaming-event-test")
.instanceId(instanceId)
.unleashAPI(uri)
.experimentalStreamingMode()
.subscriber(testSubscriber)
.scheduledExecutor(executor)
.disableMetrics()
.build();
}
@Test
void should_handle_unleash_connected_event() throws Exception {
String hydrationData =
"{\"events\":[{\"type\":\"hydration\",\"eventId\":1,\"features\":[{\"name\":\"deltaFeature\",\"enabled\":true,\"strategies\":[],\"variants\":[]}],\"segments\":[]}]}";
stubFor(
get(urlEqualTo("/api/client/streaming"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/event-stream")
.withBody(
"event: unleash-connected\n"
+ "data: "
+ hydrationData
+ "\n\n")));
Unleash unleash = new DefaultUnleash(config);
assertThat(unleash).isNotNull();
assertThat(config.isStreamingMode()).isTrue();
boolean eventReceived = testSubscriber.awaitTogglesFetched(5, TimeUnit.SECONDS);
assertThat(eventReceived).isTrue();
assertThat(testSubscriber.getTogglesFetchedCount()).isGreaterThan(0);
boolean isEnabled = unleash.isEnabled("deltaFeature");
assertThat(isEnabled).isTrue();
unleash.shutdown();
verify(getRequestedFor(urlMatching("/api/client/streaming")));
}
@Test
void should_handle_unleash_updated_event_and_shutdown() throws Exception {
String initialHydration =
"{\"events\":[{\"type\":\"hydration\",\"eventId\":1,\"features\":[{\"name\":\"deltaFeature\",\"enabled\":true,\"strategies\":[],\"variants\":[]}],\"segments\":[]}]}";
String updateData =
"{\"events\":[{\"type\":\"feature-updated\",\"eventId\":2,\"feature\":{\"name\":\"deltaFeature\",\"enabled\":false,\"strategies\":[],\"variants\":[]}}]}";
stubFor(
get(urlEqualTo("/api/client/streaming"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/event-stream")
.withBody(
"event: unleash-connected\n"
+ "data: "
+ initialHydration
+ "\n\n"
+ "event: unleash-updated\n"
+ "data: "
+ updateData
+ "\n\n")));
Unleash unleash = new DefaultUnleash(config);
assertThat(unleash).isNotNull();
boolean eventsReceived = testSubscriber.awaitTogglesFetched(5, TimeUnit.SECONDS, 2);
assertThat(eventsReceived).isTrue();
assertThat(testSubscriber.getTogglesFetchedCount()).isGreaterThanOrEqualTo(2);
boolean updatedResult = unleash.isEnabled("deltaFeature");
assertThat(updatedResult).isFalse();
unleash.shutdown();
verify(getRequestedFor(urlMatching("/api/client/streaming")));
}
private static class TestSubscriber implements UnleashSubscriber {
private final CountDownLatch togglesFetchedLatch = new CountDownLatch(1);
private final CountDownLatch multipleTogglesFetchedLatch = new CountDownLatch(2);
private int togglesFetchedCount = 0;
private List<ClientFeaturesResponse> responses = new ArrayList<>();
@Override
public void togglesFetched(ClientFeaturesResponse toggleResponse) {
togglesFetchedCount++;
responses.add(toggleResponse);
togglesFetchedLatch.countDown();
multipleTogglesFetchedLatch.countDown();
}
public boolean awaitTogglesFetched(long timeout, TimeUnit unit)
throws InterruptedException {
return togglesFetchedLatch.await(timeout, unit);
}
public boolean awaitTogglesFetched(long timeout, TimeUnit unit, int expectedCount)
throws InterruptedException {
if (expectedCount <= 1) {
return togglesFetchedLatch.await(timeout, unit);
} else {
return multipleTogglesFetchedLatch.await(timeout, unit);
}
}
public int getTogglesFetchedCount() {
return togglesFetchedCount;
}
}
}