|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package bigtable |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + btpb "cloud.google.com/go/bigtable/apiv2/bigtablepb" |
| 24 | + "google.golang.org/api/option" |
| 25 | + "google.golang.org/grpc" |
| 26 | + "google.golang.org/grpc/credentials/insecure" |
| 27 | + "google.golang.org/grpc/test/bufconn" |
| 28 | +) |
| 29 | + |
| 30 | +// TestClientConfig_DisableSession_SkipsSessionInit pins the |
| 31 | +// DisableSession=true gate: NewClientWithConfig must NOT construct |
| 32 | +// the session data-plane client, and every downstream sessionImpl / |
| 33 | +// sessionTables field must be nil. |
| 34 | +// |
| 35 | +// The test uses WithContextDialer (not WithGRPCConn) so preDialed |
| 36 | +// stays false — otherwise the preDialed path would also skip |
| 37 | +// session.NewClient and the test would not isolate the DisableSession |
| 38 | +// gate. With DisableSession=true AND preDialed=false, only the |
| 39 | +// DisableSession gate can be responsible for sessionImpl being nil. |
| 40 | +// |
| 41 | +// The bufconn has no server behind it. If the gate is broken, |
| 42 | +// session.NewClient would try to reach a non-responsive backend and |
| 43 | +// the 5-second context would time out — the test would fail with a |
| 44 | +// deadline error, distinguishing "gate broken" from "gate worked." |
| 45 | +func TestClientConfig_DisableSession_SkipsSessionInit(t *testing.T) { |
| 46 | + lis := bufconn.Listen(1024 * 1024) |
| 47 | + t.Cleanup(func() { _ = lis.Close() }) |
| 48 | + |
| 49 | + // Serve a minimal fake so the classic pool's Prime (PingAndWarm) |
| 50 | + // succeeds; without it, mPool never finishes construction and this |
| 51 | + // test would fail on the classic side, not the DisableSession gate. |
| 52 | + grpcSrv := grpc.NewServer() |
| 53 | + t.Cleanup(grpcSrv.Stop) |
| 54 | + btpb.RegisterBigtableServer(grpcSrv, newFakeBigtableServer(t)) |
| 55 | + go func() { _ = grpcSrv.Serve(lis) }() |
| 56 | + |
| 57 | + dialer := func(ctx context.Context, _ string) (net.Conn, error) { |
| 58 | + return lis.DialContext(ctx) |
| 59 | + } |
| 60 | + |
| 61 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 62 | + defer cancel() |
| 63 | + |
| 64 | + c, err := NewClientWithConfig(ctx, "test-project", "test-instance", ClientConfig{ |
| 65 | + DisableSession: true, |
| 66 | + MetricsProvider: NoopMetricsProvider{}, |
| 67 | + }, |
| 68 | + option.WithEndpoint("passthrough:///bufnet"), |
| 69 | + option.WithGRPCDialOption(grpc.WithContextDialer(dialer)), |
| 70 | + option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 71 | + option.WithoutAuthentication(), |
| 72 | + ) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("NewClientWithConfig with DisableSession=true failed: %v", err) |
| 75 | + } |
| 76 | + t.Cleanup(func() { _ = c.Close() }) |
| 77 | + |
| 78 | + if c.sessionImpl != nil { |
| 79 | + t.Errorf("sessionImpl = %T, want nil (DisableSession=true must skip session.NewClient)", c.sessionImpl) |
| 80 | + } |
| 81 | + if c.sessionTables != nil { |
| 82 | + t.Errorf("sessionTables = %v, want nil (DisableSession=true must skip cache init)", c.sessionTables) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// TestClientConfig_DisableSession_ClientCloseWorks confirms Client.Close |
| 87 | +// on a DisableSession client is a clean no-op on the session side — |
| 88 | +// every downstream nil-guard fires as intended (client.go:322 comment |
| 89 | +// promises "sessionTables is nil ... the cache's own close() nil-checks |
| 90 | +// for that"; client.go:329 wraps sessionImpl.Close in a nil check). |
| 91 | +func TestClientConfig_DisableSession_ClientCloseWorks(t *testing.T) { |
| 92 | + lis := bufconn.Listen(1024 * 1024) |
| 93 | + t.Cleanup(func() { _ = lis.Close() }) |
| 94 | + |
| 95 | + // Serve a minimal fake so the classic pool's Prime (PingAndWarm) |
| 96 | + // succeeds; without it, mPool never finishes construction and this |
| 97 | + // test would fail on the classic side, not the DisableSession gate. |
| 98 | + grpcSrv := grpc.NewServer() |
| 99 | + t.Cleanup(grpcSrv.Stop) |
| 100 | + btpb.RegisterBigtableServer(grpcSrv, newFakeBigtableServer(t)) |
| 101 | + go func() { _ = grpcSrv.Serve(lis) }() |
| 102 | + |
| 103 | + dialer := func(ctx context.Context, _ string) (net.Conn, error) { |
| 104 | + return lis.DialContext(ctx) |
| 105 | + } |
| 106 | + |
| 107 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 108 | + defer cancel() |
| 109 | + |
| 110 | + c, err := NewClientWithConfig(ctx, "test-project", "test-instance", ClientConfig{ |
| 111 | + DisableSession: true, |
| 112 | + MetricsProvider: NoopMetricsProvider{}, |
| 113 | + }, |
| 114 | + option.WithEndpoint("passthrough:///bufnet"), |
| 115 | + option.WithGRPCDialOption(grpc.WithContextDialer(dialer)), |
| 116 | + option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 117 | + option.WithoutAuthentication(), |
| 118 | + ) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("NewClientWithConfig: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Explicit Close — must not panic on the nil sessionImpl / sessionTables. |
| 124 | + if err := c.Close(); err != nil { |
| 125 | + t.Errorf("Close on DisableSession client returned err = %v, want nil", err) |
| 126 | + } |
| 127 | + // Second Close must also be safe (idempotent per session_pool_lifecycle |
| 128 | + // / mPool.Close conventions). |
| 129 | + _ = c.Close() |
| 130 | +} |
| 131 | + |
| 132 | +// TestClientConfig_DisableSession_OpenTableRoutesClassicOnly confirms |
| 133 | +// that on a DisableSession client, TableShim's session side is nil, so |
| 134 | +// useSession() reports false and every Apply / ReadRow routes to the |
| 135 | +// classic path unconditionally — regardless of the Diverter's |
| 136 | +// SessionLoad. This is the load-bearing contract for callers that |
| 137 | +// opt out: they get classic behavior even if some future code path |
| 138 | +// bumps the Diverter. |
| 139 | +func TestClientConfig_DisableSession_OpenTableRoutesClassicOnly(t *testing.T) { |
| 140 | + lis := bufconn.Listen(1024 * 1024) |
| 141 | + t.Cleanup(func() { _ = lis.Close() }) |
| 142 | + |
| 143 | + // Serve a minimal fake so the classic pool's Prime (PingAndWarm) |
| 144 | + // succeeds; without it, mPool never finishes construction and this |
| 145 | + // test would fail on the classic side, not the DisableSession gate. |
| 146 | + grpcSrv := grpc.NewServer() |
| 147 | + t.Cleanup(grpcSrv.Stop) |
| 148 | + btpb.RegisterBigtableServer(grpcSrv, newFakeBigtableServer(t)) |
| 149 | + go func() { _ = grpcSrv.Serve(lis) }() |
| 150 | + |
| 151 | + dialer := func(ctx context.Context, _ string) (net.Conn, error) { |
| 152 | + return lis.DialContext(ctx) |
| 153 | + } |
| 154 | + |
| 155 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 156 | + defer cancel() |
| 157 | + |
| 158 | + c, err := NewClientWithConfig(ctx, "test-project", "test-instance", ClientConfig{ |
| 159 | + DisableSession: true, |
| 160 | + MetricsProvider: NoopMetricsProvider{}, |
| 161 | + }, |
| 162 | + option.WithEndpoint("passthrough:///bufnet"), |
| 163 | + option.WithGRPCDialOption(grpc.WithContextDialer(dialer)), |
| 164 | + option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 165 | + option.WithoutAuthentication(), |
| 166 | + ) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("NewClientWithConfig: %v", err) |
| 169 | + } |
| 170 | + t.Cleanup(func() { _ = c.Close() }) |
| 171 | + |
| 172 | + // Force the Diverter into session-preferring mode. If DisableSession |
| 173 | + // works as advertised, useSession() still reports false because |
| 174 | + // TableShim.session is nil. |
| 175 | + if c.diverter != nil { |
| 176 | + c.diverter.SetSessionLoad(1.0) |
| 177 | + } |
| 178 | + |
| 179 | + tblAPI := c.OpenTable("mytable") |
| 180 | + shim, ok := tblAPI.(*TableShim) |
| 181 | + if !ok { |
| 182 | + t.Fatalf("OpenTable returned %T, want *TableShim", tblAPI) |
| 183 | + } |
| 184 | + if shim.session != nil { |
| 185 | + t.Errorf("shim.session = %T, want nil (DisableSession clients must not wire a session TableAPI)", shim.session) |
| 186 | + } |
| 187 | + if shim.useSession() { |
| 188 | + t.Error("shim.useSession() = true, want false (nil session side must gate to classic)") |
| 189 | + } |
| 190 | +} |
0 commit comments