|
| 1 | +package testing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "runtime" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/open-feature/go-sdk/openfeature" |
| 11 | + "github.com/open-feature/go-sdk/openfeature/memprovider" |
| 12 | +) |
| 13 | + |
| 14 | +const testNameKey = "testName" |
| 15 | + |
| 16 | +// NewTestProvider creates a new `TestAwareProvider` |
| 17 | +func NewTestProvider() TestProvider { |
| 18 | + return TestProvider{ |
| 19 | + providers: &sync.Map{}, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// TestProvider is the recommended way to defined flags within the scope of a test. |
| 24 | +// It uses the InMemoryProvider, with flags scoped per test. |
| 25 | +// Before executing a test, specify the flag values to be used for the specific test using the UsingFlags function |
| 26 | +type TestProvider struct { |
| 27 | + openfeature.NoopProvider |
| 28 | + providers *sync.Map |
| 29 | +} |
| 30 | + |
| 31 | +// UsingFlags sets flags for the scope of a test |
| 32 | +func (tp TestProvider) UsingFlags(test *testing.T, flags map[string]memprovider.InMemoryFlag) { |
| 33 | + storeGoroutineLocal(test.Name()) |
| 34 | + tp.providers.Store(test.Name(), memprovider.NewInMemoryProvider(flags)) |
| 35 | +} |
| 36 | + |
| 37 | +// Cleanup deletes the flags provider bound to the current test and should be executed after each test execution |
| 38 | +// e.g. using a defer statement. |
| 39 | +func (tp TestProvider) Cleanup() { |
| 40 | + tp.providers.Delete(getGoroutineLocal()) |
| 41 | + deleteGoroutineLocal() |
| 42 | +} |
| 43 | + |
| 44 | +func (tp TestProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, flCtx openfeature.FlattenedContext) openfeature.BoolResolutionDetail { |
| 45 | + return tp.getProvider().BooleanEvaluation(ctx, flag, defaultValue, flCtx) |
| 46 | +} |
| 47 | + |
| 48 | +func (tp TestProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, flCtx openfeature.FlattenedContext) openfeature.StringResolutionDetail { |
| 49 | + return tp.getProvider().StringEvaluation(ctx, flag, defaultValue, flCtx) |
| 50 | +} |
| 51 | + |
| 52 | +func (tp TestProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, flCtx openfeature.FlattenedContext) openfeature.FloatResolutionDetail { |
| 53 | + return tp.getProvider().FloatEvaluation(ctx, flag, defaultValue, flCtx) |
| 54 | +} |
| 55 | + |
| 56 | +func (tp TestProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, flCtx openfeature.FlattenedContext) openfeature.IntResolutionDetail { |
| 57 | + return tp.getProvider().IntEvaluation(ctx, flag, defaultValue, flCtx) |
| 58 | +} |
| 59 | + |
| 60 | +func (tp TestProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, flCtx openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail { |
| 61 | + return tp.getProvider().ObjectEvaluation(ctx, flag, defaultValue, flCtx) |
| 62 | +} |
| 63 | + |
| 64 | +func (tp TestProvider) Hooks() []openfeature.Hook { |
| 65 | + return tp.NoopProvider.Hooks() |
| 66 | +} |
| 67 | + |
| 68 | +func (tp TestProvider) Metadata() openfeature.Metadata { |
| 69 | + return tp.NoopProvider.Metadata() |
| 70 | +} |
| 71 | + |
| 72 | +func (tp TestProvider) getProvider() openfeature.FeatureProvider { |
| 73 | + // Retrieve the test name from the goroutine-local storage. |
| 74 | + testName, ok := getGoroutineLocal().(string) |
| 75 | + if !ok { |
| 76 | + panic("unable to detect test name; be sure to call `UsingFlags` in the scope of a test (in T.run)!") |
| 77 | + } |
| 78 | + |
| 79 | + // Load the feature provider corresponding to the test name. |
| 80 | + provider, ok := tp.providers.Load(testName) |
| 81 | + if !ok { |
| 82 | + panic("unable to find feature provider for given test name: " + testName) |
| 83 | + } |
| 84 | + |
| 85 | + // Assert that the loaded provider is of type openfeature.FeatureProvider. |
| 86 | + featureProvider, ok := provider.(openfeature.FeatureProvider) |
| 87 | + if !ok { |
| 88 | + panic("invalid type for feature provider for given test name: " + testName) |
| 89 | + } |
| 90 | + |
| 91 | + return featureProvider |
| 92 | +} |
| 93 | + |
| 94 | +var goroutineLocalData sync.Map |
| 95 | + |
| 96 | +func storeGoroutineLocal(value interface{}) { |
| 97 | + gID := getGoroutineID() |
| 98 | + goroutineLocalData.Store(fmt.Sprintf("%d_%v", gID, testNameKey), value) |
| 99 | +} |
| 100 | + |
| 101 | +func getGoroutineLocal() interface{} { |
| 102 | + gID := getGoroutineID() |
| 103 | + value, _ := goroutineLocalData.Load(fmt.Sprintf("%d_%v", gID, testNameKey)) |
| 104 | + return value |
| 105 | +} |
| 106 | + |
| 107 | +func deleteGoroutineLocal() { |
| 108 | + gID := getGoroutineID() |
| 109 | + goroutineLocalData.Delete(fmt.Sprintf("%d_%v", gID, testNameKey)) |
| 110 | +} |
| 111 | + |
| 112 | +func getGoroutineID() uint64 { |
| 113 | + var buf [64]byte |
| 114 | + n := runtime.Stack(buf[:], false) |
| 115 | + stackLine := string(buf[:n]) |
| 116 | + var gID uint64 |
| 117 | + _, err := fmt.Sscanf(stackLine, "goroutine %d ", &gID) |
| 118 | + if err != nil { |
| 119 | + panic("unable to extract GID from stack trace") |
| 120 | + } |
| 121 | + return gID |
| 122 | +} |
0 commit comments