|
| 1 | +package dev.openfeature.sdk.testutils; |
| 2 | + |
| 3 | +import dev.openfeature.sdk.EvaluationContext; |
| 4 | +import dev.openfeature.sdk.EventProvider; |
| 5 | +import dev.openfeature.sdk.Metadata; |
| 6 | +import dev.openfeature.sdk.ProviderEvaluation; |
| 7 | +import dev.openfeature.sdk.ProviderEvent; |
| 8 | +import dev.openfeature.sdk.ProviderEventDetails; |
| 9 | +import dev.openfeature.sdk.Value; |
| 10 | +import java.util.function.Consumer; |
| 11 | + |
| 12 | +public class TestStackedEmitCallsProvider extends EventProvider { |
| 13 | + private final NestedBlockingEmitter nestedBlockingEmitter = new NestedBlockingEmitter(this::onProviderEvent); |
| 14 | + |
| 15 | + @Override |
| 16 | + public Metadata getMetadata() { |
| 17 | + return () -> getClass().getSimpleName(); |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public void initialize(EvaluationContext evaluationContext) throws Exception { |
| 22 | + synchronized (nestedBlockingEmitter) { |
| 23 | + nestedBlockingEmitter.init(); |
| 24 | + while (!nestedBlockingEmitter.isReady()) { |
| 25 | + try { |
| 26 | + nestedBlockingEmitter.wait(); |
| 27 | + } catch (InterruptedException e) { |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private void onProviderEvent(ProviderEvent providerEvent) { |
| 34 | + synchronized (nestedBlockingEmitter) { |
| 35 | + if (providerEvent == ProviderEvent.PROVIDER_READY) { |
| 36 | + nestedBlockingEmitter.setReady(); |
| 37 | + /* |
| 38 | + * This line deadlocked in the original implementation without the emitterExecutor see |
| 39 | + * https://github.com/open-feature/java-sdk/issues/1299 |
| 40 | + */ |
| 41 | + emitProviderReady(ProviderEventDetails.builder().build()); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
| 48 | + throw new UnsupportedOperationException("Unimplemented method 'getBooleanEvaluation'"); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
| 53 | + throw new UnsupportedOperationException("Unimplemented method 'getStringEvaluation'"); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
| 58 | + throw new UnsupportedOperationException("Unimplemented method 'getIntegerEvaluation'"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
| 63 | + throw new UnsupportedOperationException("Unimplemented method 'getDoubleEvaluation'"); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
| 68 | + throw new UnsupportedOperationException("Unimplemented method 'getObjectEvaluation'"); |
| 69 | + } |
| 70 | + |
| 71 | + static class NestedBlockingEmitter { |
| 72 | + |
| 73 | + private final Consumer<ProviderEvent> emitProviderEvent; |
| 74 | + private volatile boolean isReady; |
| 75 | + |
| 76 | + public NestedBlockingEmitter(Consumer<ProviderEvent> emitProviderEvent) { |
| 77 | + this.emitProviderEvent = emitProviderEvent; |
| 78 | + } |
| 79 | + |
| 80 | + public void init() { |
| 81 | + // run init outside monitored thread |
| 82 | + new Thread(() -> { |
| 83 | + try { |
| 84 | + Thread.sleep(500); |
| 85 | + } catch (InterruptedException e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + |
| 89 | + emitProviderEvent.accept(ProviderEvent.PROVIDER_READY); |
| 90 | + }) |
| 91 | + .start(); |
| 92 | + } |
| 93 | + |
| 94 | + public boolean isReady() { |
| 95 | + return isReady; |
| 96 | + } |
| 97 | + |
| 98 | + public synchronized void setReady() { |
| 99 | + isReady = true; |
| 100 | + this.notifyAll(); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments