github.com/letsencrypt/boulder@v0.20251208.0/grpc/server_test.go (about) 1 package grpc 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 "time" 8 9 blog "github.com/letsencrypt/boulder/log" 10 "github.com/letsencrypt/boulder/test" 11 "google.golang.org/grpc/health" 12 ) 13 14 func TestServerBuilderInitLongRunningCheck(t *testing.T) { 15 t.Parallel() 16 hs := health.NewServer() 17 mockLogger := blog.NewMock() 18 sb := &serverBuilder{ 19 healthSrv: hs, 20 logger: mockLogger, 21 checkInterval: time.Millisecond * 50, 22 } 23 ctx, cancel := context.WithCancel(context.Background()) 24 defer cancel() 25 26 count := 0 27 failEveryThirdCheck := func(context.Context) error { 28 count++ 29 if count%3 == 0 { 30 return errors.New("oops") 31 } 32 return nil 33 } 34 sb.initLongRunningCheck(ctx, "test", failEveryThirdCheck) 35 time.Sleep(time.Millisecond * 110) 36 cancel() 37 38 // We expect the following transition timeline: 39 // - ~0ms 1st check passed, NOT_SERVING to SERVING 40 // - ~50ms 2nd check passed, [no transition] 41 // - ~100ms 3rd check failed, SERVING to NOT_SERVING 42 serving := mockLogger.GetAllMatching(".*\"NOT_SERVING\" to \"SERVING\"") 43 notServing := mockLogger.GetAllMatching((".*\"SERVING\" to \"NOT_SERVING\"")) 44 test.Assert(t, len(serving) == 2, "expected two serving log lines") 45 test.Assert(t, len(notServing) == 2, "expected two not serving log lines") 46 47 mockLogger.Clear() 48 49 ctx, cancel = context.WithCancel(context.Background()) 50 defer cancel() 51 52 count = 0 53 failEveryOtherCheck := func(context.Context) error { 54 count++ 55 if count%2 == 0 { 56 return errors.New("oops") 57 } 58 return nil 59 } 60 sb.initLongRunningCheck(ctx, "test", failEveryOtherCheck) 61 time.Sleep(time.Millisecond * 110) 62 cancel() 63 64 // We expect the following transition timeline: 65 // - ~0ms 1st check passed, NOT_SERVING to SERVING 66 // - ~50ms 2nd check failed, SERVING to NOT_SERVING 67 // - ~100ms 3rd check passed, NOT_SERVING to SERVING 68 serving = mockLogger.GetAllMatching(".*\"NOT_SERVING\" to \"SERVING\"") 69 notServing = mockLogger.GetAllMatching((".*\"SERVING\" to \"NOT_SERVING\"")) 70 test.Assert(t, len(serving) == 4, "expected four serving log lines") 71 test.Assert(t, len(notServing) == 2, "expected two not serving log lines") 72 }