-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: internal provider comparison causing race conditions in tests (#312
) fix: internal provider comparison causing race conditions in tests Signed-off-by: Bernd Warmuth <[email protected]> Co-authored-by: Todd Baert <[email protected]>
- Loading branch information
Showing
3 changed files
with
106 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package openfeature | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// newProviderRef creates a new providerReference instance that wraps around a FeatureProvider implementation | ||
func newProviderRef(provider FeatureProvider) providerReference { | ||
return providerReference{ | ||
featureProvider: provider, | ||
kind: reflect.TypeOf(provider).Kind(), | ||
shutdownSemaphore: make(chan interface{}), | ||
} | ||
} | ||
|
||
// providerReference is a helper struct to store FeatureProvider along with their | ||
// shutdown semaphore | ||
type providerReference struct { | ||
featureProvider FeatureProvider | ||
kind reflect.Kind | ||
shutdownSemaphore chan interface{} | ||
} | ||
|
||
func (pr providerReference) equals(other providerReference) bool { | ||
if pr.kind == reflect.Ptr && other.kind == reflect.Ptr { | ||
return pr.featureProvider == other.featureProvider | ||
} | ||
return reflect.DeepEqual(pr.featureProvider, other.featureProvider) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package openfeature | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestProviderReferenceEquals(t *testing.T) { | ||
|
||
type myProvider struct { | ||
NoopProvider | ||
field string | ||
} | ||
|
||
p1 := myProvider{} | ||
p2 := myProvider{} | ||
|
||
tests := []struct { | ||
name string | ||
pr1 providerReference | ||
pr2 providerReference | ||
expected bool | ||
}{ | ||
|
||
{ | ||
name: "both pointers, different instances", | ||
pr1: newProviderRef(&p1), | ||
pr2: newProviderRef(&p2), | ||
expected: false, | ||
}, | ||
{ | ||
name: "both pointers, same instance", | ||
pr1: newProviderRef(&p1), | ||
pr2: newProviderRef(&p1), | ||
expected: true, | ||
}, | ||
{ | ||
name: "different pointers, different instance", | ||
pr1: newProviderRef(p1), | ||
pr2: newProviderRef(&p1), | ||
expected: false, | ||
}, | ||
{ | ||
name: "no pointers, same instance", | ||
pr1: newProviderRef(p1), | ||
pr2: newProviderRef(p1), | ||
expected: true, | ||
}, | ||
{ | ||
name: "no pointers, different equal instances", | ||
pr1: newProviderRef(myProvider{field: "A"}), | ||
pr2: newProviderRef(myProvider{field: "A"}), | ||
expected: true, | ||
}, | ||
{ | ||
name: "no pointers, different not equal instances", | ||
pr1: newProviderRef(myProvider{field: "A"}), | ||
pr2: newProviderRef(myProvider{field: "B"}), | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.pr1.equals(tt.pr2); got != tt.expected { | ||
t.Errorf("providerReference.equals() = %v, want %v", got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |