github.com/gogf/gf@v1.16.9/net/gtcp/gtcp_unit_pool_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gtcp_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/net/gtcp"
    12  	"github.com/gogf/gf/test/gtest"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func Test_Pool_Basic1(t *testing.T) {
    18  	p, _ := ports.PopRand()
    19  	s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
    20  		defer conn.Close()
    21  		for {
    22  			data, err := conn.RecvPkg()
    23  			if err != nil {
    24  				break
    25  			}
    26  			conn.SendPkg(data)
    27  		}
    28  	})
    29  	go s.Run()
    30  	defer s.Close()
    31  	time.Sleep(100 * time.Millisecond)
    32  	gtest.C(t, func(t *gtest.T) {
    33  		conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
    34  		t.Assert(err, nil)
    35  		defer conn.Close()
    36  		data := []byte("9999")
    37  		err = conn.SendPkg(data)
    38  		t.Assert(err, nil)
    39  		err = conn.SendPkgWithTimeout(data, time.Second)
    40  		t.Assert(err, nil)
    41  	})
    42  }
    43  
    44  func Test_Pool_Basic2(t *testing.T) {
    45  	p, _ := ports.PopRand()
    46  	s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
    47  		conn.Close()
    48  	})
    49  	go s.Run()
    50  	defer s.Close()
    51  	time.Sleep(100 * time.Millisecond)
    52  	gtest.C(t, func(t *gtest.T) {
    53  		conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
    54  		t.Assert(err, nil)
    55  		defer conn.Close()
    56  		data := []byte("9999")
    57  		err = conn.SendPkg(data)
    58  		t.Assert(err, nil)
    59  		//err = conn.SendPkgWithTimeout(data, time.Second)
    60  		//t.Assert(err, nil)
    61  
    62  		_, err = conn.SendRecv(data, -1)
    63  		t.AssertNE(err, nil)
    64  	})
    65  }