Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@
<version>1.4.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>

<!--
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down Expand Up @@ -167,6 +167,11 @@
<version>1.37</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vmlens</groupId>
<artifactId>api</artifactId>
<version>1.2.11</version>
</dependency>

</dependencies>

Expand All @@ -176,6 +181,7 @@
<!-- Start mockito workaround -->
<!-- https://github.com/mockito/mockito/issues/3121 -->
<!-- These are transitive dependencies of mockito we are forcing -->
<!--
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
Expand All @@ -189,6 +195,7 @@
<version>1.17.6</version>
<scope>test</scope>
</dependency>
-->
<!-- End mockito workaround-->

<dependency>
Expand All @@ -206,7 +213,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -298,7 +304,24 @@
</archive>
</configuration>
</plugin>

<plugin>
<groupId>com.vmlens</groupId>
<artifactId>vmlens-maven-plugin</artifactId>
<version>1.2.11</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/VmLensTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -327,7 +350,8 @@
<ignoredUnusedDeclaredDependency>com.github.spotbugs:*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.junit*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>com.tngtech.archunit*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.simplify4u:slf4j2-mock*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.simplify4u:slf4j2-mock*
</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
<ignoredDependencies>
<ignoredDependency>com.google.guava*</ignoredDependency>
Expand Down
6 changes: 6 additions & 0 deletions sdk.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="CheckStyle-IDEA-Module" serialisationVersion="2">
<option name="activeLocationsIds" />
</component>
</module>
2 changes: 1 addition & 1 deletion src/main/java/dev/openfeature/sdk/Awaitable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Awaitable {
*/
public static final Awaitable FINISHED = new Awaitable(true);

private boolean isDone = false;
private volatile boolean isDone = false;

public Awaitable() {}

Expand Down
117 changes: 117 additions & 0 deletions src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.openfeature.sdk;

import com.vmlens.api.AllInterleavings;
import dev.openfeature.sdk.exceptions.ExceptionUtils;
import dev.openfeature.sdk.exceptions.FatalError;
import dev.openfeature.sdk.exceptions.GeneralError;
import dev.openfeature.sdk.exceptions.OpenFeatureError;
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
import dev.openfeature.sdk.internal.ObjectUtils;
import dev.openfeature.sdk.providers.memory.Flag;
import dev.openfeature.sdk.providers.memory.InMemoryProvider;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -15,6 +18,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import lombok.Getter;
Expand Down Expand Up @@ -515,4 +519,117 @@ public Client removeHandler(ProviderEvent event, Consumer<EventDetails> handler)
openfeatureApi.removeHandler(domain, event, handler);
return this;
}

static int jaVar = 0;

/**
* run.
*
* @param args the args
* @throws InterruptedException something
*/
public static void main(String[] args) throws InterruptedException {

System.out.println("OpenFeatureClientTest.testUpdate");
final OpenFeatureAPI api = new OpenFeatureAPI();

var flags = new HashMap<String, Flag<?>>();
flags.put("a", Flag.builder().variant("a", "def").defaultVariant("a").build());
flags.put("b", Flag.builder().variant("a", "as").defaultVariant("a").build());
flags.put("c", Flag.builder().variant("a", "dfs").defaultVariant("a").build());
flags.put("d", Flag.builder().variant("a", "asddd").defaultVariant("a").build());
api.setProviderAndWait(new InMemoryProvider(flags));
var i = 0;
var c = new AtomicInteger();

System.out.println("before try");
try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent evaluations and hook additions")) {
while (allInterleavings.hasNext()) {
c.incrementAndGet();
jaVar = 0;
Thread first = new Thread() {
@Override
public void run() {
jaVar++;
}
};
first.start();
jaVar++;
first.join();
if (jaVar != 2) {
throw new RuntimeException("jaVar=" + jaVar);
}
/*
var client = api.getClient();
var firstReady = new Awaitable();
var secondReady = new Awaitable();
var startThreads = new Awaitable();
Thread first = new Thread() {
@Override
public void run() {
firstReady.wakeup();
startThreads.await();
client.getStringValue("a", "a");
}
};
Thread hookAdder = new Thread() {
@Override
public void run() {
secondReady.wakeup();
startThreads.await();
client.addHooks(new Hook() {});
}
};
hookAdder.start();
first.start();
firstReady.await();
secondReady.await();
startThreads.wakeup();
first.join();
hookAdder.join();
*/
/*System.out.println("has interlereaving");
i++;
//var latch = new CountDownLatch(1);
var client = api.getClient();
var concurrentModException = new AtomicReference<ConcurrentModificationException>();
Thread eval = new Thread(() -> {
System.out.println("eval");
try {
latch.await();
} catch (InterruptedException ignored) {
}
client.getStringValue("a", "a");
client.getStringValue("b", "a");
client.getStringValue("c", "a");
client.getStringValue("d", "a");

});
Thread hookAdder = new Thread(() -> {
System.out.println("hook adder");
try {
latch.await();
} catch (InterruptedException ignored) {
}
client.addHooks(new Hook() {});

});
System.out.println("starting...");
//eval.start();
//hookAdder.start();
System.out.println("started");
Thread.sleep(200);
client.getStringValue("d", "a");
client.addHooks(new Hook() {});
client.getStringValue("d", "a");
//latch.countDown();
eval.join();
hookAdder.join();
System.out.println(concurrentModException.get());*/
}
}
System.out.println("i = " + i);
System.out.println("jaVar = " + jaVar);
System.out.println("c.get() = " + c.get());
}
}
69 changes: 69 additions & 0 deletions src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;

