...
1
18
19 package health
20
21 import (
22 "sync"
23 "testing"
24 "time"
25
26 healthpb "google.golang.org/grpc/health/grpc_health_v1"
27 "google.golang.org/grpc/internal/grpctest"
28 )
29
30 type s struct {
31 grpctest.Tester
32 }
33
34 func Test(t *testing.T) {
35 grpctest.RunSubTests(t, s{})
36 }
37
38 func (s) TestShutdown(t *testing.T) {
39 const testService = "tteesstt"
40 s := NewServer()
41 s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
42
43 status := s.statusMap[testService]
44 if status != healthpb.HealthCheckResponse_SERVING {
45 t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
46 }
47
48 var wg sync.WaitGroup
49 wg.Add(2)
50
51 go func() {
52 for i := 0; i < 1000; i++ {
53 s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
54 time.Sleep(time.Microsecond)
55 }
56 wg.Done()
57 }()
58 go func() {
59 time.Sleep(300 * time.Microsecond)
60 s.Shutdown()
61 wg.Done()
62 }()
63 wg.Wait()
64
65 s.mu.Lock()
66 status = s.statusMap[testService]
67 s.mu.Unlock()
68 if status != healthpb.HealthCheckResponse_NOT_SERVING {
69 t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
70 }
71
72 s.Resume()
73 status = s.statusMap[testService]
74 if status != healthpb.HealthCheckResponse_SERVING {
75 t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
76 }
77
78 s.SetServingStatus(testService, healthpb.HealthCheckResponse_NOT_SERVING)
79 status = s.statusMap[testService]
80 if status != healthpb.HealthCheckResponse_NOT_SERVING {
81 t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
82 }
83 }
84
View as plain text