You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
2.4 KiB
Go

4 years ago
package main
import (
"io/ioutil"
"net"
"net/http"
4 years ago
"net/url"
4 years ago
"testing"
"time"
log "github.com/fangdingjun/go-log/v5"
"github.com/gorilla/websocket"
"gopkg.in/yaml.v2"
)
4 years ago
func echoServer(addr string) {
l1, err := net.Listen("tcp", addr)
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return
4 years ago
}
4 years ago
defer l1.Close()
for {
c1, err := l1.Accept()
if err != nil {
log.Errorln(err)
return
4 years ago
}
4 years ago
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
4 years ago
}
4 years ago
c.Write(data[:n])
log.Infof("%s receive: %s", addr, data[:n])
}
}(c1)
}
}
4 years ago
4 years ago
func sendAndRecv(addr string, msg string) string {
u, _ := url.Parse(addr)
if u.Scheme == "ws" || u.Scheme == "wss" {
return _sendAndRecvWS(addr, msg)
4 years ago
}
4 years ago
if u.Scheme == "tcp" {
return _sendAndRecvTCP(addr, msg)
4 years ago
}
4 years ago
return ""
}
func _sendAndRecvTCP(addr string, msg string) string {
u, _ := url.Parse(addr)
c, err := net.Dial("tcp", u.Host)
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return ""
4 years ago
}
4 years ago
4 years ago
defer c.Close()
4 years ago
_, err = c.Write([]byte(msg))
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return ""
4 years ago
}
4 years ago
data := make([]byte, 100)
n, err := c.Read(data)
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return ""
4 years ago
}
4 years ago
return string(data[:n])
}
func _sendAndRecvWS(addr string, msg string) string {
c1, resp, err := dialer.Dial(addr, nil)
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return ""
4 years ago
}
4 years ago
resp.Body.Close()
4 years ago
if resp.StatusCode != http.StatusSwitchingProtocols {
4 years ago
log.Errorf("dial ws code %d", resp.StatusCode)
4 years ago
return ""
4 years ago
}
4 years ago
defer c1.Close()
4 years ago
err = c1.WriteMessage(websocket.BinaryMessage, []byte(msg))
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return ""
4 years ago
}
4 years ago
4 years ago
_, d, err := c1.ReadMessage()
4 years ago
if err != nil {
4 years ago
log.Errorln(err)
return ""
4 years ago
}
4 years ago
return string(d)
}
4 years ago
4 years ago
func TestServer(t *testing.T) {
cfgfile := "config.example.yaml"
log.Default.Level = log.DEBUG
data, err := ioutil.ReadFile(cfgfile)
4 years ago
if err != nil {
4 years ago
log.Fatal(err)
4 years ago
}
4 years ago
var cfg conf
if err := yaml.Unmarshal(data, &cfg); err != nil {
log.Fatal(err)
4 years ago
}
4 years ago
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)
}
4 years ago
}
}