mirror of
https://github.com/gogrlx/snack.git
synced 2026-04-02 05:08:42 -07:00
detect: cache Default() result with sync.Once, add Reset()
Co-authored-by: taigrr <8261498+taigrr@users.noreply.github.com>
This commit is contained in:
@@ -20,3 +20,26 @@ func TestDefaultDoesNotPanic(t *testing.T) {
|
||||
// May return error if no managers available; that's fine.
|
||||
_, _ = Default()
|
||||
}
|
||||
|
||||
func TestDefaultCachesResult(t *testing.T) {
|
||||
Reset()
|
||||
m1, err1 := Default()
|
||||
m2, err2 := Default()
|
||||
if m1 != m2 {
|
||||
t.Error("expected Default() to return the same Manager on repeated calls")
|
||||
}
|
||||
if err1 != err2 {
|
||||
t.Error("expected Default() to return the same error on repeated calls")
|
||||
}
|
||||
// Exactly one of m1/err1 must be non-nil (either a manager was found or not).
|
||||
if (m1 == nil) == (err1 == nil) {
|
||||
t.Error("expected exactly one of Manager or error to be non-nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetAllowsRedetection(t *testing.T) {
|
||||
_, _ = Default()
|
||||
Reset()
|
||||
// After reset, defaultOnce should be fresh; calling Default() again should work.
|
||||
_, _ = Default()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user