Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed May 13, 2024
1 parent 14243ce commit 280f706
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ jobs:
go-version: "1.22"
cache: false
- name: Run lint
uses: golangci/golangci-lint-action@v5
uses: golangci/golangci-lint-action@v6
with:
version: v1.57
version: v1.58
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ go 1.22

require (
github.com/gorilla/websocket v1.5.1
github.com/stretchr/testify v1.8.4
goyave.dev/goyave/v5 v5.0.0-rc10
)

require (
github.com/Code-Hex/uniseg v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/samber/lo v1.39.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/net v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/gorm v1.25.9 // indirect
goyave.dev/copier v0.4.3 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
Expand Down
79 changes: 79 additions & 0 deletions http/controller/chat/chat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package chat

import (
"fmt"
"sync"
"testing"
"time"

ws "github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"goyave.dev/goyave/v5"
"goyave.dev/goyave/v5/middleware/parse"
"goyave.dev/goyave/v5/util/testutil"
"goyave.dev/goyave/v5/websocket"
)

func TestChat(t *testing.T) {
server := testutil.NewTestServer(t, "config.test.json")

hub := NewHub()
server.RegisterRoutes(func(_ *goyave.Server, router *goyave.Router) {
router.GlobalMiddleware(&parse.Middleware{})
router.Subrouter("/chat").Controller(websocket.New(hub))
})

wg := sync.WaitGroup{}
wg.Add(2)

go hub.Run()

server.RegisterStartupHook(func(_ *goyave.Server) {
go func() {
defer wg.Done()
connectClient(t, server.Host(), "bob", func(conn *ws.Conn) {
go func() {
// Connect the second user when bob is already connected
// so they can see the "alice joined." message.
defer wg.Done()
connectClient(t, server.Host(), "alice", func(conn *ws.Conn) {
expectMessage(t, conn, "bob: Hi Alice!")
assert.NoError(t, conn.WriteMessage(ws.TextMessage, []byte("What's up Bob?")))
expectMessage(t, conn, "alice: What's up Bob?")
})
}()
expectMessage(t, conn, "alice joined.")
assert.NoError(t, conn.WriteMessage(ws.TextMessage, []byte("Hi Alice!")))
expectMessage(t, conn, "bob: Hi Alice!")
expectMessage(t, conn, "alice: What's up Bob?")
expectMessage(t, conn, "alice left.")
})
}()
})

go func() {
assert.NoError(t, server.Start())
}()
defer server.Stop()

wg.Wait()
}

func connectClient(t *testing.T, serverHost, name string, f func(conn *ws.Conn)) {
addr := fmt.Sprintf("ws://%s/chat?name=%s", serverHost, name)
conn, _, err := ws.DefaultDialer.Dial(addr, nil)
assert.NoError(t, err)
defer func() { _ = conn.Close() }()

f(conn)

m := ws.FormatCloseMessage(ws.CloseNormalClosure, "Connection closed by client")
assert.NoError(t, conn.WriteControl(ws.CloseMessage, m, time.Now().Add(time.Second)))
}

func expectMessage(t *testing.T, conn *ws.Conn, message string) {
messageType, data, err := conn.ReadMessage()
assert.NoError(t, err)
assert.Equal(t, ws.TextMessage, messageType)
assert.Equal(t, []byte(message), data)
}

0 comments on commit 280f706

Please sign in to comment.