summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-05-19 01:26:18 +0300
committerPaul Buetow <paul@buetow.org>2023-05-19 01:26:18 +0300
commit5dc583de26cc7fc9816ce6b44ca4e80e723b8311 (patch)
tree079a564ad847a27217c237fefca791f5094b6588
parentf03889b92628d1589d2e161c56510646398686a1 (diff)
add vote test
-rw-r--r--internal/tcpserver.go3
-rw-r--r--internal/vote.go13
-rw-r--r--internal/vote_test.go33
3 files changed, 44 insertions, 5 deletions
diff --git a/internal/tcpserver.go b/internal/tcpserver.go
index a7153f8..b689419 100644
--- a/internal/tcpserver.go
+++ b/internal/tcpserver.go
@@ -6,7 +6,6 @@ import (
"fmt"
"log"
"net"
- "time"
)
func startTcpServer(ctx context.Context, config config, ch chan<- vote) error {
@@ -54,7 +53,7 @@ func handleConnection(ctx context.Context, conn net.Conn, ch chan<- vote) {
}
log.Printf("Received message from %s: %s", remoteAddr, message)
- ch <- vote{From: remoteAddr, time: time.Now()}
+ ch <- newVote(remoteAddr, message)
conn.Write([]byte(message))
}
diff --git a/internal/vote.go b/internal/vote.go
index 322c620..80fdb9f 100644
--- a/internal/vote.go
+++ b/internal/vote.go
@@ -1,9 +1,16 @@
package internal
-import "time"
+import (
+ "strings"
+ "time"
+)
type vote struct {
- From string
- IDs []string
+ from string
+ ids []string
time time.Time
}
+
+func newVote(from, message string) vote {
+ return vote{stripPort(from), strings.Split(strings.TrimSpace(message), " "), time.Now()}
+}
diff --git a/internal/vote_test.go b/internal/vote_test.go
new file mode 100644
index 0000000..d5afe34
--- /dev/null
+++ b/internal/vote_test.go
@@ -0,0 +1,33 @@
+package internal
+
+import (
+ "testing"
+)
+
+func TestVote(t *testing.T) {
+ v := newVote("earth:334234", " foo bar baz bay\n")
+
+ if v.from != "earth" {
+ t.Errorf("Expected vote to come from earth but came from %s", v.from)
+ }
+
+ if len(v.ids) != 4 {
+ t.Errorf("Expected vote length to be 4 but is %d", len(v.ids))
+ }
+
+ if v.ids[0] != "foo" {
+ t.Errorf("Expected vote 1 to be foo but is %s", v.ids[0])
+ }
+
+ if v.ids[1] != "bar" {
+ t.Errorf("Expected vote 2 to be bar but is %s", v.ids[1])
+ }
+
+ if v.ids[2] != "baz" {
+ t.Errorf("Expected vote 3 to be baz but is %s", v.ids[2])
+ }
+
+ if v.ids[3] != "bay" {
+ t.Errorf("Expected vote 3 to be bay but is %s", v.ids[3])
+ }
+}