Skip to content

Commit 1f11f07

Browse files
committed
tests and error handling improvements
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent d55a251 commit 1f11f07

File tree

2 files changed

+64
-6
lines changed
  • providers/flagd/src
    • main/java/dev/openfeature/contrib/providers/flagd/resolver/process/storage/connector/file
    • test/java/dev/openfeature/contrib/providers/flagd/resolver/process/storage/connector/file

2 files changed

+64
-6
lines changed

Diff for: providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/process/storage/connector/file/FileConnector.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void init() throws IOException {
4545
// initial read
4646
String flagData = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
4747
if (!queue.offer(new StreamPayload(StreamPayloadType.DATA, flagData))) {
48-
throw new RuntimeException("Unable to write to queue. Queue is full.");
48+
log.warn("Unable to offer file content to queue: queue is full");
4949
}
5050

5151
long lastTS = Files.getLastModifiedTime(filePath).toMillis();
@@ -58,7 +58,7 @@ public void init() throws IOException {
5858
lastTS = currentTS;
5959
flagData = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
6060
if (!queue.offer(new StreamPayload(StreamPayloadType.DATA, flagData))) {
61-
throw new RuntimeException("Unable to write to queue. Queue is full.");
61+
log.warn("Unable to offer file content to queue: queue is full");
6262
}
6363
}
6464

@@ -67,7 +67,9 @@ public void init() throws IOException {
6767

6868
log.info("Shutting down file connector.");
6969
} catch (Throwable t) {
70-
log.error("Error from file connector", t);
70+
log.error("Error from file connector. File connector will exit", t);
71+
// note - queue offer response is intentionally ignored
72+
queue.offer(new StreamPayload(StreamPayloadType.ERROR, t.toString()));
7173
}
7274
});
7375

Diff for: providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/resolver/process/storage/connector/file/FileConnectorTest.java

+59-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.StreamPayloadType;
55
import org.junit.jupiter.api.Test;
66

7+
import java.io.File;
78
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.StandardOpenOption;
812
import java.time.Duration;
913
import java.util.concurrent.BlockingQueue;
1014

1115
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.VALID_LONG;
1216
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.getResourcePath;
1317
import static org.junit.jupiter.api.Assertions.assertEquals;
1418
import static org.junit.jupiter.api.Assertions.assertNotNull;
15-
import static org.junit.jupiter.api.Assertions.assertThrows;
1619
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
1720

1821
class FileConnectorTest {
@@ -39,12 +42,65 @@ void readAndExposeFeatureFlagsFromSource() throws IOException {
3942
}
4043

4144
@Test
42-
void throwsErrorIfInvalidFile(){
45+
void emitErrorStateForInvalidPath() throws IOException {
4346
// given
4447
final FileConnector connector = new FileConnector("INVALID_PATH");
4548

49+
// when
50+
connector.init();
51+
52+
// then
53+
final BlockingQueue<StreamPayload> stream = connector.getStream();
54+
55+
// Must emit an error within considerable time
56+
final StreamPayload[] payload = new StreamPayload[1];
57+
assertTimeoutPreemptively(Duration.ofMillis(200), () -> {
58+
payload[0] = stream.take();
59+
});
60+
61+
assertNotNull(payload[0].getData());
62+
assertEquals(StreamPayloadType.ERROR, payload[0].getType());
63+
}
64+
65+
@Test
66+
void watchForFileUpdatesAndEmitThem() throws IOException {
67+
final String initial = "{\"flags\":{\"myBoolFlag\":{\"state\":\"ENABLED\",\"variants\":{\"on\":true," +
68+
"\"off\":false},\"defaultVariant\":\"on\"}}}";
69+
70+
final String updatedFlags = "{\"flags\":{\"myBoolFlag\":{\"state\":\"ENABLED\",\"variants\":{\"on\":true," +
71+
"\"off\":false},\"defaultVariant\":\"off\"}}}";
72+
73+
// given
74+
final File tmpFlagFile = File.createTempFile("flagd", "flags");
75+
final Path flagSourcePath = tmpFlagFile.toPath();
76+
77+
Files.write(flagSourcePath, initial.getBytes(), StandardOpenOption.WRITE);
78+
final FileConnector connector = new FileConnector(flagSourcePath.toString());
79+
80+
// when
81+
connector.init();
82+
4683
// then
47-
assertThrows(IOException.class, connector::init);
84+
final BlockingQueue<StreamPayload> stream = connector.getStream();
85+
86+
final StreamPayload[] payload = new StreamPayload[1];
87+
88+
// first validate the initial payload
89+
assertTimeoutPreemptively(Duration.ofMillis(200), () -> {
90+
payload[0] = stream.take();
91+
});
92+
93+
assertEquals(initial, payload[0].getData());
94+
95+
// then update the flags
96+
Files.write(flagSourcePath, updatedFlags.getBytes(), StandardOpenOption.WRITE);
97+
98+
// finally wait for updated payload
99+
assertTimeoutPreemptively(Duration.ofSeconds(10), () -> {
100+
payload[0] = stream.take();
101+
});
102+
103+
assertEquals(updatedFlags, payload[0].getData());
48104
}
49105

50106
}

0 commit comments

Comments
 (0)