TestServer helper, integrate proxy
This commit is contained in:
@@ -2,41 +2,31 @@ package elect_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/dchest/uniuri"
|
||||||
"github.com/gopatchy/elect"
|
"github.com/gopatchy/elect"
|
||||||
|
"github.com/gopatchy/proxy"
|
||||||
|
"github.com/samber/lo"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSimple(t *testing.T) {
|
func TestSimple(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
p := lo.Must(proxy.NewProxy(t, ts.Addr()))
|
||||||
require.NoError(t, err)
|
defer p.Close()
|
||||||
|
|
||||||
srv := &http.Server{
|
url := fmt.Sprintf("http://%s/", p.Addr())
|
||||||
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)
|
|
||||||
|
|
||||||
|
v := elect.NewVoter(url, signingKey)
|
||||||
defer v.Stop()
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -5,6 +5,7 @@ go 1.20
|
|||||||
require (
|
require (
|
||||||
github.com/dchest/uniuri v1.2.0
|
github.com/dchest/uniuri v1.2.0
|
||||||
github.com/go-resty/resty/v2 v2.7.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/samber/lo v1.38.1
|
||||||
github.com/stretchr/testify v1.8.4
|
github.com/stretchr/testify v1.8.4
|
||||||
go.uber.org/goleak v1.2.1
|
go.uber.org/goleak v1.2.1
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
|
||||||
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
|
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/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 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
|||||||
47
lib_test.go
Normal file
47
lib_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user