-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: flagd in process offline mode (#473)
Signed-off-by: Kavindu Dodanduwa <[email protected]> Signed-off-by: Kavindu Dodanduwa <[email protected]> Co-authored-by: Giovanni Liva <[email protected]> Co-authored-by: Todd Baert <[email protected]>
- Loading branch information
1 parent
51d15d2
commit 6920557
Showing
12 changed files
with
198 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...eature/contrib/providers/flagd/resolver/process/storage/connector/file/FileConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.file; | ||
|
||
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.Connector; | ||
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.StreamPayload; | ||
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.StreamPayloadType; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
|
||
/** | ||
* File connector reads flag configurations and expose the context through {@code Connector} contract. | ||
* The implementation is kept minimal and suites testing, local development needs. | ||
*/ | ||
@SuppressFBWarnings(value = {"EI_EXPOSE_REP", "PATH_TRAVERSAL_IN"}, | ||
justification = "File connector read feature flag from a file source.") | ||
@Slf4j | ||
public class FileConnector implements Connector { | ||
|
||
private final String flagSourcePath; | ||
private final BlockingQueue<StreamPayload> queue = new LinkedBlockingQueue<>(1); | ||
|
||
public FileConnector(final String flagSourcePath) { | ||
this.flagSourcePath = flagSourcePath; | ||
} | ||
|
||
/** | ||
* Initialize file connector. Reads content of the provided source file and offer it through queue. | ||
*/ | ||
public void init() throws IOException { | ||
final String flagData = new String(Files.readAllBytes(Paths.get(flagSourcePath)), StandardCharsets.UTF_8); | ||
|
||
if (!queue.offer(new StreamPayload(StreamPayloadType.DATA, flagData))) { | ||
throw new RuntimeException("Unable to write to queue. Queue is full."); | ||
} | ||
|
||
log.info(String.format("Using feature flag configurations from file %s", flagSourcePath)); | ||
} | ||
|
||
/** | ||
* Expose the queue to fulfil the {@code Connector} contract. | ||
*/ | ||
public BlockingQueue<StreamPayload> getStream() { | ||
return queue; | ||
} | ||
|
||
/** | ||
* NO-OP shutdown. | ||
*/ | ||
public void shutdown() throws InterruptedException { | ||
// NO-OP nothing to do here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...re/contrib/providers/flagd/resolver/process/storage/connector/file/FileConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.file; | ||
|
||
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.StreamPayload; | ||
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.StreamPayloadType; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.concurrent.BlockingQueue; | ||
|
||
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.VALID_LONG; | ||
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.getResourcePath; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; | ||
|
||
class FileConnectorTest { | ||
|
||
@Test | ||
void readAndExposeFeatureFlagsFromSource() throws IOException { | ||
// given | ||
final FileConnector connector = new FileConnector(getResourcePath(VALID_LONG)); | ||
|
||
// when | ||
connector.init(); | ||
|
||
// then | ||
final BlockingQueue<StreamPayload> stream = connector.getStream(); | ||
final StreamPayload[] payload = new StreamPayload[1]; | ||
|
||
assertNotNull(stream); | ||
assertTimeoutPreemptively(Duration.ofMillis(200), () -> { | ||
payload[0] = stream.take(); | ||
}); | ||
|
||
assertNotNull(payload[0].getData()); | ||
assertEquals(StreamPayloadType.DATA, payload[0].getType()); | ||
} | ||
|
||
@Test | ||
void throwsErrorIfInvalidFile(){ | ||
// given | ||
final FileConnector connector = new FileConnector("INVALID_PATH"); | ||
|
||
// then | ||
assertThrows(IOException.class, connector::init); | ||
} | ||
|
||
} |