for leasedkv tests, don't use default etcd config

The default etcd config means that if two of this test run around the
same time, we end up with one of them failing because it can't bind.
Elsewhere, we resolve this by binding to ephemeral ports and fixing
up the config to use them, so we duplicate that here.

This includes duplicating the existing listenerWithURL from test/,
because that package has to import us, so we can't import it, and
I don't really want to make a separate package for one trivial
function.
This commit is contained in:
Seebs 2022-01-21 10:38:56 -06:00
parent 096c44884a
commit 03a18e9beb

View file

@ -3,7 +3,8 @@ package etcd
import (
"context"
"errors"
"fmt"
"net"
"os"
"testing"
"time"
@ -11,16 +12,48 @@ import (
"github.com/molecula/featurebase/v2/disco"
"github.com/molecula/featurebase/v2/logger"
"github.com/molecula/featurebase/v2/testhook"
"github.com/pkg/errors"
"go.etcd.io/etcd/embed"
"go.etcd.io/etcd/etcdserver/api/v3client"
"go.etcd.io/etcd/pkg/types"
)
const initVal = "test"
const newVal = "newValue"
// listenerWithURL builds a TCP listener and corresponding http://localhost:%d
// URL, and returns those. Identical to the copy in /test, except we can't
// import that because it imports us.
func listenerWithURL() (listener *net.TCPListener, url string, err error) {
l, err := net.Listen("tcp", ":0")
if err != nil {
return listener, url, err
}
listener = l.(*net.TCPListener)
port := listener.Addr().(*net.TCPAddr).Port
url = fmt.Sprintf("http://localhost:%d", port)
return listener, url, err
}
func TestLeasedKv(t *testing.T) {
cfg := embed.NewConfig()
clientListener, clientURL, err := listenerWithURL()
if err != nil {
t.Fatal(errors.Wrap(err, "creating client listener"))
}
peerListener, peerURL, err := listenerWithURL()
if err != nil {
t.Fatal(errors.Wrap(err, "creating peer listener"))
}
cfg.LPUrls = types.MustNewURLs([]string{peerURL})
cfg.LPeerSocket = []*net.TCPListener{peerListener}
cfg.APUrls = types.MustNewURLs([]string{peerURL})
cfg.LCUrls = types.MustNewURLs([]string{clientURL})
cfg.LClientSocket = []*net.TCPListener{clientListener}
cfg.ACUrls = types.MustNewURLs([]string{clientURL})
cfg.InitialCluster = cfg.Name + "=" + peerURL
dir, err := testhook.TempDir(t, "leasedkv-*")
if err != nil {
t.Fatal(err)