|
| 1 | +package openfeature |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// testContextAwareProvider is a test provider that implements ContextAwareStateHandler |
| 10 | +type testContextAwareProvider struct { |
| 11 | + initDelay time.Duration |
| 12 | +} |
| 13 | + |
| 14 | +func (p *testContextAwareProvider) Metadata() Metadata { |
| 15 | + return Metadata{Name: "test-context-aware-provider"} |
| 16 | +} |
| 17 | + |
| 18 | +// InitWithContext implements ContextAwareStateHandler |
| 19 | +func (p *testContextAwareProvider) InitWithContext(ctx context.Context, evalCtx EvaluationContext) error { |
| 20 | + select { |
| 21 | + case <-time.After(p.initDelay): |
| 22 | + return nil |
| 23 | + case <-ctx.Done(): |
| 24 | + return ctx.Err() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Init implements StateHandler for backward compatibility |
| 29 | +func (p *testContextAwareProvider) Init(evalCtx EvaluationContext) error { |
| 30 | + return p.InitWithContext(context.Background(), evalCtx) |
| 31 | +} |
| 32 | + |
| 33 | +func (p *testContextAwareProvider) Shutdown() {} |
| 34 | + |
| 35 | +func (p *testContextAwareProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, flatCtx FlattenedContext) BoolResolutionDetail { |
| 36 | + return BoolResolutionDetail{ |
| 37 | + Value: defaultValue, |
| 38 | + ProviderResolutionDetail: ProviderResolutionDetail{Reason: DefaultReason}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (p *testContextAwareProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, flatCtx FlattenedContext) StringResolutionDetail { |
| 43 | + return StringResolutionDetail{ |
| 44 | + Value: defaultValue, |
| 45 | + ProviderResolutionDetail: ProviderResolutionDetail{Reason: DefaultReason}, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (p *testContextAwareProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, flatCtx FlattenedContext) FloatResolutionDetail { |
| 50 | + return FloatResolutionDetail{ |
| 51 | + Value: defaultValue, |
| 52 | + ProviderResolutionDetail: ProviderResolutionDetail{Reason: DefaultReason}, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (p *testContextAwareProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, flatCtx FlattenedContext) IntResolutionDetail { |
| 57 | + return IntResolutionDetail{ |
| 58 | + Value: defaultValue, |
| 59 | + ProviderResolutionDetail: ProviderResolutionDetail{Reason: DefaultReason}, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (p *testContextAwareProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue any, flatCtx FlattenedContext) InterfaceResolutionDetail { |
| 64 | + return InterfaceResolutionDetail{ |
| 65 | + Value: defaultValue, |
| 66 | + ProviderResolutionDetail: ProviderResolutionDetail{Reason: DefaultReason}, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (p *testContextAwareProvider) Hooks() []Hook { |
| 71 | + return []Hook{} |
| 72 | +} |
| 73 | + |
| 74 | +func TestContextAwareInitialization(t *testing.T) { |
| 75 | + // Save original state |
| 76 | + originalAPI := api |
| 77 | + originalEventing := eventing |
| 78 | + defer func() { |
| 79 | + api = originalAPI |
| 80 | + eventing = originalEventing |
| 81 | + }() |
| 82 | + |
| 83 | + // Create fresh API for isolated testing |
| 84 | + exec := newEventExecutor() |
| 85 | + testAPI := newEvaluationAPI(exec) |
| 86 | + api = testAPI |
| 87 | + eventing = exec |
| 88 | + |
| 89 | + t.Run("fast provider succeeds within timeout", func(t *testing.T) { |
| 90 | + fastProvider := &testContextAwareProvider{initDelay: 10 * time.Millisecond} |
| 91 | + |
| 92 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 93 | + defer cancel() |
| 94 | + |
| 95 | + err := SetProviderWithContextAndWait(ctx, fastProvider) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("Expected fast provider to succeed, got error: %v", err) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("slow provider times out", func(t *testing.T) { |
| 102 | + slowProvider := &testContextAwareProvider{initDelay: 200 * time.Millisecond} |
| 103 | + |
| 104 | + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) |
| 105 | + defer cancel() |
| 106 | + |
| 107 | + err := SetProviderWithContextAndWait(ctx, slowProvider) |
| 108 | + if err == nil { |
| 109 | + t.Error("Expected timeout error but got success") |
| 110 | + } |
| 111 | + if err != context.DeadlineExceeded { |
| 112 | + t.Errorf("Expected context deadline exceeded, got: %v", err) |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("async initialization returns immediately", func(t *testing.T) { |
| 117 | + asyncProvider := &testContextAwareProvider{initDelay: 50 * time.Millisecond} |
| 118 | + |
| 119 | + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) |
| 120 | + defer cancel() |
| 121 | + |
| 122 | + start := time.Now() |
| 123 | + err := SetProviderWithContext(ctx, asyncProvider) |
| 124 | + elapsed := time.Since(start) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + t.Errorf("Async setup should not fail: %v", err) |
| 128 | + } |
| 129 | + if elapsed > 25*time.Millisecond { |
| 130 | + t.Errorf("Async setup took too long: %v", elapsed) |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("named provider with context works", func(t *testing.T) { |
| 135 | + namedProvider := &testContextAwareProvider{initDelay: 10 * time.Millisecond} |
| 136 | + |
| 137 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 138 | + defer cancel() |
| 139 | + |
| 140 | + err := SetNamedProviderWithContextAndWait(ctx, "test-domain", namedProvider) |
| 141 | + if err != nil { |
| 142 | + t.Errorf("Named provider should succeed: %v", err) |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + t.Run("backward compatibility with regular provider", func(t *testing.T) { |
| 147 | + legacyProvider := &NoopProvider{} |
| 148 | + |
| 149 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 150 | + defer cancel() |
| 151 | + |
| 152 | + err := SetProviderWithContextAndWait(ctx, legacyProvider) |
| 153 | + if err != nil { |
| 154 | + t.Errorf("Legacy provider should work: %v", err) |
| 155 | + } |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +func TestContextAwareStateHandlerDetection(t *testing.T) { |
| 160 | + // Test that the initializerWithContext function correctly detects ContextAwareStateHandler |
| 161 | + evalCtx := EvaluationContext{} |
| 162 | + |
| 163 | + t.Run("detects ContextAwareStateHandler", func(t *testing.T) { |
| 164 | + provider := &testContextAwareProvider{initDelay: 1 * time.Millisecond} |
| 165 | + |
| 166 | + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) |
| 167 | + defer cancel() |
| 168 | + |
| 169 | + event, err := initializerWithContext(ctx, provider, evalCtx) |
| 170 | + if err != nil { |
| 171 | + t.Errorf("Context-aware provider should initialize successfully: %v", err) |
| 172 | + } |
| 173 | + if event.EventType != ProviderReady { |
| 174 | + t.Errorf("Expected ProviderReady event, got: %v", event.EventType) |
| 175 | + } |
| 176 | + }) |
| 177 | + |
| 178 | + t.Run("falls back to regular StateHandler", func(t *testing.T) { |
| 179 | + provider := &NoopProvider{} |
| 180 | + |
| 181 | + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) |
| 182 | + defer cancel() |
| 183 | + |
| 184 | + event, err := initializerWithContext(ctx, provider, evalCtx) |
| 185 | + if err != nil { |
| 186 | + t.Errorf("Regular provider should initialize successfully: %v", err) |
| 187 | + } |
| 188 | + if event.EventType != ProviderReady { |
| 189 | + t.Errorf("Expected ProviderReady event, got: %v", event.EventType) |
| 190 | + } |
| 191 | + }) |
| 192 | + |
| 193 | + t.Run("handles timeout in context-aware provider", func(t *testing.T) { |
| 194 | + provider := &testContextAwareProvider{initDelay: 100 * time.Millisecond} |
| 195 | + |
| 196 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 197 | + defer cancel() |
| 198 | + |
| 199 | + event, err := initializerWithContext(ctx, provider, evalCtx) |
| 200 | + if err == nil { |
| 201 | + t.Error("Expected timeout error") |
| 202 | + } |
| 203 | + if err != context.DeadlineExceeded { |
| 204 | + t.Errorf("Expected deadline exceeded, got: %v", err) |
| 205 | + } |
| 206 | + if event.EventType != ProviderError { |
| 207 | + t.Errorf("Expected ProviderError event, got: %v", event.EventType) |
| 208 | + } |
| 209 | + }) |
| 210 | +} |
0 commit comments