From 38e82f30f20763febd4237982b5181990f30589e Mon Sep 17 00:00:00 2001 From: "Derrick J. Wippler" Date: Thu, 29 Oct 2026 15:36:13 -0500 Subject: [PATCH] Pass the caller's logger to default transports in New and cluster.StartWith --- cluster/cluster.go | 2 +- cluster/cluster_test.go | 35 +++++++++++++++++++++++++++++++++++ instance.go | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cluster/cluster.go b/cluster/cluster.go index 8a0ef29..1e26c0d 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -102,7 +102,7 @@ func StartWith(ctx context.Context, peers []peer.Info, opts groupcache.Options) var parent transport.Transport if opts.Transport == nil { - parent = transport.NewHttpTransport(transport.HttpTransportOptions{}) + parent = transport.NewHttpTransport(transport.HttpTransportOptions{Logger: opts.Logger}) } else { parent = opts.Transport } diff --git a/cluster/cluster_test.go b/cluster/cluster_test.go index 67d63e9..c51fac3 100644 --- a/cluster/cluster_test.go +++ b/cluster/cluster_test.go @@ -17,8 +17,13 @@ limitations under the License. package cluster_test import ( + "bytes" "context" + "log/slog" + "strings" + "sync" "testing" + "time" "github.com/groupcache/groupcache-go/v3" "github.com/groupcache/groupcache-go/v3/cluster" @@ -81,3 +86,33 @@ func TestStartWithInvalidPeer(t *testing.T) { assert.Nil(t, cluster.ListDaemons()) require.NoError(t, cluster.Shutdown(context.Background())) } + +type syncBuffer struct { + mu sync.Mutex + buf bytes.Buffer +} + +func (s *syncBuffer) Write(p []byte) (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.buf.Write(p) +} + +func (s *syncBuffer) String() string { + s.mu.Lock() + defer s.mu.Unlock() + return s.buf.String() +} + +func TestStartLogsEveryDaemonToLogger(t *testing.T) { + var out syncBuffer + logger := slog.New(slog.NewTextHandler(&out, nil)) + + require.NoError(t, cluster.Start(context.Background(), 3, groupcache.Options{Logger: logger})) + defer func() { require.NoError(t, cluster.Shutdown(context.Background())) }() + + // The listening message is logged from a goroutine after the server starts. + require.Eventually(t, func() bool { + return strings.Count(out.String(), "Listening on") == 3 + }, 5*time.Second, 10*time.Millisecond) +} diff --git a/instance.go b/instance.go index 49ba356..e65057d 100644 --- a/instance.go +++ b/instance.go @@ -84,7 +84,7 @@ func New(opts Options) *Instance { } if opts.Transport == nil { - opts.Transport = transport.NewHttpTransport(transport.HttpTransportOptions{}) + opts.Transport = transport.NewHttpTransport(transport.HttpTransportOptions{Logger: opts.Logger}) } i := &Instance{