2018-01-12 21:58:44 +00:00
|
|
|
package pool
|
2018-01-11 01:21:28 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-12 21:58:44 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-01-11 01:21:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-01-12 21:58:44 +00:00
|
|
|
"github.com/hashicorp/consul/lib/freeport"
|
2018-01-11 01:21:28 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/yamux"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newTestPool(t *testing.T) *ConnPool {
|
|
|
|
w := testlog.NewWriter(t)
|
|
|
|
p := NewPool(w, 1*time.Minute, 10, nil)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnPool_ConnListener(t *testing.T) {
|
2018-01-12 21:58:44 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
ports := freeport.GetT(t, 1)
|
|
|
|
addrStr := fmt.Sprintf("127.0.0.1:%d", ports[0])
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", addrStr)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
exitCh := make(chan struct{})
|
|
|
|
defer close(exitCh)
|
|
|
|
go func() {
|
|
|
|
ln, err := net.Listen("tcp", addrStr)
|
|
|
|
require.Nil(err)
|
|
|
|
defer ln.Close()
|
|
|
|
conn, _ := ln.Accept()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
<-exitCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Create a test pool
|
2018-01-11 01:21:28 +00:00
|
|
|
pool := newTestPool(t)
|
|
|
|
|
|
|
|
// Setup a listener
|
|
|
|
c := make(chan *yamux.Session, 1)
|
|
|
|
pool.SetConnListener(c)
|
|
|
|
|
|
|
|
// Make an RPC
|
2018-01-12 21:58:44 +00:00
|
|
|
_, err = pool.acquire("test", addr, structs.ApiMajorVersion)
|
|
|
|
require.Nil(err)
|
2018-01-11 01:21:28 +00:00
|
|
|
|
|
|
|
// Assert we get a connection.
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the channel is closed when the pool shuts down.
|
2018-01-12 21:58:44 +00:00
|
|
|
require.Nil(pool.Shutdown())
|
2018-01-11 01:21:28 +00:00
|
|
|
_, ok := <-c
|
2018-01-12 21:58:44 +00:00
|
|
|
require.False(ok)
|
2018-01-11 01:21:28 +00:00
|
|
|
}
|