Skip to content

Commit ff0619a

Browse files
committed
Improve examples and tests
1 parent 7152d48 commit ff0619a

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

examples/chat-client/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func main() {
4545
}
4646
}()
4747

48-
time.Sleep(10 * time.Second)
48+
time.Sleep(5 * time.Second)
4949

5050
c.WriteJSON(websocketserver.Message{
5151
Type: "logout",

examples/chat-server/main.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/labstack/echo/v4"
77
"github.com/labstack/echo/v4/middleware"
88
"github.com/ndthuan/websocketserver"
9-
"math/rand"
9+
"log"
1010
"time"
1111
)
1212

@@ -31,6 +31,7 @@ func broadcast(msg string, excluded *websocket.Conn) {
3131

3232
func loginHandler() websocketserver.MessageHandler {
3333
return func(connection *websocket.Conn, listener *websocketserver.Server, message websocketserver.Message) error {
34+
log.Println("Login handler triggered")
3435
username := message.Payload
3536

3637
authenticatedConnections[connection] = username
@@ -43,9 +44,14 @@ func loginHandler() websocketserver.MessageHandler {
4344

4445
func logoutHandler() websocketserver.MessageHandler {
4546
return func(connection *websocket.Conn, listener *websocketserver.Server, message websocketserver.Message) error {
47+
log.Println("Logout handler triggered")
4648
username, loggedIn := authenticatedConnections[connection]
4749

4850
if loggedIn {
51+
connection.WriteJSON(websocketserver.Message{
52+
Type: "system-message",
53+
Payload: "Bye-bye, " + username,
54+
})
4955
delete(authenticatedConnections, connection)
5056
broadcast(fmt.Sprintf("%s left the room", username), connection)
5157
_ = connection.WriteMessage(websocket.CloseMessage, []byte{})
@@ -57,9 +63,10 @@ func logoutHandler() websocketserver.MessageHandler {
5763

5864
func disconnectedCallback() websocketserver.ConnectionCallback {
5965
return func(connection *websocket.Conn, listener *websocketserver.Server) error {
66+
log.Println("Client disconnected")
6067
loggedInUser, already := authenticatedConnections[connection]
6168
if already {
62-
broadcast(fmt.Sprintf("%s disconnected", loggedInUser), connection)
69+
broadcast(fmt.Sprintf("%s was disconnected", loggedInUser), connection)
6370
delete(authenticatedConnections, connection)
6471
}
6572
return nil
@@ -68,9 +75,10 @@ func disconnectedCallback() websocketserver.ConnectionCallback {
6875

6976
func connectedCallback() websocketserver.ConnectionCallback {
7077
return func(connection *websocket.Conn, listener *websocketserver.Server) error {
78+
log.Println("Client connected")
7179
return connection.WriteJSON(websocketserver.Message{
7280
Type: "system-message",
73-
Payload: "Hi there, this is private message to you. Welcome!",
81+
Payload: "You are connected",
7482
})
7583
}
7684
}
@@ -89,8 +97,9 @@ func humanMessageHandler() websocketserver.MessageHandler {
8997

9098
func standaloneRunner() websocketserver.StandaloneRunner {
9199
return func(server *websocketserver.Server) {
100+
log.Println("Standalone runner was started")
92101
for {
93-
time.Sleep(time.Duration(rand.Intn(10)) * time.Second)
102+
time.Sleep(time.Second)
94103

95104
server.Broadcast(func(conn *websocket.Conn) (*websocketserver.Message, bool) {
96105
if username, loggedIn := authenticatedConnections[conn]; loggedIn {

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
5050
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5151
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
5252
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
53+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
5354
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
55+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
5456
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
57+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
5558
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5659
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
5760
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

scripts/test.sh

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
#!/usr/bin/env bash
22

3-
set -e
3+
set -ex
44

55
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
66
echo "Bad code format"
77
exit 1
88
fi
99

10+
echo "=== mod download ==="
11+
12+
go mod download
13+
1014
echo "=== unit test ==="
1115

1216
go test
1317

1418
echo "=== functional test ==="
1519

1620
go build -o /tmp/chatserver ./examples/chat-server/
21+
go build -o /tmp/chatclient ./examples/chat-client/
1722

1823
/tmp/chatserver > /tmp/output 2>&1 &
1924
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 &
25+
26+
sleep 3
27+
28+
/tmp/chatclient > /tmp/client-output 2>&1 &
29+
/tmp/chatclient >> /tmp/client-output 2>&1 &
30+
/tmp/chatclient >> /tmp/client-output 2>&1 &
2231

2332
sleep 10
2433

2534
kill $SERVER_PID
2635

27-
grep -q "http server started on" /tmp/output || (echo "server not started"; exit 1)
36+
grep -q "http server started on" /tmp/output || (echo "server was not started"; exit 1)
2837

29-
clientsConnected=$(grep -c 'Hi there, this is private message to you. Welcome!' /tmp/output)
30-
if [ "$clientsConnected" != "2" ]; then
31-
echo "there must be 2 clients connected, found: $clientsConnected"
38+
clientsConnected=$(grep -c 'Client connected' /tmp/output)
39+
if [ "$clientsConnected" != "3" ]; then
40+
echo "there must be 3 clients connected, found: $clientsConnected"
3241
exit 1
3342
fi
3443

35-
grep -q "Everyone, please welcome" /tmp/output || (echo "broadcast messages not sent"; exit 1)
44+
standaloneStarted=$(grep -c "Standalone runner was started" /tmp/output)
45+
if [ "$standaloneStarted" != "1" ]; then
46+
echo "standalone runner should be started only once, found: $standaloneStarted"
47+
exit 1
48+
fi
3649

37-
grep -q "how are you" /tmp/output || (echo "standalone runner's messages not sent"; exit 1)
50+
grep -q "Login handler triggered" /tmp/output || (echo "login handler was not triggered"; exit 1)
51+
grep -q "Logout handler triggered" /tmp/output || (echo "logout handler was not triggered"; exit 1)
52+
grep -q "Client disconnected" /tmp/output || (echo "disconnection callback was not triggered"; exit 1)
3853

3954
echo "All good"

server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func (s *Server) Handlers() map[string]MessageHandler {
6060
return s.handlers
6161
}
6262

63-
//OnAll registers a catch-all handler that will be executed on all message types. If there is a specific handler
64-
//registered on a type, this will be executed after.
63+
//OnAll registers a catch-all handler that will be executed on all message types.
64+
//If there is a specific handler registered on a type, this will be executed after.
6565
func (s *Server) OnAll(handler MessageHandler) {
6666
s.allHandler = handler
6767
}

0 commit comments

Comments
 (0)