diff options
| -rw-r--r-- | internal/quorum.go | 27 | ||||
| -rw-r--r-- | internal/vote.go | 15 | ||||
| -rw-r--r-- | internal/vote_test.go | 18 |
3 files changed, 49 insertions, 11 deletions
diff --git a/internal/quorum.go b/internal/quorum.go index 1307f20..1c8e451 100644 --- a/internal/quorum.go +++ b/internal/quorum.go @@ -2,17 +2,30 @@ package internal import ( "log" - "time" ) -type quorum struct { - ID string - Age time.Duration - Votes int -} +type quorumMap map[string]vote -type quorumMap map[string]quorum +type score struct { + id string + score int +} func (q quorumMap) vote(v vote) { log.Printf("Adding vote %v", v) + q[v.from] = v +} + +func (q quorumMap) score() (scores []score) { + /* + scoreMap := make(map[string]int) + var expired []string + + for from, vote := range q { + + for _, id := range vote.ids { + } + } + */ + return } diff --git a/internal/vote.go b/internal/vote.go index 3aadd53..b2db2d9 100644 --- a/internal/vote.go +++ b/internal/vote.go @@ -6,10 +6,12 @@ import ( "time" ) +const voteExpiry = 20 * time.Second + type vote struct { - from string - ids []string - time time.Time + from string + ids []string + expiresAt time.Time } func newVote(config config, from, message string) vote { @@ -22,5 +24,10 @@ func newVote(config config, from, message string) vote { ids = append(ids, id) } - return vote{stripPort(from), ids, time.Now()} + return vote{stripPort(from), ids, time.Now().Add(voteExpiry)} +} + +func (v vote) expired() bool { + now := time.Now() + return now.After(v.expiresAt) || now.Equal(v.expiresAt) } diff --git a/internal/vote_test.go b/internal/vote_test.go index 50f8a2f..9ebb57e 100644 --- a/internal/vote_test.go +++ b/internal/vote_test.go @@ -2,6 +2,7 @@ package internal import ( "testing" + "time" ) func TestVote(t *testing.T) { @@ -24,3 +25,20 @@ func TestVote(t *testing.T) { t.Errorf("Expected vote 2 to be bay but is %s", v.ids[1]) } } + +func TestVoteExpiry(t *testing.T) { + config := config{Participants: []string{"foo:1234", "bay:4321"}} + v := newVote(config, "earth:334234", " foo bar baz bay\n") + + // Set expiry 1h into the future + v.expiresAt = time.Now().Add(1 * time.Hour) + if v.expired() { + t.Errorf("Didn't expect vote to be expired") + } + + // Set expiry to now + v.expiresAt = time.Now() + if !v.expired() { + t.Errorf("Expected vote to be expired") + } +} |
