|
| 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 atepg |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "net" |
| 22 | + "net/url" |
| 23 | + "sync" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +// connectForPing connects to the shared container with a fresh schema that is |
| 29 | +// dropped when the test ends. The returned config lets a test adjust the DSN |
| 30 | +// or pool size before connecting. |
| 31 | +func connectForPing(t *testing.T, schema string, adjust func(*ConnectConfig)) *Persistence { |
| 32 | + t.Helper() |
| 33 | + admin := requirePool(t) |
| 34 | + ctx := t.Context() |
| 35 | + drop := fmt.Sprintf(`DROP SCHEMA IF EXISTS %q CASCADE`, schema) |
| 36 | + if _, err := admin.Exec(ctx, drop); err != nil { |
| 37 | + t.Fatalf("resetting PostgreSQL schema: %v", err) |
| 38 | + } |
| 39 | + t.Cleanup(func() { _, _ = admin.Exec(context.Background(), drop) }) |
| 40 | + |
| 41 | + cfg := testConnectConfig(schema) |
| 42 | + adjust(&cfg) |
| 43 | + p, err := Connect(ctx, cfg) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("Connect failed: %v", err) |
| 46 | + } |
| 47 | + t.Cleanup(func() { |
| 48 | + p.Close() |
| 49 | + p.pool.Close() |
| 50 | + }) |
| 51 | + return p |
| 52 | +} |
| 53 | + |
| 54 | +// A saturated request pool must not fail the readiness probe: the probe cannot |
| 55 | +// tell a busy replica from an unreachable store if it borrows from that pool. |
| 56 | +func TestPingIgnoresSaturatedRequestPool(t *testing.T) { |
| 57 | + p := connectForPing(t, "ping-saturated", func(cfg *ConnectConfig) { cfg.PoolMaxConns = 1 }) |
| 58 | + ctx := t.Context() |
| 59 | + |
| 60 | + if p.probePool == p.pool { |
| 61 | + t.Fatal("probe pool is the request pool") |
| 62 | + } |
| 63 | + if got := p.probePool.Config().MaxConns; got != probePoolMaxConns { |
| 64 | + t.Errorf("probe pool MaxConns = %d, want %d", got, probePoolMaxConns) |
| 65 | + } |
| 66 | + if got := p.probePool.Config().MinConns; got != probePoolMinConns { |
| 67 | + t.Errorf("probe pool MinConns = %d, want %d", got, probePoolMinConns) |
| 68 | + } |
| 69 | + |
| 70 | + held, err := p.pool.Acquire(ctx) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("acquiring the only request connection: %v", err) |
| 73 | + } |
| 74 | + defer held.Release() |
| 75 | + |
| 76 | + budget := func() (context.Context, context.CancelFunc) { |
| 77 | + return context.WithTimeout(ctx, 500*time.Millisecond) |
| 78 | + } |
| 79 | + |
| 80 | + // The premise: the request pool really is exhausted, and a ping through it |
| 81 | + // blocks past the readiness budget. |
| 82 | + poolCtx, cancel := budget() |
| 83 | + defer cancel() |
| 84 | + if err := p.pool.Ping(poolCtx); err == nil { |
| 85 | + t.Fatal("request pool ping succeeded while every connection was held; the pool is not saturated") |
| 86 | + } |
| 87 | + |
| 88 | + probeCtx, cancel := budget() |
| 89 | + defer cancel() |
| 90 | + if err := p.Ping(probeCtx); err != nil { |
| 91 | + t.Errorf("Ping with a saturated request pool = %v, want nil", err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestPingFailsAfterClose(t *testing.T) { |
| 96 | + p := connectForPing(t, "ping-closed", func(*ConnectConfig) {}) |
| 97 | + if err := p.Ping(t.Context()); err != nil { |
| 98 | + t.Fatalf("Ping before Close = %v, want nil", err) |
| 99 | + } |
| 100 | + p.Close() |
| 101 | + if err := p.Ping(t.Context()); err == nil { |
| 102 | + t.Error("Ping after Close succeeded, want an error from the closed probe pool") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Ping fails while PostgreSQL does not answer and passes again once it does, |
| 107 | +// with no restart of the pool. |
| 108 | +func TestPingFollowsStoreReachability(t *testing.T) { |
| 109 | + requirePool(t) |
| 110 | + dsn, err := url.Parse(containerDSN) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("parsing PostgreSQL DSN: %v", err) |
| 113 | + } |
| 114 | + proxy := newTCPProxy(t, dsn.Host) |
| 115 | + dsn.Host = proxy.addr |
| 116 | + p := connectForPing(t, "ping-reachability", func(cfg *ConnectConfig) { cfg.ReadWriteDSN = dsn.String() }) |
| 117 | + |
| 118 | + ping := func() error { |
| 119 | + ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second) |
| 120 | + defer cancel() |
| 121 | + return p.Ping(ctx) |
| 122 | + } |
| 123 | + if err := ping(); err != nil { |
| 124 | + t.Fatalf("Ping with the store reachable = %v, want nil", err) |
| 125 | + } |
| 126 | + proxy.stop() |
| 127 | + if err := ping(); err == nil { |
| 128 | + t.Error("Ping with the store unreachable succeeded, want an error") |
| 129 | + } |
| 130 | + proxy.start() |
| 131 | + if err := ping(); err != nil { |
| 132 | + t.Errorf("Ping after the store became reachable = %v, want nil", err) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// tcpProxy forwards TCP connections to target until stopped, and start brings |
| 137 | +// it back on the same address. |
| 138 | +type tcpProxy struct { |
| 139 | + t *testing.T |
| 140 | + target string |
| 141 | + addr string |
| 142 | + |
| 143 | + mu sync.Mutex |
| 144 | + ln net.Listener |
| 145 | + conns []net.Conn |
| 146 | +} |
| 147 | + |
| 148 | +func newTCPProxy(t *testing.T, target string) *tcpProxy { |
| 149 | + t.Helper() |
| 150 | + p := &tcpProxy{t: t, target: target, addr: "127.0.0.1:0"} |
| 151 | + p.start() |
| 152 | + t.Cleanup(p.stop) |
| 153 | + return p |
| 154 | +} |
| 155 | + |
| 156 | +func (p *tcpProxy) start() { |
| 157 | + p.t.Helper() |
| 158 | + ln, err := net.Listen("tcp", p.addr) |
| 159 | + if err != nil { |
| 160 | + p.t.Fatalf("listening on %s: %v", p.addr, err) |
| 161 | + } |
| 162 | + p.mu.Lock() |
| 163 | + p.ln = ln |
| 164 | + p.addr = ln.Addr().String() |
| 165 | + p.mu.Unlock() |
| 166 | + go func() { |
| 167 | + for { |
| 168 | + client, err := ln.Accept() |
| 169 | + if err != nil { |
| 170 | + return |
| 171 | + } |
| 172 | + go p.forward(client) |
| 173 | + } |
| 174 | + }() |
| 175 | +} |
| 176 | + |
| 177 | +func (p *tcpProxy) forward(client net.Conn) { |
| 178 | + upstream, err := net.Dial("tcp", p.target) |
| 179 | + if err != nil { |
| 180 | + client.Close() |
| 181 | + return |
| 182 | + } |
| 183 | + p.mu.Lock() |
| 184 | + p.conns = append(p.conns, client, upstream) |
| 185 | + p.mu.Unlock() |
| 186 | + go func() { |
| 187 | + io.Copy(upstream, client) //nolint:errcheck // Either side closing ends the copy. |
| 188 | + upstream.Close() |
| 189 | + }() |
| 190 | + io.Copy(client, upstream) //nolint:errcheck // Either side closing ends the copy. |
| 191 | + client.Close() |
| 192 | +} |
| 193 | + |
| 194 | +// stop refuses new connections and drops the open ones. |
| 195 | +func (p *tcpProxy) stop() { |
| 196 | + p.mu.Lock() |
| 197 | + defer p.mu.Unlock() |
| 198 | + if p.ln != nil { |
| 199 | + p.ln.Close() |
| 200 | + p.ln = nil |
| 201 | + } |
| 202 | + for _, c := range p.conns { |
| 203 | + c.Close() |
| 204 | + } |
| 205 | + p.conns = nil |
| 206 | +} |
0 commit comments