|
| 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 internal |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + btopt "cloud.google.com/go/bigtable/internal/option" |
| 23 | +) |
| 24 | + |
| 25 | +func TestConnectionRecycler_CheckRecycle(t *testing.T) { |
| 26 | + fake := &fakeService{} |
| 27 | + addr := setupTestServer(t, fake) |
| 28 | + dialFunc := func() (*BigtableConn, error) { return dialBigtableserver(addr) } |
| 29 | + ctx := context.Background() |
| 30 | + |
| 31 | + setAge := func(entry *connEntry, age time.Duration) { |
| 32 | + entry.conn.createdAt.Store(time.Now().Add(-age).UnixMilli()) |
| 33 | + } |
| 34 | + |
| 35 | + t.Run("RecycleOldConnection", func(t *testing.T) { |
| 36 | + config := btopt.ConnectionRecycleConfig{ |
| 37 | + MaxAge: 10 * time.Minute, |
| 38 | + MaxJitter: 0, |
| 39 | + } |
| 40 | + |
| 41 | + pool, err := NewBigtableChannelPool(ctx, 1, btopt.RoundRobin, dialFunc, time.Now()) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Failed to create pool: %v", err) |
| 44 | + } |
| 45 | + defer pool.Close() |
| 46 | + |
| 47 | + recycler := NewConnectionRecycler(config, pool) |
| 48 | + |
| 49 | + conns := pool.getConns() |
| 50 | + if len(conns) != 1 { |
| 51 | + t.Fatalf("Expected 1 connection, got %d", len(conns)) |
| 52 | + } |
| 53 | + originalEntry := conns[0] |
| 54 | + originalConnPtr := originalEntry.conn |
| 55 | + |
| 56 | + // maxAge > 20m |
| 57 | + setAge(originalEntry, 20*time.Minute) |
| 58 | + recycler.checkRecycle() |
| 59 | + |
| 60 | + // recycled fast as it does not have any pending rpcs |
| 61 | + newConns := pool.getConns() |
| 62 | + if newConns[0].conn == originalConnPtr { |
| 63 | + t.Error("Connection was older than MaxAge but was NOT recycled") |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("DoesNotReplaceIfConnWithinMaxAge", func(t *testing.T) { |
| 68 | + config := btopt.ConnectionRecycleConfig{ |
| 69 | + MaxAge: 10 * time.Minute, |
| 70 | + MaxJitter: 0, |
| 71 | + } |
| 72 | + |
| 73 | + pool, err := NewBigtableChannelPool(ctx, 1, btopt.RoundRobin, dialFunc, time.Now()) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("Failed to create pool: %v", err) |
| 76 | + } |
| 77 | + defer pool.Close() |
| 78 | + |
| 79 | + recycler := NewConnectionRecycler(config, pool) |
| 80 | + |
| 81 | + entry := pool.getConns()[0] |
| 82 | + originalConnPtr := entry.conn |
| 83 | + |
| 84 | + // < 10mins |
| 85 | + setAge(entry, 5*time.Minute) |
| 86 | + |
| 87 | + // recycled fast as it does not have any pending rpcs |
| 88 | + recycler.checkRecycle() |
| 89 | + |
| 90 | + if pool.getConns()[0].conn != originalConnPtr { |
| 91 | + t.Error("Connection WAS recycled unexpectedly") |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("RespectsMaxRecyclePerBatch", func(t *testing.T) { |
| 96 | + config := btopt.ConnectionRecycleConfig{ |
| 97 | + MaxAge: 10 * time.Minute, |
| 98 | + MaxJitter: 0, |
| 99 | + } |
| 100 | + // 5 conns |
| 101 | + poolSize := 5 |
| 102 | + pool, err := NewBigtableChannelPool(ctx, poolSize, btopt.RoundRobin, dialFunc, time.Now()) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to create pool: %v", err) |
| 105 | + } |
| 106 | + defer pool.Close() |
| 107 | + |
| 108 | + recycler := NewConnectionRecycler(config, pool) |
| 109 | + |
| 110 | + // force age to be old |
| 111 | + conns := pool.getConns() |
| 112 | + originalConns := make(map[*BigtableConn]bool) |
| 113 | + for _, e := range conns { |
| 114 | + setAge(e, 60*time.Minute) |
| 115 | + originalConns[e.conn] = true |
| 116 | + } |
| 117 | + |
| 118 | + // Trigger recycle |
| 119 | + recycler.checkRecycle() |
| 120 | + |
| 121 | + currentConns := pool.getConns() |
| 122 | + changedCount := 0 |
| 123 | + for _, e := range currentConns { |
| 124 | + if !originalConns[e.conn] { |
| 125 | + changedCount++ |
| 126 | + } |
| 127 | + } |
| 128 | + if changedCount != maxRecyclePerBatch { |
| 129 | + t.Errorf("Expected exactly %d recycled connections (batch limit), but got %d", maxRecyclePerBatch, changedCount) |
| 130 | + } |
| 131 | + }) |
| 132 | +} |
0 commit comments