Test ConnPool non-blocking connections locally
- instead of hitting microsoft.com
This commit is contained in:
+32
-12
@@ -154,26 +154,47 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) {
|
|||||||
releaseAllConnections(pool, allConnections)
|
releaseAllConnections(pool, allConnections)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPoolNonBlockingConections(t *testing.T) {
|
func TestPoolNonBlockingConnections(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
maxConnections := 5
|
var dialCountLock sync.Mutex
|
||||||
|
dialCount := 0
|
||||||
openTimeout := 1 * time.Second
|
openTimeout := 1 * time.Second
|
||||||
dialer := net.Dialer{
|
testDialer := func(network, address string) (net.Conn, error) {
|
||||||
Timeout: openTimeout,
|
var firstDial bool
|
||||||
|
dialCountLock.Lock()
|
||||||
|
dialCount++
|
||||||
|
firstDial = dialCount == 1
|
||||||
|
dialCountLock.Unlock()
|
||||||
|
|
||||||
|
if firstDial {
|
||||||
|
return net.Dial(network, address)
|
||||||
|
} else {
|
||||||
|
time.Sleep(openTimeout)
|
||||||
|
return nil, &net.OpError{Op: "dial", Net: "tcp"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maxConnections := 3
|
||||||
config := pgx.ConnPoolConfig{
|
config := pgx.ConnPoolConfig{
|
||||||
ConnConfig: *defaultConnConfig,
|
ConnConfig: *defaultConnConfig,
|
||||||
MaxConnections: maxConnections,
|
MaxConnections: maxConnections,
|
||||||
}
|
}
|
||||||
config.ConnConfig.Dial = dialer.Dial
|
config.ConnConfig.Dial = testDialer
|
||||||
// We need a server that would silently DROP all incoming requests.
|
|
||||||
// P.S. I bet there is something better than microsoft.com that does this...
|
|
||||||
config.Host = "microsoft.com"
|
|
||||||
|
|
||||||
pool, err := pgx.NewConnPool(config)
|
pool, err := pgx.NewConnPool(config)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected NewConnPool not to fail, instead it failed with")
|
t.Fatalf("Expected NewConnPool not to fail, instead it failed with: %v", err)
|
||||||
|
}
|
||||||
|
defer pool.Close()
|
||||||
|
|
||||||
|
// NewConnPool establishes an initial connection
|
||||||
|
// so we need to close that for the rest of the test
|
||||||
|
if conn, err := pool.Acquire(); err == nil {
|
||||||
|
conn.Close()
|
||||||
|
pool.Release(conn)
|
||||||
|
} else {
|
||||||
|
t.Fatalf("pool.Acquire unexpectedly failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@@ -196,10 +217,9 @@ func TestPoolNonBlockingConections(t *testing.T) {
|
|||||||
// With createConnectionUnlocked() it takes ~ 1 * openTimeout seconds.
|
// With createConnectionUnlocked() it takes ~ 1 * openTimeout seconds.
|
||||||
timeTaken := time.Now().Sub(startedAt)
|
timeTaken := time.Now().Sub(startedAt)
|
||||||
if timeTaken > openTimeout+1*time.Second {
|
if timeTaken > openTimeout+1*time.Second {
|
||||||
t.Fatalf("Expected all Aquire() to run in paralles and take about %v, instead it took '%v'", openTimeout, timeTaken)
|
t.Fatalf("Expected all Acquire() to run in parallel and take about %v, instead it took '%v'", openTimeout, timeTaken)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer pool.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAcquireTimeoutSanity(t *testing.T) {
|
func TestAcquireTimeoutSanity(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user