diff --git a/elect_test.go b/elect_test.go index 2c3d998..c2607c3 100644 --- a/elect_test.go +++ b/elect_test.go @@ -2,41 +2,31 @@ package elect_test import ( "fmt" - "net" - "net/http" "testing" "time" + "github.com/dchest/uniuri" "github.com/gopatchy/elect" + "github.com/gopatchy/proxy" + "github.com/samber/lo" "github.com/stretchr/testify/require" ) func TestSimple(t *testing.T) { t.Parallel() - c := elect.NewCandidate(1, "abc123") + signingKey := uniuri.New() - defer c.Stop() + ts := NewTestServer(t, signingKey) + defer ts.Stop() - listener, err := net.ListenTCP("tcp", nil) - require.NoError(t, err) + p := lo.Must(proxy.NewProxy(t, ts.Addr())) + defer p.Close() - srv := &http.Server{ - Handler: c, - ReadHeaderTimeout: 30 * time.Second, - } - - go func() { - err := srv.Serve(listener) - require.ErrorIs(t, err, http.ErrServerClosed) - }() - - defer srv.Close() - - v := elect.NewVoter(fmt.Sprintf("http://%s/", listener.Addr()), "abc123") - require.NotNil(t, v) + url := fmt.Sprintf("http://%s/", p.Addr()) + v := elect.NewVoter(url, signingKey) defer v.Stop() - require.Eventually(t, c.IsLeader, 15*time.Second, 100*time.Millisecond) + require.Eventually(t, ts.Candidate.IsLeader, 15*time.Second, 100*time.Millisecond) } diff --git a/go.mod b/go.mod index 267a09e..8926c1a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.20 require ( github.com/dchest/uniuri v1.2.0 github.com/go-resty/resty/v2 v2.7.0 + github.com/gopatchy/proxy v0.0.0-20230529015759-4d806f5d5908 github.com/samber/lo v1.38.1 github.com/stretchr/testify v1.8.4 go.uber.org/goleak v1.2.1 diff --git a/go.sum b/go.sum index 0620610..6565b41 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g= github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY= github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= +github.com/gopatchy/proxy v0.0.0-20230529015759-4d806f5d5908 h1:EGXKOTP1F8WxmgjqaUopxDHBu99fMhhsY9FLvK3VeWM= +github.com/gopatchy/proxy v0.0.0-20230529015759-4d806f5d5908/go.mod h1:KzL3DePH2QgRe2QskCWhh+a+ZehmMQR265CpY/NepCM= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= diff --git a/lib_test.go b/lib_test.go new file mode 100644 index 0000000..1e5b77c --- /dev/null +++ b/lib_test.go @@ -0,0 +1,47 @@ +package elect_test + +import ( + "net" + "net/http" + "testing" + "time" + + "github.com/gopatchy/elect" + "github.com/samber/lo" + "github.com/stretchr/testify/require" +) + +type TestServer struct { + Candidate *elect.Candidate + + listener *net.TCPListener + srv *http.Server +} + +func NewTestServer(t *testing.T, signingKey string) *TestServer { + ts := &TestServer{ + Candidate: elect.NewCandidate(1, signingKey), + listener: lo.Must(net.ListenTCP("tcp", nil)), + } + + ts.srv = &http.Server{ + Handler: ts.Candidate, + ReadHeaderTimeout: 30 * time.Second, + } + + go func() { + err := ts.srv.Serve(ts.listener) + require.ErrorIs(t, err, http.ErrServerClosed) + }() + + return ts +} + +func (ts *TestServer) Stop() { + ts.srv.Close() + ts.Candidate.Stop() +} + +func (ts *TestServer) Addr() *net.TCPAddr { + return ts.listener.Addr().(*net.TCPAddr) +}