One proxy per voter

This commit is contained in:
Ian Gulliver
2023-06-07 21:52:38 -07:00
parent ddea4c91e4
commit 0bcd5a0b06

View File

@@ -1,7 +1,6 @@
package elect_test package elect_test
import ( import (
"fmt"
"net" "net"
"net/http" "net/http"
"testing" "testing"
@@ -25,7 +24,7 @@ type TestSystem struct {
signingKey string signingKey string
servers []*TestServer servers []*TestServer
voters []*elect.Voter voters []*elect.Voter
proxy *proxy.Proxy proxies []*proxy.Proxy
} }
func NewTestServer(t *testing.T, signingKey string) *TestServer { 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++ { for i := 0; i < num; i++ {
ts.servers = append(ts.servers, NewTestServer(t, ts.signingKey)) 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++ { for i := 0; i < num; i++ {
ts.voters = append(ts.voters, elect.NewVoter(url, ts.signingKey, ts.Candidate(i)))
} }
return ts return ts
@@ -85,21 +81,29 @@ func (ts *TestSystem) Stop() {
v.Stop() v.Stop()
} }
ts.proxy.Close() for _, p := range ts.proxies {
p.Close()
}
} }
func (ts *TestSystem) SetServer(i int) { func (ts *TestSystem) SetServer(i int) {
ts.proxy.SetBackend(ts.Server(i).Addr()) for _, p := range ts.proxies {
} p.SetBackend(ts.Server(i).Addr())
}
func (ts *TestSystem) Server(i int) *TestServer {
return ts.servers[i]
} }
func (ts *TestSystem) Candidate(i int) *elect.Candidate { func (ts *TestSystem) Candidate(i int) *elect.Candidate {
return ts.servers[i].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 { func (ts *TestSystem) Voter(i int) *elect.Voter {
return ts.voters[i] return ts.voters[i]
} }