Skip to content

Commit

Permalink
refactor!: RunUntilStopped now accepts a chan <-struct{} instead of c…
Browse files Browse the repository at this point in the history
…han <-interface{} (#33)

Actually achieving what's described in
#32 (comment)
 returns a  and not a ...
  • Loading branch information
Guillaume Bouvignies authored Mar 16, 2023
1 parent 552c0ce commit 1f3f722
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions golang/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func (server MinimalGRPCServer) RunUntilInterrupted() error {
// Signals are used to interrupt the server, so we catch them here
termSignalChan := make(chan os.Signal, 1)
signal.Notify(termSignalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
serverStopChan := make(chan interface{}, 1)
serverStopChan := make(chan struct{}, 1)
go func() {
interruptSignal := <-termSignalChan
<-termSignalChan
interruptSignal := struct{}{}
serverStopChan <- interruptSignal
}()
if err := server.RunUntilStopped(serverStopChan); err != nil {
Expand All @@ -48,7 +49,7 @@ func (server MinimalGRPCServer) RunUntilInterrupted() error {
}

// Runs the server synchronously until a signal is received on the given channel
func (server MinimalGRPCServer) RunUntilStopped(stopper <-chan interface{}) error {
func (server MinimalGRPCServer) RunUntilStopped(stopper <-chan struct{}) error {
loggingInterceptorFunc := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
grpcMethod := info.FullMethod
logrus.Debugf("Received gRPC request to method '%v' with args:\n%+v", grpcMethod, req)
Expand Down
14 changes: 7 additions & 7 deletions golang/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ import (
)

const (
listenPort = uint16(9003)
listenPort = uint16(9003)
stopGracePeriod = 10 * time.Second
)

func TestManualShutdown(t *testing.T) {
server := NewMinimalGRPCServer(listenPort, stopGracePeriod, []func(*grpc.Server){})

stopChan := make(chan interface{})
stopChan := make(chan struct{})
runResultChan := make(chan error)
go func() {
runResultChan <- server.RunUntilStopped(stopChan)
}()
select {
case err := <- runResultChan:
case err := <-runResultChan:
t.Fatalf("The server unexpectedly returned a result before we sent a signal to stop it:\n%v", err)
case <- time.After(1 * time.Second):
case <-time.After(1 * time.Second):
// Nothing
}
stopChan <- "Stop now"
stopChan <- struct{}{}

maxWaitForServerToStop := 1 * time.Second
var errAfterStop error
select {
case errAfterStop = <- runResultChan:
case errAfterStop = <-runResultChan:
// Assignment is all we have to do
case <- time.After(maxWaitForServerToStop):
case <-time.After(maxWaitForServerToStop):
t.Fatalf("Expected the server to have stopped after %v, but it didn't", maxWaitForServerToStop)
}
assert.NoError(t, errAfterStop, "Expected the server to exit without an error but it threw the following:\n%v", errAfterStop)
Expand Down

0 comments on commit 1f3f722

Please sign in to comment.