Skip to content

Commit

Permalink
fix: blocking in-process init, e2e tests (#436)
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert authored Sep 18, 2023
1 parent a34ad58 commit 0326095
Show file tree
Hide file tree
Showing 19 changed files with 343 additions and 220 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ jobs:
services:
# flagd-testbed for flagd-provider e2e tests
flagd:
image: ghcr.io/open-feature/flagd-testbed:latest
image: ghcr.io/open-feature/flagd-testbed:v0.4.0
ports:
- 8013:8013
# sync-testbed for flagd-provider e2e tests
sync:
image: ghcr.io/open-feature/sync-testbed:v0.4.0
ports:
- 9090:9090

steps:
- name: Checkout Repository
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "providers/flagd/test-harness"]
path = providers/flagd/test-harness
url = https://github.com/open-feature/test-harness.git
[submodule "providers/flagd/spec"]
path = providers/flagd/spec
url = https://github.com/open-feature/spec.git
4 changes: 2 additions & 2 deletions providers/flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A feature flag daemon with a Unix philosophy.

## Usage

### Remote resolver
### Remote resolver (RPC)

This is the default mode of operation of the provider.
In this mode, `FlagdProvider` communicates with [flagd](https://github.com/open-feature/flagd) via the gRPC protocol.
Expand All @@ -41,7 +41,7 @@ Consider following example to create a `FlagdProvider` with in-process evaluatio
```java
FlagdProvider flagdProvider = new FlagdProvider(
FlagdOptions.builder()
.resolverType(Config.ResolverType.IN_PROCESS)
.resolverType(Config.Evaluator.IN_PROCESS)
.build());
```

Expand Down
25 changes: 21 additions & 4 deletions providers/flagd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
<goal>exec</goal>
</goals>
<configuration>
<!-- run: git submodule update \-\-init \-\-recursive -->
<!-- run: git submodule update \-\-init test-harness -->
<executable>git</executable>
<arguments>
<argument>submodule</argument>
Expand All @@ -259,7 +259,24 @@
</configuration>
</execution>
<execution>
<id>copy-gherkin-tests</id>
<id>update-spec-submodule</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<!-- run: git submodule update \-\-init spec -->
<executable>git</executable>
<arguments>
<argument>submodule</argument>
<argument>update</argument>
<argument>--init</argument>
<argument>spec</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>copy-gherkin-evaluation.feature</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
Expand All @@ -269,11 +286,11 @@
<!-- run: cp test-harness/features/evaluation.feature src/test/resources/features/ -->
<executable>cp</executable>
<arguments>
<argument>test-harness/features/evaluation.feature</argument>
<argument>spec/specification/assets/gherkin/evaluation.feature</argument>
<argument>src/test/resources/features/</argument>
</arguments>
</configuration>
</execution>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
1 change: 1 addition & 0 deletions providers/flagd/spec
Submodule spec added at e2eb2b
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Helper class to hold configuration default values.
*/
public final class Config {
static final ResolverType DEFAULT_RESOLVER_TYPE = ResolverType.FLAGD;
static final Evaluator DEFAULT_RESOLVER_TYPE = Evaluator.RPC;
static final String DEFAULT_PORT = "8013";
static final String DEFAULT_TLS = "false";
static final String DEFAULT_HOST = "localhost";
Expand Down Expand Up @@ -52,16 +52,18 @@ static int fallBackToEnvOrDefault(String key, int defaultValue) {
}

/**
* flagd resolving type.
* flagd evaluator type.
*/
public enum ResolverType {
public enum Evaluator {
/**
* This is the default resolver type, which connects to flagd instance with flag evaluation gRPC contract.
* Evaluations are performed remotely.
*/
FLAGD,
RPC,
/**
* This is the in-process resolving type, where flags are fetched with flag sync gRPC contract and stored
* locally for in-process evaluation.
* Evaluations are preformed in-process.
*/
IN_PROCESS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static dev.openfeature.contrib.providers.flagd.Config.TLS_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.fallBackToEnvOrDefault;


/**
* FlagdOptions is a builder to build flagd provider options.
* */
Expand All @@ -38,7 +37,7 @@ public class FlagdOptions {
* flagd resolving type.
* */
@Builder.Default
private Config.ResolverType resolverType = DEFAULT_RESOLVER_TYPE;
private Config.Evaluator resolverType = DEFAULT_RESOLVER_TYPE;

/**
* flagd connection host.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FlagdProvider(final FlagdOptions options) {
case IN_PROCESS:
this.flagResolver = new InProcessResolver(options, this::setState);
break;
case FLAGD:
case RPC:
this.flagResolver =
new GrpcResolver(options, CacheFactory.getCache(options), this::getState, this::setState);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.openfeature.contrib.providers.flagd.resolver.common;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* Utils for flagd resolvers.
*/
public class Util {
/**
* A helper to block the caller for given conditions.
*
* @param deadline number of milliseconds to block
* @param check {@link AtomicBoolean} to check for status true
*/
public static void busyWaitAndCheck(final Long deadline, final AtomicBoolean check) throws InterruptedException {
long start = System.currentTimeMillis();

do {
if (deadline <= System.currentTimeMillis() - start) {
throw new RuntimeException(
String.format("Deadline exceeded. Condition did not complete within the %d deadline", deadline));
}

Thread.sleep(50L);
} while (!check.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.Cache;
import dev.openfeature.contrib.providers.flagd.resolver.common.ChannelBuilder;
import dev.openfeature.contrib.providers.flagd.resolver.common.Util;
import dev.openfeature.flagd.grpc.Schema;
import dev.openfeature.flagd.grpc.ServiceGrpc;
import dev.openfeature.sdk.ProviderState;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void initialize() throws Exception {
eventObserverThread.start();

// block till ready
busyWaitAndCheck(this.deadline, this.connected);
Util.busyWaitAndCheck(this.deadline, this.connected);
}

/**
Expand Down Expand Up @@ -160,22 +161,4 @@ private void grpcStateConsumer(final ProviderState state) {
// chain to initiator
this.stateConsumer.accept(state);
}

/**
* A helper to block the caller for given conditions.
*
* @param deadline number of milliseconds to block
* @param check {@link AtomicBoolean} to check for status true
*/
private static void busyWaitAndCheck(final Long deadline, final AtomicBoolean check) throws InterruptedException {
long start = System.currentTimeMillis();

do {
if (deadline <= System.currentTimeMillis() - start) {
throw new RuntimeException(String.format("Initialization not complete after %d ms", deadline));
}

Thread.sleep(50L);
} while (!check.get());
}
}
Loading

0 comments on commit 0326095

Please sign in to comment.