diff --git a/admin/client.go b/admin/client.go index 17537a2..71810c3 100644 --- a/admin/client.go +++ b/admin/client.go @@ -52,18 +52,26 @@ func (c *Client) RPCCall(addr, method string, params interface{}, result interfa return err } - onReply, err := c.Send(nkn.NewStringArray(addr), req, nil) + var onReply *nkn.OnMessage + var reply *nkn.Message +Loop: + for i := 0; i < 3; i++ { // retry 3 times if timeout + onReply, err = c.Send(nkn.NewStringArray(addr), req, nil) + if err != nil { + return err + } + + select { + case reply = <-onReply.C: + break Loop + case <-time.After(c.replyTimeout): + err = errReplyTimeout + } + } if err != nil { return err } - var reply *nkn.Message - select { - case reply = <-onReply.C: - case <-time.After(c.replyTimeout): - return errReplyTimeout - } - resp := &rpcResp{ Result: result, } diff --git a/tests/config.go b/tests/config.go index 8b4f00b..dc7927e 100644 --- a/tests/config.go +++ b/tests/config.go @@ -7,9 +7,9 @@ const ( seedHex = "e68e046d13dd911594576ba0f4a196e9666790dc492071ad9ea5972c0b940435" - tcpPort = ":54321" - httpPort = ":54322" - udpPort = ":54323" + tcpPort = ":20001" + httpPort = ":20002" + udpPort = ":20003" tunaNodeStarted = "tuna node is started" ) diff --git a/tests/main_test.go b/tests/main_test.go index d9eec9d..68a9edc 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -60,7 +60,7 @@ func TestMain(m *testing.M) { return } - time.Sleep(5 * time.Second) + time.Sleep(10 * time.Second) exitVal := m.Run() os.Exit(exitVal) diff --git a/tests/pub.go b/tests/pub.go index 2b66bb1..faf48d7 100644 --- a/tests/pub.go +++ b/tests/pub.go @@ -42,7 +42,7 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error } if opts.Client { - port, err := getFreePort(port) + port, err = getFreePort(port) if err != nil { return err } @@ -66,7 +66,7 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error } }() - time.Sleep(3 * time.Second) // wait for nconnect to create tunnels + time.Sleep(5 * time.Second) // wait for nconnect to create tunnels tunnels := nc.GetTunnels() for _, tunnel := range tunnels { @@ -140,16 +140,16 @@ type Person struct { Age int } -func getFreePort(port int) (int, error) { +func getFreePort(p int) (int, error) { for i := 0; i < 100; i++ { - addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", port)) + addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", p)) if err != nil { return 0, err } l, err := net.ListenTCP("tcp", addr) if err != nil { - port++ + p++ continue } @@ -164,8 +164,8 @@ func waitForSSProxReady() error { for i := 0; i < 100; i++ { conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%v", port)) if err != nil { - fmt.Printf("waitForSSProxReady err: %v\n", err) time.Sleep(2 * time.Second) + continue } if conn != nil { conn.Close() diff --git a/tests/tcp.go b/tests/tcp.go index 5f39160..00783da 100644 --- a/tests/tcp.go +++ b/tests/tcp.go @@ -22,6 +22,7 @@ func StartTcpServer() error { return err } go func(conn net.Conn) { + defer conn.Close() b := make([]byte, 1024) for { n, err := conn.Read(b) @@ -57,6 +58,8 @@ func StartTCPClient(serverAddr string) error { fmt.Printf("StartTCPClient, dailer.Dial err: %v\n", err) return err } + + defer conn.Close() fmt.Printf("StartTCPClient, dail to %v success\n", serverAddr) user := &Person{Name: "tcp_boy", Age: 0} diff --git a/tests/udp.go b/tests/udp.go index 842a455..5d44cb5 100644 --- a/tests/udp.go +++ b/tests/udp.go @@ -20,6 +20,7 @@ func StartUdpServer() error { return err } + defer udpServer.Close() fmt.Printf("UDP server is listening at %v\n", udpPort) b := make([]byte, 1024)