Skip to content

Commit

Permalink
fix: websocket test failed (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsytj0413 authored and siddontang committed May 14, 2018
1 parent cb568a3 commit 2b7082d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
6 changes: 3 additions & 3 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ var marshalErrorItems = []testItemType{
{&structWithDupKeys{},
"Duplicated key 'name' in struct bson_test.structWithDupKeys"},
{bson.Raw{0x0A, []byte{}},
"Attempted to unmarshal Raw kind 10 as a document"},
"Attempted to marshal Raw kind 10 as a document"},
{&inlineCantPtr{&struct{ A, B int }{1, 2}},
"Option ,inline needs a struct value or map field"},
{&inlineDupName{1, struct{ A, B int }{2, 3}},
Expand Down Expand Up @@ -1429,9 +1429,9 @@ func (s *S) TestObjectIdJSONUnmarshaling(c *C) {
func (s *S) TestObjectIdJSONUnmarshalingError(c *C) {
v := jsonType{}
err := json.Unmarshal([]byte(`{"Id":"4d88e15b60f486e428412dc9A"}`), &v)
c.Assert(err, ErrorMatches, `Invalid ObjectId in JSON: "4d88e15b60f486e428412dc9A"`)
c.Assert(err, ErrorMatches, `invalid ObjectId in JSON: "4d88e15b60f486e428412dc9A"`)
err = json.Unmarshal([]byte(`{"Id":"4d88e15b60f486e428412dcZ"}`), &v)
c.Assert(err, ErrorMatches, `Invalid ObjectId in JSON: "4d88e15b60f486e428412dcZ" .*`)
c.Assert(err, ErrorMatches, `invalid ObjectId in JSON: "4d88e15b60f486e428412dcZ" .*`)
}

// --------------------------------------------------------------------------
Expand Down
54 changes: 34 additions & 20 deletions websocket/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,56 @@ import (
"net"
"net/http"
"net/url"
"sync"
"testing"
"time"

"github.com/gorilla/websocket"
)

func TestWSClient(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)

http.HandleFunc("/test/client", func(w http.ResponseWriter, r *http.Request) {
defer func() {
t.Log("server: wg.Done")
wg.Done()
}()

conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if err != nil {
t.Fatal(err.Error())
}
t.Log("websocket.Upgrade")

msgType, msg, err := conn.ReadMessage()
conn.WriteMessage(websocket.TextMessage, msg)
conn.SetPingHandler(func(d string) error {
t.Log("receive from client: ", d)
conn.WriteMessage(websocket.PongMessage, []byte("server.Pong"))
return nil
})

msgType, msg, err := conn.ReadMessage()
if err != nil {
t.Fatal(err.Error())
}

if msgType != websocket.TextMessage {
t.Fatal("invalid msg type", msgType)
}

msgType, msg, err = conn.ReadMessage()
err = conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
t.Fatal(err.Error())
}

if msgType != websocket.PingMessage {
t.Fatal("invalid msg type", msgType)
}

conn.WriteMessage(websocket.PongMessage, []byte{})

conn.WriteMessage(websocket.PingMessage, []byte{})

msgType, msg, err = conn.ReadMessage()
if err != nil {
t.Fatal(err.Error())
}
println(msgType)
if msgType != websocket.PongMessage {

if msgType != websocket.TextMessage {
t.Fatal("invalid msg type", msgType)
}
conn.WriteMessage(websocket.PongMessage, []byte("server.Pong"))
})

go http.ListenAndServe(":65500", nil)
Expand All @@ -61,7 +65,7 @@ func TestWSClient(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
ws, _, err := NewClient(conn, &url.URL{Host: "127.0.0.1:65500", Path: "/test/client"}, nil)
ws, _, err := NewClient(conn, &url.URL{Scheme: "ws", Host: "127.0.0.1:65501", Path: "/test/client"}, nil)

if err != nil {
t.Fatal(err.Error())
Expand All @@ -72,9 +76,12 @@ func TestWSClient(t *testing.T) {
payload[i] = 'x'
}

ws.WriteString(payload)
err = ws.WriteString(payload)
if err != nil {
t.Fatal(err.Error())
}

msgType, msg, err := ws.Read()
msgType, msg, err := ws.ReadMessage()
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -84,11 +91,14 @@ func TestWSClient(t *testing.T) {

if string(msg) != string(payload) {
t.Fatal("invalid msg", string(msg))

}

//test ping
ws.Ping([]byte{})
err = ws.Ping([]byte("client.Ping"))
if err != nil {
t.Fatal(err.Error())
}

msgType, msg, err = ws.ReadMessage()
if err != nil {
t.Fatal(err.Error())
Expand All @@ -97,4 +107,8 @@ func TestWSClient(t *testing.T) {
t.Fatal("invalid msg type", msgType)
}

ws.WriteMessage(websocket.TextMessage, []byte("done"))

// ws.Close()
wg.Wait()
}
2 changes: 1 addition & 1 deletion websocket/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestWSServer(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
ws, _, err := websocket.NewClient(conn, &url.URL{Host: "127.0.0.1:65500", Path: "/test/server"}, nil, 1024, 1024)
ws, _, err := websocket.NewClient(conn, &url.URL{Scheme: "ws", Host: "127.0.0.1:65500", Path: "/test/server"}, nil, 1024, 1024)

ws.SetPongHandler(func(string) error {
println("pong")
Expand Down

0 comments on commit 2b7082d

Please sign in to comment.