From c1259879ad65851303f8bde8fb2674d36f1e9310 Mon Sep 17 00:00:00 2001 From: William Johansson Date: Thu, 31 Oct 2024 09:26:28 +0100 Subject: [PATCH] fix(spanner): use correct logger when stopping emulator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes a bug introduced in b1b5e7ed8973dc6b8b858b9ac67b2081b2a27c72 (PR #594) where an empty context is used, which removes the logger. Could be done cleaner with Go >=1.21 as can be seen in a TODO comment. Before: ``` ❯ make go-test [sage] building binary and generating Makefiles... [go-test] running Go tests... [go-test] starting Cloud Spanner emulator... [go-test] running Cloud Spanner emulator on 0.0.0.0:44611 [sage] stopping down Cloud Spanner emulator... ``` After: ``` ❯ make go-test [sage] building binary and generating Makefiles... [go-test] running Go tests... [go-test] starting Cloud Spanner emulator... [go-test] running Cloud Spanner emulator on 0.0.0.0:46023 [go-test] stopping down Cloud Spanner emulator... ``` --- tools/sgcloudspanner/emulator.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/sgcloudspanner/emulator.go b/tools/sgcloudspanner/emulator.go index 0c32789e..51ad06d0 100644 --- a/tools/sgcloudspanner/emulator.go +++ b/tools/sgcloudspanner/emulator.go @@ -44,21 +44,24 @@ func RunEmulator(ctx context.Context) (_ func(), err error) { } containerID := strings.TrimSpace(dockerRunStdout.String()) cleanup := func() { + // TODO(radhus): when bumping to Go >=1.21, we should define a context like this instead and avoid storing a logger. + // ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) + logger := sg.Logger(ctx) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - sg.Logger(ctx).Println("stopping down Cloud Spanner emulator...") + logger.Println("stopping down Cloud Spanner emulator...") cmd := sgdocker.Command(ctx, "kill", containerID) cmd.Stdout, cmd.Stderr = nil, nil if err := cmd.Run(); err != nil { - sg.Logger(ctx).Printf("failed to kill emulator container: %v", err) + logger.Printf("failed to kill emulator container: %v", err) } cmd = sgdocker.Command(ctx, "rm", "-v", containerID) cmd.Stdout, cmd.Stderr = nil, nil if err := cmd.Run(); err != nil { - sg.Logger(ctx).Printf("failed to remove emulator container: %v", err) + logger.Printf("failed to remove emulator container: %v", err) } if err := os.Unsetenv("SPANNER_EMULATOR_HOST"); err != nil { - sg.Logger(ctx).Printf("failed to unset SPANNER_EMULATOR_HOST: %v", err) + logger.Printf("failed to unset SPANNER_EMULATOR_HOST: %v", err) } } emulatorHost, err := inspectPortAddress(ctx, containerID, "9010/tcp")