|
| 1 | +// Copyright 2025 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 | +// https://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 api |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "cloud.google.com/go/spanner" |
| 24 | + "cloud.google.com/go/spanner/apiv1/spannerpb" |
| 25 | + "github.com/googleapis/go-sql-spanner/testutil" |
| 26 | + "google.golang.org/grpc/codes" |
| 27 | +) |
| 28 | + |
| 29 | +func TestCreateAndCloseConnection(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + ctx := context.Background() |
| 33 | + server, teardown := setupMockServer(t) |
| 34 | + defer teardown() |
| 35 | + dsn := fmt.Sprintf("%s/projects/p/instances/i/databases/d?useplaintext=true", server.Address) |
| 36 | + |
| 37 | + poolId, err := CreatePool(ctx, dsn) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("CreatePool returned unexpected error: %v", err) |
| 40 | + } |
| 41 | + connId, err := CreateConnection(ctx, poolId) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("CreateConnection returned unexpected error: %v", err) |
| 44 | + } |
| 45 | + if connId == 0 { |
| 46 | + t.Fatal("CreateConnection returned unexpected zero id") |
| 47 | + } |
| 48 | + p, ok := pools.Load(poolId) |
| 49 | + if !ok { |
| 50 | + t.Fatal("pool not found in map") |
| 51 | + } |
| 52 | + pool := p.(*Pool) |
| 53 | + if _, ok := pool.connections.Load(connId); !ok { |
| 54 | + t.Fatal("connection not in map") |
| 55 | + } |
| 56 | + |
| 57 | + requests := server.TestSpanner.DrainRequestsFromServer() |
| 58 | + createSessionRequests := testutil.RequestsOfType(requests, reflect.TypeOf(&spannerpb.CreateSessionRequest{})) |
| 59 | + if g, w := len(createSessionRequests), 1; g != w { |
| 60 | + t.Fatalf("num CreateSession requests mismatch\n Got: %d\nWant: %d", g, w) |
| 61 | + } |
| 62 | + |
| 63 | + if err := CloseConnection(ctx, poolId, connId); err != nil { |
| 64 | + t.Fatalf("CloseConnection returned unexpected error: %v", err) |
| 65 | + } |
| 66 | + if _, ok := pool.connections.Load(connId); ok { |
| 67 | + t.Fatal("connection still in map") |
| 68 | + } |
| 69 | + |
| 70 | + if err := ClosePool(ctx, poolId); err != nil { |
| 71 | + t.Fatalf("ClosePool returned unexpected error: %v", err) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestCreateConnectionWithUnknownPool(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + |
| 78 | + ctx := context.Background() |
| 79 | + // Try to create a connection for an unknown pool. |
| 80 | + _, err := CreateConnection(ctx, -1) |
| 81 | + if g, w := spanner.ErrCode(err), codes.NotFound; g != w { |
| 82 | + t.Fatalf("error code mismatch\n Got: %d\nWant: %d", g, w) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestCreateTwoConnections(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + |
| 89 | + ctx := context.Background() |
| 90 | + server, teardown := setupMockServer(t) |
| 91 | + defer teardown() |
| 92 | + dsn := fmt.Sprintf("%s/projects/p/instances/i/databases/d?useplaintext=true", server.Address) |
| 93 | + |
| 94 | + poolId, err := CreatePool(ctx, dsn) |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("CreatePool returned unexpected error: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + for range 2 { |
| 100 | + connId, err := CreateConnection(ctx, poolId) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("CreateConnection returned unexpected error: %v", err) |
| 103 | + } |
| 104 | + //goland:noinspection GoDeferInLoop |
| 105 | + defer func() { _ = CloseConnection(ctx, poolId, connId) }() |
| 106 | + } |
| 107 | + |
| 108 | + // Two connections in one pool should only create one multiplexed session. |
| 109 | + requests := server.TestSpanner.DrainRequestsFromServer() |
| 110 | + createSessionRequests := testutil.RequestsOfType(requests, reflect.TypeOf(&spannerpb.CreateSessionRequest{})) |
| 111 | + if g, w := len(createSessionRequests), 1; g != w { |
| 112 | + t.Fatalf("num CreateSession requests mismatch\n Got: %d\nWant: %d", g, w) |
| 113 | + } |
| 114 | + |
| 115 | + if err := ClosePool(ctx, poolId); err != nil { |
| 116 | + t.Fatalf("ClosePool returned unexpected error: %v", err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestCloseConnectionTwice(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + |
| 123 | + ctx := context.Background() |
| 124 | + server, teardown := setupMockServer(t) |
| 125 | + defer teardown() |
| 126 | + dsn := fmt.Sprintf("%s/projects/p/instances/i/databases/d?useplaintext=true", server.Address) |
| 127 | + |
| 128 | + poolId, err := CreatePool(ctx, dsn) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("CreatePool returned unexpected error: %v", err) |
| 131 | + } |
| 132 | + connId, err := CreateConnection(ctx, poolId) |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("CreateConnection returned unexpected error: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + for range 2 { |
| 138 | + if err := CloseConnection(ctx, poolId, connId); err != nil { |
| 139 | + t.Fatalf("CloseConnection returned unexpected error: %v", err) |
| 140 | + } |
| 141 | + } |
| 142 | + if err := ClosePool(ctx, poolId); err != nil { |
| 143 | + t.Fatalf("ClosePool returned unexpected error: %v", err) |
| 144 | + } |
| 145 | +} |
0 commit comments