Skip to content

Commit 6a6431f

Browse files
committed
Basic functional tests
1 parent 121070a commit 6a6431f

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

.github/workflows/docker.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Go
1+
name: websocketserver
22

33
on:
44
push:

examples/chat-client/main.go

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
65
"github.com/gorilla/websocket"
76
"github.com/ndthuan/websocketserver"
87
"os"
8+
"time"
99
)
1010

1111
func main() {
@@ -45,33 +45,12 @@ func main() {
4545
}
4646
}()
4747

48-
go func() {
49-
s := bufio.NewScanner(os.Stdin)
50-
51-
for {
52-
if !s.Scan() {
53-
break
54-
}
55-
56-
input := s.Text()
57-
58-
switch input {
59-
case "bye":
60-
c.WriteJSON(websocketserver.Message{
61-
Type: "logout",
62-
Payload: "Gotta go",
63-
})
48+
time.Sleep(10 * time.Second)
6449

65-
quit <- 0
66-
break
67-
default:
68-
c.WriteJSON(websocketserver.Message{
69-
Type: "human-message",
70-
Payload: input,
71-
})
72-
}
73-
}
74-
}()
50+
c.WriteJSON(websocketserver.Message{
51+
Type: "logout",
52+
Payload: "Gotta go",
53+
})
7554

7655
select {
7756
case <-quit:

scripts/test.sh

+22
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,26 @@ if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
77
exit 1
88
fi
99

10+
echo "=== unit test ==="
11+
1012
go test
13+
14+
echo "=== functional test ==="
15+
16+
go build -o /tmp/chatserver ./examples/chat-server/
17+
18+
/tmp/chatserver > /tmp/output 2>&1 &
19+
SERVER_PID=$!
20+
go run examples/chat-client/main.go >> /tmp/output 2>&1 &
21+
go run examples/chat-client/main.go >> /tmp/output 2>&1 &
22+
23+
sleep 10
24+
25+
kill $SERVER_PID
26+
27+
grep -q "http server started on" /tmp/output || (echo "server not started"; exit 1)
28+
grep -q "Hi there, this is private message to you" /tmp/output || (echo "private message not sent"; exit 1)
29+
grep -q "Everyone, please welcome" /tmp/output || (echo "broadcast messages not sent"; exit 1)
30+
grep -q "how are you" /tmp/output || (echo "standalone runner's messages not sent"; exit 1)
31+
32+
echo "All good"

0 commit comments

Comments
 (0)