Add TestSystem

This commit is contained in:
Ian Gulliver
2023-06-04 10:43:32 -07:00
parent 0b9c2543e4
commit bce6488620
3 changed files with 61 additions and 27 deletions

View File

@@ -20,8 +20,8 @@ type Voter struct {
// used by loop() goroutine only
client *resty.Client
signingKey []byte
candidate *Candidate
vote vote
candidates []*Candidate
period time.Duration
}
@@ -40,12 +40,13 @@ type voteResponse struct {
ResponseSent time.Time `json:"responseSent"`
}
func NewVoter(url string, signingKey string) *Voter {
func NewVoter(url string, signingKey string, candidate *Candidate) *Voter {
v := &Voter{
client: resty.New().
SetCloseConnection(true).
SetBaseURL(url),
signingKey: []byte(signingKey),
candidate: candidate,
update: make(chan time.Duration),
done: make(chan bool),
vote: vote{
@@ -64,10 +65,6 @@ func (v *Voter) Stop() {
<-v.done
}
func (v *Voter) AddCandidate(c *Candidate) {
v.candidates = append(v.candidates, c)
}
func (v *Voter) loop() {
defer close(v.done)
@@ -111,9 +108,7 @@ func (v *Voter) poll() bool {
func (v *Voter) sendVote() {
v.vote.VoteSent = time.Now().UTC()
for _, c := range v.candidates {
c.voteIfNo(&v.vote)
}
v.candidate.voteIfNo(&v.vote)
js := lo.Must(json.Marshal(v.vote))