diff options
| author | Paul Buetow <paul@buetow.org> | 2023-06-18 21:12:18 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-06-18 21:12:18 +0300 |
| commit | 71a5b5fc4b1e8a652f1bc8ca7c04eacaa289faf3 (patch) | |
| tree | 7b1a0d6b4fb01deec30536175df27d5dae1b5fd1 /internal/vote | |
| parent | 831137abdecfcafeb21fb5f3de45156819f35ed4 (diff) | |
can specify my id
Diffstat (limited to 'internal/vote')
| -rw-r--r-- | internal/vote/vote.go | 12 | ||||
| -rw-r--r-- | internal/vote/vote_test.go | 29 |
2 files changed, 27 insertions, 14 deletions
diff --git a/internal/vote/vote.go b/internal/vote/vote.go index 6fa2510..5378655 100644 --- a/internal/vote/vote.go +++ b/internal/vote/vote.go @@ -2,8 +2,9 @@ package vote import ( "encoding/json" - "os" "time" + + "codeberg.org/snonux/gorum/internal/config" ) const Expiry = 11 * time.Second @@ -14,15 +15,12 @@ type Vote struct { ExpiresAt time.Time `json:"-"` } -func New(ids []string) (Vote, error) { +func New(conf config.Config, ids []string) (Vote, error) { var v Vote - hostname, err := os.Hostname() - if err != nil { - return v, err - } - v.FromID = hostname + v.FromID = conf.MyID v.IDs = ids + return v, nil } diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index cccb1be..b5480b9 100644 --- a/internal/vote/vote_test.go +++ b/internal/vote/vote_test.go @@ -1,18 +1,23 @@ package vote import ( - "os" "testing" "time" + + "codeberg.org/snonux/gorum/internal/config" ) func TestVote(t *testing.T) { t.Parallel() - v, _ := New([]string{"foo", "bar", "baz", "bay"}) - hostname, _ := os.Hostname() - if v.FromID != hostname { - t.Errorf("Expected vote to come from earth but came from %s", v.FromID) + conf := config.Config{ + MyID: "foo.zone", + } + + v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) + + if v.FromID != "foo.zone" { + t.Errorf("Expected vote to come from foo.zone but came from %s", v.FromID) } if len(v.IDs) != 4 { @@ -30,7 +35,12 @@ func TestVote(t *testing.T) { func TestVoteExpiry(t *testing.T) { t.Parallel() - v, _ := New([]string{"foo", "bar", "baz", "bay"}) + + conf := config.Config{ + MyID: "foo.zone", + } + + v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) // Set expiry 1h into the future v.ExpiresAt = time.Now().Add(1 * time.Hour) @@ -47,7 +57,12 @@ func TestVoteExpiry(t *testing.T) { func TestMarshalling(t *testing.T) { t.Parallel() - v, _ := New([]string{"foo", "bar", "baz", "bay"}) + + conf := config.Config{ + MyID: "foo.zone", + } + + v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) jsonStr, err := v.ToJSON() if err != nil { t.Errorf("unable to serialize vote to json: %v", err) |
