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,127 +13,125 @@ 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) return
return
}
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])
log.Infof("2903 receive: %s", string(data[:n]))
}
}(c1)
} }
}() go func(c net.Conn) {
l2, err := net.Listen("tcp", "127.0.0.1:2904") defer c.Close()
if err != nil { data := make([]byte, 1024)
t.Fatal(err) for {
} n, err := c.Read(data)
go func() { if err != nil {
defer l2.Close() log.Errorln(err)
for { break
c1, err := l2.Accept()
if err != nil {
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])
log.Infof("2904 receive: %s", string(data[:n]))
} }
}(c1) c.Write(data[:n])
} log.Infof("%s receive: %s", addr, data[:n])
}() }
}(c1)
}
}
time.Sleep(time.Second) func sendAndRecv(addr string, msg string) string {
c1, resp, err := dialer.Dial("ws://127.0.0.1:2901/p1", nil) u, _ := url.Parse(addr)
if err != nil { if u.Scheme == "ws" || u.Scheme == "wss" {
t.Fatal(err) return _sendAndRecvWS(addr, msg)
} }
if resp.StatusCode != http.StatusSwitchingProtocols { if u.Scheme == "tcp" {
t.Fatalf("dial ws code %d", resp.StatusCode) return _sendAndRecvTCP(addr, msg)
} }
err = c1.WriteMessage(websocket.BinaryMessage, []byte("p1")) 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 ""
} }
_, d, err := c1.ReadMessage()
_, err = c.Write([]byte(msg))
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)
data := make([]byte, 100)
n, err := c.Read(data)
if err != nil { if err != nil {
t.Fatal(err) log.Errorln(err)
return ""
} }
return string(data[:n])
}
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) {
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) {
if err != nil { cfgfile := "config.example.yaml"
t.Fatal(err)
} log.Default.Level = log.DEBUG
_, err = c3.Write([]byte("c3"))
data, err := ioutil.ReadFile(cfgfile)
if err != nil { if err != nil {
t.Fatal(err) log.Fatal(err)
} }
d2 := make([]byte, 20) var cfg conf
n, err := c3.Read(d2) if err := yaml.Unmarshal(data, &cfg); err != nil {
if err != nil { log.Fatal(err)
t.Fatal(err)
} }
if !bytes.Equal([]byte("c3"), d2[:n]) {
t.Errorf("failed msg not equal, expect c3, got %s", d2[:n]) makeServers(cfg)
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)
}
} }
} }

Loading…
Cancel
Save