Skip to content

Commit

Permalink
test: add a test to ensure no logs after stop
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jan 5, 2024
1 parent d02f2e6 commit f4db639
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ func (s *simpleReceiver) OnCallback(result string) {
s.ch <- result
}

type noLogAfterStopLogger struct {
StructuredLogger
shouldPanic atomic.Bool

Check failure on line 125 in client_test.go

View workflow job for this annotation

GitHub Actions / Test and Coverage

undefined: atomic.Bool
}

func (n *noLogAfterStopLogger) Log(keyVals ...interface{}) error {
if n.shouldPanic.Load() {
panic("oh no")
}
return n.StructuredLogger.Log(keyVals)
}

var _ = Describe("Client", func() {
formatOption := TransferFormat("Text")
j := 1
Expand Down Expand Up @@ -174,6 +186,38 @@ var _ = Describe("Client", func() {
server.cancel()
close(done)
})
It("should not log after stop", func(done Done) {
// Create a simple server
server, err := NewServer(context.TODO(), SimpleHubFactory(&simpleHub{}),
testLoggerOption(),
ChanReceiveTimeout(200*time.Millisecond),
StreamBufferCapacity(5))
Expect(err).NotTo(HaveOccurred())
Expect(server).NotTo(BeNil())
// Create both ends of the connection
cliConn, srvConn := newClientServerConnections()
// Start the server
go func() { _ = server.Serve(srvConn) }()
// Create the Client
clientConn, err := NewClient(context.Background(), WithConnection(cliConn), testLoggerOption(), formatOption)
Expect(err).NotTo(HaveOccurred())
Expect(clientConn).NotTo(BeNil())
// Replace loggers with loggers that panic after stop
info, debug := clientConn.loggers()
panicableInfo, panicableDebug := &noLogAfterStopLogger{StructuredLogger: info}, &noLogAfterStopLogger{StructuredLogger: debug}
clientConn.setLoggers(panicableInfo, panicableDebug)
// Start it
clientConn.Start()
Expect(<-clientConn.WaitForState(context.Background(), ClientConnected)).NotTo(HaveOccurred())
clientConn.Stop()
panicableInfo.shouldPanic.Store(true)
panicableDebug.shouldPanic.Store(true)
// Ensure that we really don't get any logs anymore
time.Sleep(1 * time.Second)
Expect(clientConn.State()).To(BeEquivalentTo(ClientClosed))
server.cancel()
close(done)
})
})
Context("Invoke", func() {
It("should invoke a server method and return the result", func(done Done) {
Expand Down

0 comments on commit f4db639

Please sign in to comment.