Skip to content

Commit

Permalink
added test to show logging on shutdown if error happens (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethgrid authored Dec 8, 2024
1 parent bf7ae6a commit 2966923
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ func (s *Server) Close() error {
})
}

if s.eventStore != nil {
// Launch a goroutine to close the event store
g.Go(func() error {
err := s.eventStore.Close()
if err != nil {
s.parentLogger.Error("unable to close event store", "error", err.Error())
}
return err
})
}

// Wait for all goroutines to complete and return any error
return g.Wait()
}
Expand Down
18 changes: 16 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestEventStoreErr(t *testing.T) {
require.Contains(t, logbuf.String(), "oh noes, mysql err")
}

func TestGracefulShutdown(t *testing.T) {
func TestGracefulShutdownOK(t *testing.T) {
srv, err := newTestServer()
require.NoError(t, err)

Expand Down Expand Up @@ -83,6 +83,20 @@ func TestGracefulShutdown(t *testing.T) {
wg.Wait()
}

func TestGracefulShutdownErr(t *testing.T) {
logbuf := lockbuffer.NewLockBuffer()

srv, err := newTestServer(WithLogWriter(logbuf))
require.NoError(t, err)

srv.eventStore = &fakeEventStore{err: fmt.Errorf("oh noes, close err")}

err = srv.Close()
require.Error(t, err)
assert.Contains(t, logbuf.String(), `"error":"oh noes, close err"`)
assert.Contains(t, logbuf.String(), `"msg":"unable to close event store"`)
}

func TestContextTimeoutAndRequestTimeout(t *testing.T) {
logbuf := lockbuffer.NewLockBuffer()
customConfig := Config{
Expand Down Expand Up @@ -244,5 +258,5 @@ func (f *fakeEventStore) Write(userID int64, message string) error {
}

func (f *fakeEventStore) Close() error {
return nil
return f.err
}

0 comments on commit 2966923

Please sign in to comment.