open-nomad/helper/pool/pool_test.go
Michael Schurter 7494a0c4fd core: remove all traces of unused protocol version
Nomad inherited protocol version numbering configuration from Consul and
Serf, but unlike those projects Nomad has never used it. Nomad's
`protocol_version` has always been `1`.

While the code is effectively unused and therefore poses no runtime
risks to leave, I felt like removing it was best because:

1. Nomad's RPC subsystem has been able to evolve extensively without
   needing to increment the version number.
2. Nomad's HTTP API has evolved extensively without increment
   `API{Major,Minor}Version`. If we want to version the HTTP API in the
   future, I doubt this is the mechanism we would choose.
3. The presence of the `server.protocol_version` configuration
   parameter is confusing since `server.raft_protocol` *is* an important
   parameter for operators to consider. Even more confusing is that
   there is a distinct Serf protocol version which is included in `nomad
   server members` output under the heading `Protocol`. `raft_protocol`
   is the *only* protocol version relevant to Nomad developers and
   operators. The other protocol versions are either deadcode or have
   never changed (Serf).
4. If we were to need to version the RPC, HTTP API, or Serf protocols, I
   don't think these configuration parameters and variables are the best
   choice. If we come to that point we should choose a versioning scheme
   based on the use case and modern best practices -- not this 6+ year
   old dead code.
2022-02-18 16:12:36 -08:00

67 lines
1.2 KiB
Go

package pool
import (
"fmt"
"net"
"testing"
"time"
"github.com/hashicorp/nomad/helper/freeport"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/stretchr/testify/require"
)
func newTestPool(t *testing.T) *ConnPool {
l := testlog.HCLogger(t)
p := NewPool(l, 1*time.Minute, 10, nil)
return p
}
func TestConnPool_ConnListener(t *testing.T) {
require := require.New(t)
ports := freeport.MustTake(1)
defer freeport.Return(ports)
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
pool := newTestPool(t)
// Setup a listener
c := make(chan *Conn, 1)
pool.SetConnListener(c)
// Make an RPC
_, err = pool.acquire("test", addr)
require.Nil(err)
// 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.
require.Nil(pool.Shutdown())
_, ok := <-c
require.False(ok)
}