From 0bcd5a0b06306b1385a2373dcc9d67efa1cf14c4 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 7 Jun 2023 21:52:38 -0700 Subject: [PATCH] One proxy per voter --- lib_test.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib_test.go b/lib_test.go index 692d0da..ed9e534 100644 --- a/lib_test.go +++ b/lib_test.go @@ -1,7 +1,6 @@ package elect_test import ( - "fmt" "net" "net/http" "testing" @@ -25,7 +24,7 @@ type TestSystem struct { signingKey string servers []*TestServer voters []*elect.Voter - proxy *proxy.Proxy + proxies []*proxy.Proxy } func NewTestServer(t *testing.T, signingKey string) *TestServer { @@ -63,14 +62,11 @@ func NewTestSystem(t *testing.T, num int) *TestSystem { for i := 0; i < num; i++ { ts.servers = append(ts.servers, NewTestServer(t, ts.signingKey)) + ts.proxies = append(ts.proxies, proxy.NewProxy(t, ts.Server(0).Addr())) + ts.voters = append(ts.voters, elect.NewVoter(ts.Proxy(i).HTTP(), ts.signingKey, ts.Candidate(i))) } - ts.proxy = lo.Must(proxy.NewProxy(t, ts.Server(0).Addr())) - - url := fmt.Sprintf("http://%s/", ts.proxy.Addr()) - for i := 0; i < num; i++ { - ts.voters = append(ts.voters, elect.NewVoter(url, ts.signingKey, ts.Candidate(i))) } return ts @@ -85,21 +81,29 @@ func (ts *TestSystem) Stop() { v.Stop() } - ts.proxy.Close() + for _, p := range ts.proxies { + p.Close() + } } func (ts *TestSystem) SetServer(i int) { - ts.proxy.SetBackend(ts.Server(i).Addr()) -} - -func (ts *TestSystem) Server(i int) *TestServer { - return ts.servers[i] + for _, p := range ts.proxies { + p.SetBackend(ts.Server(i).Addr()) + } } func (ts *TestSystem) Candidate(i int) *elect.Candidate { return ts.servers[i].Candidate } +func (ts *TestSystem) Proxy(i int) *proxy.Proxy { + return ts.proxies[i] +} + +func (ts *TestSystem) Server(i int) *TestServer { + return ts.servers[i] +} + func (ts *TestSystem) Voter(i int) *elect.Voter { return ts.voters[i] }