github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/analytics/fake.go (about) 1 package analytics 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 8 "github.com/tilt-dev/wmclient/pkg/analytics" 9 ) 10 11 type FakeOpter struct { 12 initialOpt analytics.Opt 13 calls []analytics.Opt 14 mu sync.Mutex 15 } 16 17 func NewFakeOpter(initialOpt analytics.Opt) *FakeOpter { 18 return &FakeOpter{initialOpt: initialOpt} 19 } 20 21 func DefaultFakeOpter() *FakeOpter { 22 return NewFakeOpter(analytics.OptDefault) 23 } 24 25 func (to *FakeOpter) ReadUserOpt() (analytics.Opt, error) { 26 to.mu.Lock() 27 defer to.mu.Unlock() 28 if len(to.calls) == 0 { 29 return to.initialOpt, nil 30 } 31 return to.calls[len(to.calls)-1], nil 32 } 33 34 func (to *FakeOpter) SetUserOpt(opt analytics.Opt) error { 35 to.mu.Lock() 36 defer to.mu.Unlock() 37 to.calls = append(to.calls, opt) 38 return nil 39 } 40 41 func (to *FakeOpter) Calls() []analytics.Opt { 42 to.mu.Lock() 43 defer to.mu.Unlock() 44 return append([]analytics.Opt{}, to.calls...) 45 } 46 47 func (to *FakeOpter) WaitUntilCount(t *testing.T, expectedCount int) { 48 timeout := time.After(time.Second) 49 for { 50 select { 51 case <-time.After(5 * time.Millisecond): 52 actualCount := len(to.Calls()) 53 if actualCount == expectedCount { 54 return 55 } 56 case <-timeout: 57 actualCount := len(to.Calls()) 58 t.Errorf("waiting for opt setting count to be %d. opt setting count is currently %d", expectedCount, actualCount) 59 t.FailNow() 60 } 61 } 62 } 63 64 var _ AnalyticsOpter = &FakeOpter{}