refine test

master
dingjun 4 years ago
parent 3b3dc12918
commit 951abc1f55

@ -1,10 +1,10 @@
package main package main
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url"
"testing" "testing"
"time" "time"
@ -13,25 +13,15 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
func TestServer(t *testing.T) { func echoServer(addr string) {
cfgfile := "config.example.yaml" l1, err := net.Listen("tcp", addr)
data, err := ioutil.ReadFile(cfgfile)
if err != nil { if err != nil {
log.Fatal(err) log.Errorln(err)
} return
var cfg conf
if err := yaml.Unmarshal(data, &cfg); err != nil {
log.Fatal(err)
}
makeServers(cfg)
l1, err := net.Listen("tcp", "127.0.0.1:2903")
if err != nil {
t.Fatal(err)
} }
go func() {
defer l1.Close() defer l1.Close()
for {
for {
c1, err := l1.Accept() c1, err := l1.Accept()
if err != nil { if err != nil {
log.Errorln(err) log.Errorln(err)
@ -47,93 +37,101 @@ func TestServer(t *testing.T) {
break break
} }
c.Write(data[:n]) c.Write(data[:n])
log.Infof("2903 receive: %s", string(data[:n])) log.Infof("%s receive: %s", addr, data[:n])
} }
}(c1) }(c1)
} }
}()
l2, err := net.Listen("tcp", "127.0.0.1:2904")
if err != nil {
t.Fatal(err)
} }
go func() {
defer l2.Close() func sendAndRecv(addr string, msg string) string {
for { u, _ := url.Parse(addr)
c1, err := l2.Accept() if u.Scheme == "ws" || u.Scheme == "wss" {
if err != nil { return _sendAndRecvWS(addr, msg)
log.Errorln(err)
}
go func(c net.Conn) {
defer c.Close()
data := make([]byte, 1024)
for {
n, err := c.Read(data)
if err != nil {
log.Errorln(err)
break
} }
c.Write(data[:n]) if u.Scheme == "tcp" {
log.Infof("2904 receive: %s", string(data[:n])) return _sendAndRecvTCP(addr, msg)
} }
}(c1) return ""
} }
}()
time.Sleep(time.Second) func _sendAndRecvTCP(addr string, msg string) string {
c1, resp, err := dialer.Dial("ws://127.0.0.1:2901/p1", nil) u, _ := url.Parse(addr)
c, err := net.Dial("tcp", u.Host)
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
} return ""
if resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("dial ws code %d", resp.StatusCode)
} }
err = c1.WriteMessage(websocket.BinaryMessage, []byte("p1"))
_, err = c.Write([]byte(msg))
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
_, d, err := c1.ReadMessage()
data := make([]byte, 100)
n, err := c.Read(data)
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
} return ""
if !bytes.Equal([]byte("p1"), d) {
t.Errorf("failed msg not equal, expect p1, got %s", d)
} }
c2, resp, err := dialer.Dial("ws://127.0.0.1:2901/p2", nil) return string(data[:n])
if err != nil {
t.Fatal(err)
} }
func _sendAndRecvWS(addr string, msg string) string {
c1, resp, err := dialer.Dial(addr, nil)
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
if resp.StatusCode != http.StatusSwitchingProtocols { if resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("dial ws code %d", resp.StatusCode) log.Errorf("dial ws code %d", resp.StatusCode)
} }
err = c2.WriteMessage(websocket.BinaryMessage, []byte("p2")) err = c1.WriteMessage(websocket.BinaryMessage, []byte(msg))
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
_, d, err = c2.ReadMessage() _, d, err := c1.ReadMessage()
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
if !bytes.Equal([]byte("p2"), d) { return string(d)
t.Errorf("failed msg not equal, expect p2, got %s", d)
} }
c3, err := net.Dial("tcp", "127.0.0.1:2905") func TestServer(t *testing.T) {
cfgfile := "config.example.yaml"
log.Default.Level = log.DEBUG
data, err := ioutil.ReadFile(cfgfile)
if err != nil { if err != nil {
t.Fatal(err) log.Fatal(err)
} }
_, err = c3.Write([]byte("c3")) var cfg conf
if err != nil { if err := yaml.Unmarshal(data, &cfg); err != nil {
t.Fatal(err) log.Fatal(err)
} }
d2 := make([]byte, 20)
n, err := c3.Read(d2) makeServers(cfg)
if err != nil {
t.Fatal(err) go echoServer("127.0.0.1:2903")
go echoServer("127.0.0.1:2904")
time.Sleep(time.Second)
testdata := []struct {
addr string
msg string
}{
{"ws://127.0.0.1:2901/p1", "p1"},
{"ws://127.0.0.1:2901/p2", "p2"},
{"tcp://127.0.0.1:2905", "c3"},
}
for _, tt := range testdata {
_m := sendAndRecv(tt.addr, tt.msg)
if _m != tt.msg {
t.Errorf("expected %s, got %s", tt.msg, _m)
} }
if !bytes.Equal([]byte("c3"), d2[:n]) {
t.Errorf("failed msg not equal, expect c3, got %s", d2[:n])
} }
} }

Loading…
Cancel
Save