fix: websocket test failed (#11)

This commit is contained in:
soren yang 2018-05-14 14:06:05 +08:00 committed by siddontang
parent cb568a3e5c
commit 2b7082d296
3 changed files with 38 additions and 24 deletions

View File

@ -581,7 +581,7 @@ var marshalErrorItems = []testItemType{
{&structWithDupKeys{}, {&structWithDupKeys{},
"Duplicated key 'name' in struct bson_test.structWithDupKeys"}, "Duplicated key 'name' in struct bson_test.structWithDupKeys"},
{bson.Raw{0x0A, []byte{}}, {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}}, {&inlineCantPtr{&struct{ A, B int }{1, 2}},
"Option ,inline needs a struct value or map field"}, "Option ,inline needs a struct value or map field"},
{&inlineDupName{1, struct{ A, B int }{2, 3}}, {&inlineDupName{1, struct{ A, B int }{2, 3}},
@ -1429,9 +1429,9 @@ func (s *S) TestObjectIdJSONUnmarshaling(c *C) {
func (s *S) TestObjectIdJSONUnmarshalingError(c *C) { func (s *S) TestObjectIdJSONUnmarshalingError(c *C) {
v := jsonType{} v := jsonType{}
err := json.Unmarshal([]byte(`{"Id":"4d88e15b60f486e428412dc9A"}`), &v) 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) 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" .*`)
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -4,6 +4,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"sync"
"testing" "testing"
"time" "time"
@ -11,45 +12,48 @@ import (
) )
func TestWSClient(t *testing.T) { func TestWSClient(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
http.HandleFunc("/test/client", func(w http.ResponseWriter, r *http.Request) { 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) conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
t.Log("websocket.Upgrade")
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() msgType, msg, err := conn.ReadMessage()
conn.WriteMessage(websocket.TextMessage, msg)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if msgType != websocket.TextMessage { if msgType != websocket.TextMessage {
t.Fatal("invalid msg type", msgType) t.Fatal("invalid msg type", msgType)
} }
msgType, msg, err = conn.ReadMessage() err = conn.WriteMessage(websocket.TextMessage, msg)
if err != nil { if err != nil {
t.Fatal(err.Error()) 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() msgType, msg, err = conn.ReadMessage()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
println(msgType) if msgType != websocket.TextMessage {
if msgType != websocket.PongMessage {
t.Fatal("invalid msg type", msgType) t.Fatal("invalid msg type", msgType)
} }
conn.WriteMessage(websocket.PongMessage, []byte("server.Pong"))
}) })
go http.ListenAndServe(":65500", nil) go http.ListenAndServe(":65500", nil)
@ -61,7 +65,7 @@ func TestWSClient(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err.Error()) 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 { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -72,9 +76,12 @@ func TestWSClient(t *testing.T) {
payload[i] = 'x' 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 { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -84,11 +91,14 @@ func TestWSClient(t *testing.T) {
if string(msg) != string(payload) { if string(msg) != string(payload) {
t.Fatal("invalid msg", string(msg)) t.Fatal("invalid msg", string(msg))
} }
//test ping //test ping
ws.Ping([]byte{}) err = ws.Ping([]byte("client.Ping"))
if err != nil {
t.Fatal(err.Error())
}
msgType, msg, err = ws.ReadMessage() msgType, msg, err = ws.ReadMessage()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -97,4 +107,8 @@ func TestWSClient(t *testing.T) {
t.Fatal("invalid msg type", msgType) t.Fatal("invalid msg type", msgType)
} }
ws.WriteMessage(websocket.TextMessage, []byte("done"))
// ws.Close()
wg.Wait()
} }

View File

@ -63,7 +63,7 @@ func TestWSServer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err.Error()) 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 { ws.SetPongHandler(func(string) error {
println("pong") println("pong")