Skip to content

Commit ff09b1c

Browse files
authored
chore(deps): migrate to spanemuboost v0.3.3 with LazyEmulator (#560)
chore(deps): migrate to spanemuboost v0.3.3 with LazyEmulator Upgrade spanemuboost v0.3.2 → v0.3.3 and migrate integration tests to use LazyEmulator for deferred emulator initialization. Key changes: - Replace eager RunEmulator in TestMain with package-level LazyEmulator - Emulator starts only when first integration test needs it - Use new Clients.ClientOptions() and Clients.URI() accessors to eliminate *Emulator references from test helpers - Remove initializeSession's *Emulator parameter - Set non-zero exit code on emulator cleanup failure
1 parent 3e248f9 commit ff09b1c

File tree

5 files changed

+24
-43
lines changed

5 files changed

+24
-43
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/apstndb/gsqlutils v0.0.0-20250517013444-d2334c88d6ae
1616
github.com/apstndb/lox v0.0.0-20241212132733-62f24606dc82
1717
github.com/apstndb/memebridge v0.5.0
18-
github.com/apstndb/spanemuboost v0.3.2
18+
github.com/apstndb/spanemuboost v0.3.3
1919
github.com/apstndb/spannerplan v0.1.3
2020
github.com/apstndb/spantype v0.3.8
2121
github.com/apstndb/spanvalue v0.1.8

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,8 +2485,8 @@ github.com/apstndb/lox v0.0.0-20241212132733-62f24606dc82 h1:l54uIOgcH4r0lTg8xKR
24852485
github.com/apstndb/lox v0.0.0-20241212132733-62f24606dc82/go.mod h1:PbqzTjsPq7Xhn8D7Vk8g/em15kd8vvEL1Y2xkI9CgGs=
24862486
github.com/apstndb/memebridge v0.5.0 h1:WaWD0Wp3Yw1BZwyvT4bIf2XsXAA+vq9ZNxUKXXV/vZQ=
24872487
github.com/apstndb/memebridge v0.5.0/go.mod h1:fPrWYKcg5/eaavD6RovT9qeq8K7bDhgLKOcGhqg6zo4=
2488-
github.com/apstndb/spanemuboost v0.3.2 h1:Z09MqWIxOx/v5kzzpz3ZxteiHE4PEsogZBwAI7wY+9Y=
2489-
github.com/apstndb/spanemuboost v0.3.2/go.mod h1:uNXjl80HX4S8hK68HCobxRrNEBLhzE+EVxkwOGuwdRM=
2488+
github.com/apstndb/spanemuboost v0.3.3 h1:auQLIR7FUdqujOPjQBrW8kiOQBG419w4pHQTrs5vEaI=
2489+
github.com/apstndb/spanemuboost v0.3.3/go.mod h1:uNXjl80HX4S8hK68HCobxRrNEBLhzE+EVxkwOGuwdRM=
24902490
github.com/apstndb/spannerplan v0.1.3 h1:nFzTEvRL4yMzQeFdtWp3V0VaUidNA/o3T7HyIYIGk/U=
24912491
github.com/apstndb/spannerplan v0.1.3/go.mod h1:iPN9r9R2AknoFnBFqA232cUO+2ZkHpk4Q1qeSAU9xEM=
24922492
github.com/apstndb/spantype v0.3.8 h1:xy43Cclc2Hz6SL+3tZYuozOqJv6AML4Mv8cASgYo1O0=

internal/mycli/integration_mcp_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ func testRunMCPWithNonExistentDatabase(t *testing.T) {
214214
ctx := t.Context()
215215

216216
// First, create a real instance to test non-existent database
217-
clients := spanemuboost.SetupClients(t, emulator, spanemuboost.WithRandomInstanceID())
217+
clients := spanemuboost.SetupClients(t, lazyEmu, spanemuboost.WithRandomInstanceID())
218218

219219
// Create system variables with non-existent database in an existing instance
220-
host, port, err := parseEndpoint(emulator.URI())
220+
host, port, err := parseEndpoint(clients.URI())
221221
if err != nil {
222222
t.Fatalf("Failed to parse emulator URI: %v", err)
223223
}
@@ -237,7 +237,7 @@ func testRunMCPWithNonExistentDatabase(t *testing.T) {
237237
Params: make(map[string]ast.Node),
238238
}
239239

240-
sessionNonExistent, err := NewSession(ctx, &sysVarsNonExistent, emulator.ClientOptions()...)
240+
sessionNonExistent, err := NewSession(ctx, &sysVarsNonExistent, clients.ClientOptions()...)
241241
if err != nil {
242242
t.Fatalf("Failed to create session for non-existent database test: %v", err)
243243
}

internal/mycli/integration_test.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func dmlResult(n int) *Result {
127127
return &Result{AffectedRows: n, IsExecutedDML: true}
128128
}
129129

130-
var emulator *spanemuboost.Emulator
130+
var lazyEmu = spanemuboost.NewLazyEmulator(spanemuboost.EnableInstanceAutoConfigOnly())
131131

132132
func TestMain(m *testing.M) {
133133
// Clear Spanner-related environment variables that could interfere with tests.
@@ -138,33 +138,18 @@ func TestMain(m *testing.M) {
138138
_ = os.Unsetenv("SPANNER_DATABASE_ID")
139139
flag.Parse()
140140

141-
// Skip emulator setup in short mode
142-
if testing.Short() {
143-
os.Exit(m.Run())
144-
}
145-
146-
emu, err := spanemuboost.RunEmulator(context.Background(),
147-
spanemuboost.EnableInstanceAutoConfigOnly(),
148-
)
149-
if err != nil {
150-
// testing.M doesn't have output method
151-
slog.Error("failed to create emulator", "err", err)
152-
os.Exit(1)
153-
}
154-
155-
defer func() {
156-
if err := emu.Close(); err != nil {
157-
slog.Error("failed to close emulator container in TestMain", "error", err)
141+
code := m.Run()
142+
if err := lazyEmu.Close(); err != nil { // no-op if no test used the emulator
143+
slog.Error("failed to close emulator container in TestMain", "error", err)
144+
if code == 0 {
145+
code = 1
158146
}
159-
}()
160-
161-
emulator = emu
162-
163-
os.Exit(m.Run())
147+
}
148+
os.Exit(code)
164149
}
165150

166-
func initializeSession(ctx context.Context, emulator *spanemuboost.Emulator, clients *spanemuboost.Clients) (session *Session, err error) {
167-
options := emulator.ClientOptions()
151+
func initializeSession(ctx context.Context, clients *spanemuboost.Clients) (session *Session, err error) {
152+
options := clients.ClientOptions()
168153
sysVars := &systemVariables{
169154
Connection: ConnectionVars{
170155
Project: clients.ProjectID,
@@ -232,10 +217,10 @@ func initializeWithDB(t *testing.T, database string, ddls, dmls []string) (clien
232217
spanemuboost.WithSetupRawDMLs(dmls),
233218
)
234219

235-
clients = spanemuboost.SetupClients(t, emulator, options...)
220+
clients = spanemuboost.SetupClients(t, lazyEmu, options...)
236221

237222
var err error
238-
session, err = initializeSession(ctx, emulator, clients)
223+
session, err = initializeSession(ctx, clients)
239224
if err != nil {
240225
t.Fatalf("failed to create test session: err=%s", err)
241226
}
@@ -254,7 +239,7 @@ func initializeAdminSession(t *testing.T) (clients *spanemuboost.Clients, sessio
254239

255240
// Admin-only mode: create instance without database
256241
// Always use instance-level isolation for all tests
257-
clients = spanemuboost.SetupClients(t, emulator,
242+
clients = spanemuboost.SetupClients(t, lazyEmu,
258243
spanemuboost.WithRandomInstanceID(), // Instance-level isolation for all tests
259244
spanemuboost.EnableInstanceAutoConfigOnly(),
260245
)
@@ -284,7 +269,7 @@ func initializeAdminSession(t *testing.T) (clients *spanemuboost.Clients, sessio
284269
sysVars.ensureRegistry()
285270

286271
var err error
287-
session, err = NewAdminSession(ctx, sysVars, emulator.ClientOptions()...)
272+
session, err = NewAdminSession(ctx, sysVars, clients.ClientOptions()...)
288273
if err != nil {
289274
t.Fatalf("failed to create admin session: err=%s", err)
290275
}

internal/mycli/session_slow_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ func TestRequestPriority(t *testing.T) {
3535
ctx := t.Context()
3636

3737
// Use shared emulator with a random instance ID for isolation
38-
clients := spanemuboost.SetupClients(t, emulator,
38+
clients := spanemuboost.SetupClients(t, lazyEmu,
3939
spanemuboost.WithRandomInstanceID(),
4040
spanemuboost.WithProjectID(project),
4141
spanemuboost.WithDatabaseID(database),
4242
spanemuboost.EnableAutoConfig(),
4343
spanemuboost.WithSetupDDLs(sliceOf("CREATE TABLE t1 (Id INT64) PRIMARY KEY (Id)")),
4444
)
45-
_ = clients // clients not directly used, but ensures proper setup
46-
4745
var recorder requestRecorder
4846
unaryInterceptor, streamInterceptor := recordRequestsInterceptors(&recorder)
4947
opts := []grpc.DialOption{
@@ -52,7 +50,7 @@ func TestRequestPriority(t *testing.T) {
5250
grpc.WithStreamInterceptor(streamInterceptor),
5351
}
5452

55-
conn, err := grpc.NewClient(emulator.URI(), opts...)
53+
conn, err := grpc.NewClient(clients.URI(), opts...)
5654
if err != nil {
5755
t.Fatalf("failed to dial: %v", err)
5856
}
@@ -164,15 +162,13 @@ func TestIsolationLevel(t *testing.T) {
164162
ctx := t.Context()
165163

166164
// Use shared emulator with a random instance ID for isolation
167-
clients := spanemuboost.SetupClients(t, emulator,
165+
clients := spanemuboost.SetupClients(t, lazyEmu,
168166
spanemuboost.WithRandomInstanceID(),
169167
spanemuboost.WithProjectID(project),
170168
spanemuboost.WithDatabaseID(database),
171169
spanemuboost.EnableAutoConfig(),
172170
spanemuboost.WithSetupDDLs(sliceOf("CREATE TABLE t1 (Id INT64) PRIMARY KEY (Id)")),
173171
)
174-
_ = clients // clients not directly used, but ensures proper setup
175-
176172
var recorder requestRecorder
177173
unaryInterceptor, streamInterceptor := recordRequestsInterceptors(&recorder)
178174
opts := []grpc.DialOption{
@@ -181,7 +177,7 @@ func TestIsolationLevel(t *testing.T) {
181177
grpc.WithStreamInterceptor(streamInterceptor),
182178
}
183179

184-
conn, err := grpc.NewClient(emulator.URI(), opts...)
180+
conn, err := grpc.NewClient(clients.URI(), opts...)
185181
if err != nil {
186182
t.Fatalf("failed to dial: %v", err)
187183
}

0 commit comments

Comments
 (0)