Skip to content

Commit

Permalink
Merge pull request #43 from k1LoW/flipflop
Browse files Browse the repository at this point in the history
Add `flipflop` service for grpc.health.v1.Health.Watch stubbing
  • Loading branch information
k1LoW authored May 17, 2023
2 parents 5f20e5f + 80eacbc commit a2a2bdf
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions grpcstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ import (
"google.golang.org/protobuf/reflect/protoregistry"
)

type serverStatus int

const (
status_unknown serverStatus = iota
status_start
status_starting
status_closing
status_closed
)

type Message map[string]interface{}

type Request struct {
Expand Down Expand Up @@ -81,6 +91,7 @@ type Server struct {
cc *grpc.ClientConn
requests []*Request
healthCheck bool
status serverStatus
t *testing.T
mu sync.RWMutex
}
Expand Down Expand Up @@ -142,6 +153,10 @@ func NewTLSServer(t *testing.T, proto string, cacert, cert, key []byte, opts ...

// Close shuts down *grpc.Server
func (s *Server) Close() {
s.status = status_closing
defer func() {
s.status = status_closed
}()
s.t.Helper()
if s.listener == nil {
s.t.Error("server is not started yet")
Expand Down Expand Up @@ -217,6 +232,10 @@ func (s *Server) ClientConn() *grpc.ClientConn {
}

func (s *Server) startServer() {
s.status = status_starting
defer func() {
s.status = status_start
}()
s.t.Helper()
reflection.Register(s.server)
s.registerServer()
Expand Down Expand Up @@ -423,6 +442,22 @@ func (s *Server) registerServer() {
healthSrv := health.NewServer()
healthpb.RegisterHealthServer(s.server, healthSrv)
healthSrv.SetServingStatus("grpcstub", healthpb.HealthCheckResponse_SERVING)
go func() {
status := healthpb.HealthCheckResponse_SERVING
healthSrv.SetServingStatus("flipflop", status)
for {
switch s.status {
case status_start, status_starting:
if status == healthpb.HealthCheckResponse_SERVING {
status = healthpb.HealthCheckResponse_NOT_SERVING
} else {
status = healthpb.HealthCheckResponse_SERVING
}
healthSrv.SetServingStatus("flipflop", status)
}
time.Sleep(100 * time.Millisecond)
}
}()
}
}

Expand Down

0 comments on commit a2a2bdf

Please sign in to comment.