github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/reporter_simple_test.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package libkbfs 6 7 import ( 8 "context" 9 "errors" 10 "testing" 11 12 "github.com/keybase/client/go/kbfs/data" 13 "github.com/keybase/client/go/kbfs/tlf" 14 ) 15 16 func checkReportedErrors(t *testing.T, expected []error, 17 got []ReportedError) { 18 if len(expected) != len(got) { 19 t.Errorf("Unexpected number of errors: %d", len(got)) 20 return 21 } 22 23 for i, e := range expected { 24 g := got[i] 25 if e != g.Error { 26 t.Errorf("Unexpected error at %d: %s vs %s", i, e, g.Error) 27 } 28 } 29 } 30 31 func TestReporterSimpleMaxLimited(t *testing.T) { 32 ctx := context.Background() 33 r := NewReporterSimple(data.WallClock{}, 3) 34 err1 := errors.New("1") 35 r.ReportErr(ctx, "", tlf.Private, ReadMode, err1) 36 checkReportedErrors(t, []error{err1}, r.AllKnownErrors()) 37 err2 := errors.New("2") 38 r.ReportErr(ctx, "", tlf.Private, ReadMode, err2) 39 err3 := errors.New("3") 40 r.ReportErr(ctx, "", tlf.Private, ReadMode, err3) 41 checkReportedErrors(t, []error{err1, err2, err3}, r.AllKnownErrors()) 42 err4 := errors.New("4") 43 r.ReportErr(ctx, "", tlf.Private, ReadMode, err4) 44 checkReportedErrors(t, []error{err2, err3, err4}, r.AllKnownErrors()) 45 err5 := errors.New("5") 46 r.ReportErr(ctx, "", tlf.Private, ReadMode, err5) 47 err6 := errors.New("6") 48 r.ReportErr(ctx, "", tlf.Private, ReadMode, err6) 49 checkReportedErrors(t, []error{err4, err5, err6}, r.AllKnownErrors()) 50 } 51 52 func TestReporterSimpleUnlimited(t *testing.T) { 53 ctx := context.Background() 54 r := NewReporterSimple(data.WallClock{}, 0) 55 err1 := errors.New("1") 56 r.ReportErr(ctx, "", tlf.Private, ReadMode, err1) 57 checkReportedErrors(t, []error{err1}, r.AllKnownErrors()) 58 err2 := errors.New("2") 59 r.ReportErr(ctx, "", tlf.Private, ReadMode, err2) 60 err3 := errors.New("3") 61 r.ReportErr(ctx, "", tlf.Private, ReadMode, err3) 62 checkReportedErrors(t, []error{err1, err2, err3}, r.AllKnownErrors()) 63 err4 := errors.New("4") 64 r.ReportErr(ctx, "", tlf.Private, ReadMode, err4) 65 checkReportedErrors(t, []error{err1, err2, err3, err4}, r.AllKnownErrors()) 66 err5 := errors.New("5") 67 r.ReportErr(ctx, "", tlf.Private, ReadMode, err5) 68 err6 := errors.New("6") 69 r.ReportErr(ctx, "", tlf.Private, ReadMode, err6) 70 checkReportedErrors(t, []error{err1, err2, err3, err4, err5, err6}, 71 r.AllKnownErrors()) 72 }