1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package topology
import (
"context"
"errors"
"net"
"testing"
"time"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
)
func TestCMAPProse(t *testing.T) {
t.Run("created and closed events", func(t *testing.T) {
created := make(chan *event.PoolEvent, 10)
closed := make(chan *event.PoolEvent, 10)
clearEvents := func() {
for len(created) > 0 {
<-created
}
for len(closed) > 0 {
<-closed
}
}
monitor := &event.PoolMonitor{
Event: func(evt *event.PoolEvent) {
switch evt.Type {
case event.ConnectionCreated:
created <- evt
case event.ConnectionClosed:
closed <- evt
}
},
}
getConfig := func() poolConfig {
return poolConfig{
PoolMonitor: monitor,
}
}
assertConnectionCounts := func(t *testing.T, p *pool, numCreated, numClosed int) {
t.Helper()
assert.Equal(t, numCreated, len(created), "expected %d creation events, got %d", numCreated, len(created))
assert.Equal(t, numClosed, len(closed), "expected %d closed events, got %d", numClosed, len(closed))
netCount := numCreated - numClosed
assert.Equal(t, netCount, p.totalConnectionCount(), "expected %d total connections, got %d", netCount,
p.totalConnectionCount())
}
t.Run("maintain", func(t *testing.T) {
t.Run("connection error publishes events", func(t *testing.T) {
// If a connection is created as part of minPoolSize maintenance and errors while connecting, checkOut()
// should report that error and publish an event.
// If maintain() creates a connection that encounters an error while connecting,
// the pool should publish connection created and closed events.
clearEvents()
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{writeerr: errors.New("write error")}, nil
}
cfg := getConfig()
cfg.MinPoolSize = 1
connOpts := []ConnectionOption{
WithDialer(func(Dialer) Dialer { return dialer }),
WithHandshaker(func(Handshaker) Handshaker {
return operation.NewHello()
}),
}
_, disconnect := createTestPool(t, cfg, connOpts...)
defer disconnect()
// Wait up to 3 seconds for the maintain() goroutine to run and for 1 connection
// created and 1 connection closed events to be published.
start := time.Now()
for len(created) != 1 || len(closed) != 1 {
if time.Since(start) > 3*time.Second {
t.Errorf(
"Expected 1 connection created and 1 connection closed events within 3 seconds. "+
"Actual created events: %d, actual closed events: %d",
len(created),
len(closed))
}
time.Sleep(time.Millisecond)
}
})
})
t.Run("checkOut", func(t *testing.T) {
t.Run("connection error publishes events", func(t *testing.T) {
// If checkOut() creates a connection that encounters an error while connecting,
// the pool should publish connection created and closed events and checkOut should
// return the error.
clearEvents()
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{writeerr: errors.New("write error")}, nil
}
cfg := getConfig()
connOpts := []ConnectionOption{
WithDialer(func(Dialer) Dialer { return dialer }),
WithHandshaker(func(Handshaker) Handshaker {
return operation.NewHello()
}),
}
pool, disconnect := createTestPool(t, cfg, connOpts...)
defer disconnect()
_, err := pool.checkOut(context.Background())
assert.NotNil(t, err, "expected checkOut() error, got nil")
assert.Equal(t, 1, len(created), "expected 1 opened events, got %d", len(created))
assert.Equal(t, 1, len(closed), "expected 1 closed events, got %d", len(closed))
})
t.Run("pool is empty", func(t *testing.T) {
// If a checkOut() has to create a new connection and that connection encounters an
// error while connecting, checkOut() should return that error and publish an event.
clearEvents()
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{writeerr: errors.New("write error")}, nil
}
connOpts := []ConnectionOption{
WithDialer(func(Dialer) Dialer { return dialer }),
WithHandshaker(func(Handshaker) Handshaker {
return operation.NewHello()
}),
}
pool, disconnect := createTestPool(t, getConfig(), connOpts...)
defer disconnect()
_, err := pool.checkOut(context.Background())
assert.NotNil(t, err, "expected checkOut() error, got nil")
assertConnectionCounts(t, pool, 1, 1)
})
})
t.Run("checkIn", func(t *testing.T) {
t.Run("errored connection", func(t *testing.T) {
// If the connection being returned to the pool encountered a network error, it should be removed from
// the pool and an event should be published.
clearEvents()
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{writeerr: errors.New("write error")}, nil
}
// We don't use the WithHandshaker option so the connection won't error during handshaking.
connOpts := []ConnectionOption{
WithDialer(func(Dialer) Dialer { return dialer }),
}
pool, disconnect := createTestPool(t, getConfig(), connOpts...)
defer disconnect()
conn, err := pool.checkOut(context.Background())
assert.Nil(t, err, "checkOut() error: %v", err)
// Force a network error by writing to the connection.
err = conn.writeWireMessage(context.Background(), nil)
assert.NotNil(t, err, "expected writeWireMessage error, got nil")
err = pool.checkIn(conn)
assert.Nil(t, err, "checkIn() error: %v", err)
assertConnectionCounts(t, pool, 1, 1)
evt := <-closed
assert.Equal(t, event.ReasonConnectionErrored, evt.Reason, "expected reason %q, got %q",
event.ReasonConnectionErrored, evt.Reason)
})
t.Run("expired connection", func(t *testing.T) {
// If the connection being returned to the pool is expired, it should be removed from the pool and an
// event should be published.
clearEvents()
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{}, nil
}
// We don't use the WithHandshaker option so the connection won't error during handshaking.
// WithIdleTimeout must be used because the connection.idleTimeoutExpired() function only checks the
// deadline if the idleTimeout option is greater than 0.
connOpts := []ConnectionOption{
WithDialer(func(Dialer) Dialer { return dialer }),
WithIdleTimeout(func(time.Duration) time.Duration { return 1 * time.Second }),
}
pool, disconnect := createTestPool(t, getConfig(), connOpts...)
defer disconnect()
conn, err := pool.checkOut(context.Background())
assert.Nil(t, err, "checkOut() error: %v", err)
// Set the idleDeadline to a time in the past to simulate expiration.
pastTime := time.Now().Add(-10 * time.Second)
conn.idleDeadline.Store(pastTime)
err = pool.checkIn(conn)
assert.Nil(t, err, "checkIn() error: %v", err)
assertConnectionCounts(t, pool, 1, 1)
evt := <-closed
assert.Equal(t, event.ReasonIdle, evt.Reason, "expected reason %q, got %q",
event.ReasonIdle, evt.Reason)
})
})
t.Run("disconnect", func(t *testing.T) {
t.Run("connections returned gracefully", func(t *testing.T) {
// If all connections are in the pool when disconnect is called, they should be closed gracefully and
// events should be published.
clearEvents()
numConns := 5
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{}, nil
}
pool, disconnect := createTestPool(t, getConfig(), WithDialer(func(Dialer) Dialer { return dialer }))
defer disconnect()
conns := checkoutConnections(t, pool, numConns)
assertConnectionCounts(t, pool, numConns, 0)
// Return all connections to the pool and assert that none were closed by checkIn().
for i, c := range conns {
err := pool.checkIn(c)
assert.Nil(t, err, "checkIn() error at index %d: %v", i, err)
}
assertConnectionCounts(t, pool, numConns, 0)
// Disconnect the pool and assert that a closed event is published for each connection.
err := pool.disconnect(context.Background())
assert.Nil(t, err, "disconnect error: %v", err)
assertConnectionCounts(t, pool, numConns, numConns)
for len(closed) > 0 {
evt := <-closed
assert.Equal(t, event.ReasonPoolClosed, evt.Reason, "expected reason %q, got %q",
event.ReasonPoolClosed, evt.Reason)
}
})
t.Run("connections closed forcefully", func(t *testing.T) {
// If some connections are still checked out when disconnect is called, they should be closed
// forcefully and events should be published for them.
clearEvents()
numConns := 5
var dialer DialerFunc = func(context.Context, string, string) (net.Conn, error) {
return &testNetConn{}, nil
}
pool, _ := createTestPool(t, getConfig(), WithDialer(func(Dialer) Dialer { return dialer }))
conns := checkoutConnections(t, pool, numConns)
assertConnectionCounts(t, pool, numConns, 0)
// Only return 2 of the connection.
for i := 0; i < 2; i++ {
err := pool.checkIn(conns[i])
assert.Nil(t, err, "checkIn() error at index %d: %v", i, err)
}
conns = conns[2:]
assertConnectionCounts(t, pool, numConns, 0)
// Disconnect and assert that events are published for all conections.
err := pool.disconnect(context.Background())
assert.Nil(t, err, "disconnect error: %v", err)
assertConnectionCounts(t, pool, numConns, numConns)
// Return the remaining connections and assert that the closed event count does not increase because
// these connections have already been closed.
for i, c := range conns {
err := pool.checkIn(c)
assert.Nil(t, err, "checkIn() error at index %d: %v", i, err)
}
assertConnectionCounts(t, pool, numConns, numConns)
// Ensure all closed events have the correct reason.
for len(closed) > 0 {
evt := <-closed
assert.Equal(t, event.ReasonPoolClosed, evt.Reason, "expected reason %q, got %q",
event.ReasonPoolClosed, evt.Reason)
}
})
})
})
}
func createTestPool(t *testing.T, cfg poolConfig, opts ...ConnectionOption) (*pool, func()) {
t.Helper()
pool := newPool(cfg, opts...)
err := pool.connect()
assert.Nil(t, err, "connect error: %v", err)
disconnect := func() {
_ = pool.disconnect(context.Background())
}
return pool, disconnect
}
func checkoutConnections(t *testing.T, p *pool, numConns int) []*connection {
conns := make([]*connection, 0, numConns)
for i := 0; i < numConns; i++ {
conn, err := p.checkOut(context.Background())
assert.Nil(t, err, "checkOut() error at index %d: %v", i, err)
conns = append(conns, conn)
}
return conns
}
|