import com.vmlens.api.AllInterleavings;
import dev.openfeature.sdk.exceptions.FatalError;
import dev.openfeature.sdk.fixtures.HookFixtures;
import dev.openfeature.sdk.providers.memory.Flag;
import dev.openfeature.sdk.providers.memory.InMemoryProvider;
import dev.openfeature.sdk.testutils.TestEventsProvider;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -104,4 +113,64 @@ void shouldNotCallEvaluationMethodsWhenProviderIsInNotReadyState() {

assertThat(details.getErrorCode()).isEqualTo(ErrorCode.PROVIDER_NOT_READY);
}

//@Test
void a() throws InterruptedException {
OpenFeatureAPI api = new OpenFeatureAPI();

var flags = new HashMap<String, Flag<?>>();
flags.put("a", Flag.builder().variant("a", "def").defaultVariant("a").build());
flags.put("b", Flag.builder().variant("a", "as").defaultVariant("a").build());
flags.put("c", Flag.builder().variant("a", "dfs").defaultVariant("a").build());
flags.put("d", Flag.builder().variant("a", "asddd").defaultVariant("a").build());
api.setProviderAndWait(new InMemoryProvider(flags));

var countDownLatch = new CountDownLatch(1);
var client = new OpenFeatureClient(api, "name", "version");
var isRunning = new AtomicBoolean(true);
var eval = new Thread(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("starting eval");
while (isRunning.get()) {
client.getStringValue("a", "def");
client.getStringValue("b", "def");
client.getStringValue("c", "def");
client.getStringValue("d", "def");
}
});
var hook = new Thread(() -> {
try {
countDownLatch.await();
Thread.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("starting hook");
while (isRunning.get()) {
for (int i = 0; i < 100; i++) {

client.addHooks(new Hook() {});
}

for (int i = 0; i < 90; i++) {

client.getHooks().remove(4);
}
}
});

eval.start();
hook.start();
Thread.sleep(100);
countDownLatch.countDown();
Thread.sleep(40000);
isRunning.set(false);
eval.join();
hook.join();
System.out.println(client.getHooks().size());
}
}
44 changes: 44 additions & 0 deletions src/test/java/dev/openfeature/sdk/VmLensTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.openfeature.sdk;

import com.vmlens.api.AllInterleavings;
import com.vmlens.api.Runner;
import dev.openfeature.sdk.providers.memory.Flag;
import dev.openfeature.sdk.providers.memory.InMemoryProvider;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Javadoc.
*/
public class VmLensTest {
public static void main(String[] args) throws InterruptedException {
new VmLensTest().asomeMethod();
}

@Test
public void asomeMethod() throws InterruptedException {
var c = new AtomicInteger();
final OpenFeatureAPI api = new OpenFeatureAPI();

var flags = new HashMap<String, Flag<?>>();
flags.put("a", Flag.builder().variant("a", "def").defaultVariant("a").build());
flags.put("b", Flag.builder().variant("a", "as").defaultVariant("a").build());
flags.put("c", Flag.builder().variant("a", "dfs").defaultVariant("a").build());
flags.put("d", Flag.builder().variant("a", "asddd").defaultVariant("a").build());
api.setProviderAndWait(new InMemoryProvider(flags));

try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent evaluations and hook additions")) {
while (allInterleavings.hasNext()) {
var client = api.getClient();
c.incrementAndGet();
Runner.runParallel(
() -> client.getStringValue("a", "a"),
() -> client.addHooks(new Hook() {})
);
}
}
api.shutdown();
System.out.println("c = " + c);
}
}
Loading
Loading