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 {
log.Fatal(err)
}
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 { if err != nil {
t.Fatal(err) log.Errorln(err)
return
} }
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")
func sendAndRecv(addr string, msg string) string {
u, _ := url.Parse(addr)
if u.Scheme == "ws" || u.Scheme == "wss" {
return _sendAndRecvWS(addr, msg)
}
if u.Scheme == "tcp" {
return _sendAndRecvTCP(addr, msg)
}
return ""
}
func _sendAndRecvTCP(addr string, msg string) string {
u, _ := url.Parse(addr)
c, err := net.Dial("tcp", u.Host)
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
go func() {
defer l2.Close() _, err = c.Write([]byte(msg))
for {
c1, err := l2.Accept()
if err != nil { if err != nil {
log.Errorln(err) log.Errorln(err)
return ""
} }
go func(c net.Conn) {
defer c.Close() data := make([]byte, 100)
data := make([]byte, 1024)
for {
n, err := c.Read(data) n, err := c.Read(data)
if err != nil { if err != nil {
log.Errorln(err) log.Errorln(err)
break return ""
}
c.Write(data[:n])
log.Infof("2904 receive: %s", string(data[:n]))
}
}(c1)
} }
}() return string(data[:n])
}
time.Sleep(time.Second) func _sendAndRecvWS(addr string, msg string) string {
c1, resp, err := dialer.Dial("ws://127.0.0.1:2901/p1", nil) 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 = c1.WriteMessage(websocket.BinaryMessage, []byte("p1")) err = c1.WriteMessage(websocket.BinaryMessage, []byte(msg))
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
_, d, err := c1.ReadMessage() _, d, err := c1.ReadMessage()
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)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("dial ws code %d", resp.StatusCode)
}
err = c2.WriteMessage(websocket.BinaryMessage, []byte("p2"))
if err != nil {
t.Fatal(err)
}
_, d, err = c2.ReadMessage()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal([]byte("p2"), d) {
t.Errorf("failed msg not equal, expect p2, got %s", d)
} }
return string(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