From 548700540cb17755e14fae42413d28d462c01157 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 7 Jun 2023 09:21:38 +0300 Subject: refactor --- internal/quorum/quorum.go | 5 +++-- internal/quorum/quorum_test.go | 30 ++++++++++++++++++++---------- internal/server/tcpserver.go | 7 +++++-- internal/vote/vote.go | 7 +++++-- internal/vote/vote_test.go | 12 ++++++++---- 5 files changed, 41 insertions(+), 20 deletions(-) (limited to 'internal') diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go index 44834f3..fb1d69d 100644 --- a/internal/quorum/quorum.go +++ b/internal/quorum/quorum.go @@ -12,9 +12,10 @@ import ( type Quorum struct { conf config.Config - votes map[string]vote.Vote - voteCh chan vote.Vote prevLiveNodes []string + + voteCh chan vote.Vote + votes map[string]vote.Vote } type Score struct { diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go index e8c02a7..8ae4043 100644 --- a/internal/quorum/quorum_test.go +++ b/internal/quorum/quorum_test.go @@ -9,8 +9,10 @@ import ( ) func TestScore(t *testing.T) { - conf := config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}} - quo := New(conf) + var ( + conf = config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}} + quo = New(conf) + ) vote1 := vote.New(conf, "foo foo bar\n") vote1.ExpiresAt = time.Now().Add(1 * time.Hour) @@ -55,8 +57,10 @@ func TestTieScore(t *testing.T) { t.Run("First tie score test", func(t *testing.T) { // If it is a tie, the first particpant (here: "foo") will win. - conf := config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}} - quo := New(conf) + var ( + conf = config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}} + quo = New(conf) + ) addVotes(conf, quo) scores := quo.score() @@ -76,8 +80,10 @@ func TestTieScore(t *testing.T) { t.Run("Second tie score test", func(t *testing.T) { // If it is a tie, the first particpant (here: "bar") will win. - conf := config.Config{Nodes: []string{"bar:1234", "foo:4321", "baz:3444"}} - quo := New(conf) + var ( + conf = config.Config{Nodes: []string{"bar:1234", "foo:4321", "baz:3444"}} + quo = New(conf) + ) addVotes(conf, quo) scores := quo.score() @@ -97,8 +103,10 @@ func TestTieScore(t *testing.T) { } func TestExpire(t *testing.T) { - conf := config.Config{Nodes: []string{"foo:1234", "bay:4321"}} - quo := New(conf) + var ( + conf = config.Config{Nodes: []string{"foo:1234", "bay:4321"}} + quo = New(conf) + ) vote1 := vote.New(conf, " foo bar baz bay\n") vote1.ExpiresAt = time.Now().Add(1 * time.Hour) @@ -127,8 +135,10 @@ func TestExpire(t *testing.T) { } func TestLiveNodes(t *testing.T) { - conf := config.Config{Nodes: []string{"foo:1234", "bay:4321"}} - quo := New(conf) + var ( + conf = config.Config{Nodes: []string{"foo:1234", "bay:4321"}} + quo = New(conf) + ) vote1 := vote.New(conf, " foo bar baz bay\n") vote1.ExpiresAt = time.Now().Add(1 * time.Hour) diff --git a/internal/server/tcpserver.go b/internal/server/tcpserver.go index 30baa4a..983228b 100644 --- a/internal/server/tcpserver.go +++ b/internal/server/tcpserver.go @@ -44,9 +44,12 @@ func handleConnection(ctx context.Context, conf config.Config, conn net.Conn, ch chan<- vote.Vote) { defer conn.Close() - remoteAddr := conn.RemoteAddr().String() - reader := bufio.NewReader(conn) + var ( + remoteAddr = conn.RemoteAddr().String() + reader = bufio.NewReader(conn) + ) + for { select { case <-ctx.Done(): diff --git a/internal/vote/vote.go b/internal/vote/vote.go index 4fff0fd..d5781b5 100644 --- a/internal/vote/vote.go +++ b/internal/vote/vote.go @@ -17,8 +17,11 @@ type Vote struct { } func New(conf config.Config, message string) Vote { - var fromID string - var ids []string + var ( + fromID string + ids []string + ) + for _, id := range strings.Split(strings.TrimSpace(message), " ") { if !conf.IsNode(id) { log.Printf("%s is not a node, excluding from the vote", id) diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index 24af55f..ca9e77c 100644 --- a/internal/vote/vote_test.go +++ b/internal/vote/vote_test.go @@ -8,8 +8,10 @@ import ( ) func TestVote(t *testing.T) { - conf := config.Config{Nodes: []string{"earth:1234", "foo:1234", "bay:4321"}} - v := New(conf, "earth foo bar baz bay\n") + var ( + conf = config.Config{Nodes: []string{"earth:1234", "foo:1234", "bay:4321"}} + v = New(conf, "earth foo bar baz bay\n") + ) if v.FromID != "earth" { t.Errorf("Expected vote to come from earth but came from %s", v.FromID) @@ -29,8 +31,10 @@ func TestVote(t *testing.T) { } func TestVoteExpiry(t *testing.T) { - conf := config.Config{Nodes: []string{"foo:1234", "bay:4321"}} - v := New(conf, "earth foo bar baz bay\n") + var ( + conf = config.Config{Nodes: []string{"foo:1234", "bay:4321"}} + v = New(conf, "earth foo bar baz bay\n") + ) // Set expiry 1h into the future v.ExpiresAt = time.Now().Add(1 * time.Hour) -- cgit v1.2.3