github.com/gogf/gf/v2@v2.7.4/net/gtcp/gtcp_z_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  	"testing"
    11  	"time"
    12  
    13  	"github.com/gogf/gf/v2/net/gtcp"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  	"github.com/gogf/gf/v2/text/gstr"
    16  )
    17  
    18  func Test_Pool_Basic1(t *testing.T) {
    19  	s := gtcp.NewServer(gtcp.FreePortAddress, 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(s.GetListenedAddress())
    34  		t.AssertNil(err)
    35  		defer conn.Close()
    36  		data := []byte("9999")
    37  		err = conn.SendPkg(data)
    38  		t.AssertNil(err)
    39  		err = conn.SendPkgWithTimeout(data, time.Second)
    40  		t.AssertNil(err)
    41  	})
    42  
    43  	gtest.C(t, func(t *gtest.T) {
    44  		_, err := gtcp.NewPoolConn("127.0.0.1:80")
    45  		t.AssertNE(err, nil)
    46  	})
    47  }
    48  
    49  func Test_Pool_Basic2(t *testing.T) {
    50  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
    51  		conn.Close()
    52  	})
    53  	go s.Run()
    54  	defer s.Close()
    55  	time.Sleep(100 * time.Millisecond)
    56  	gtest.C(t, func(t *gtest.T) {
    57  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
    58  		t.AssertNil(err)
    59  		defer conn.Close()
    60  		data := []byte("9999")
    61  		err = conn.SendPkg(data)
    62  		t.AssertNil(err)
    63  		//err = conn.SendPkgWithTimeout(data, time.Second)
    64  		//t.AssertNil(err)
    65  
    66  		_, err = conn.SendRecv(data, -1)
    67  		t.AssertNE(err, nil)
    68  	})
    69  }
    70  
    71  func Test_Pool_Send(t *testing.T) {
    72  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
    73  		for {
    74  			data, err := conn.Recv(-1)
    75  			if err != nil {
    76  				break
    77  			}
    78  			conn.Send(data)
    79  		}
    80  	})
    81  	go s.Run()
    82  	defer s.Close()
    83  	time.Sleep(100 * time.Millisecond)
    84  	gtest.C(t, func(t *gtest.T) {
    85  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
    86  		t.AssertNil(err)
    87  		defer conn.Close()
    88  		data := []byte("9999")
    89  		err = conn.Send(data)
    90  		t.AssertNil(err)
    91  		result, err := conn.Recv(-1)
    92  		t.AssertNil(err)
    93  		t.Assert(result, data)
    94  	})
    95  }
    96  
    97  func Test_Pool_Recv(t *testing.T) {
    98  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
    99  		for {
   100  			data, err := conn.Recv(-1)
   101  			if err != nil {
   102  				break
   103  			}
   104  			conn.Send(data)
   105  		}
   106  	})
   107  	go s.Run()
   108  	defer s.Close()
   109  	time.Sleep(100 * time.Millisecond)
   110  	gtest.C(t, func(t *gtest.T) {
   111  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
   112  		t.AssertNil(err)
   113  		defer conn.Close()
   114  		data := []byte("9999")
   115  		err = conn.Send(data)
   116  		t.AssertNil(err)
   117  		time.Sleep(100 * time.Millisecond)
   118  		result, err := conn.Recv(-1)
   119  		t.AssertNil(err)
   120  		t.Assert(result, data)
   121  	})
   122  }
   123  
   124  func Test_Pool_RecvLine(t *testing.T) {
   125  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
   126  		for {
   127  			data, err := conn.Recv(-1)
   128  			if err != nil {
   129  				break
   130  			}
   131  			conn.Send(data)
   132  		}
   133  	})
   134  	go s.Run()
   135  	defer s.Close()
   136  	time.Sleep(100 * time.Millisecond)
   137  	gtest.C(t, func(t *gtest.T) {
   138  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
   139  		t.AssertNil(err)
   140  		defer conn.Close()
   141  		data := []byte("9999\n")
   142  		err = conn.Send(data)
   143  		t.AssertNil(err)
   144  		time.Sleep(100 * time.Millisecond)
   145  		result, err := conn.RecvLine()
   146  		t.AssertNil(err)
   147  		splitData := gstr.Split(string(data), "\n")
   148  		t.Assert(result, splitData[0])
   149  	})
   150  }
   151  
   152  func Test_Pool_RecvTill(t *testing.T) {
   153  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
   154  		for {
   155  			data, err := conn.Recv(-1)
   156  			if err != nil {
   157  				break
   158  			}
   159  			conn.Send(data)
   160  		}
   161  	})
   162  	go s.Run()
   163  	defer s.Close()
   164  	time.Sleep(100 * time.Millisecond)
   165  	gtest.C(t, func(t *gtest.T) {
   166  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
   167  		t.AssertNil(err)
   168  		defer conn.Close()
   169  		data := []byte("9999\n")
   170  		err = conn.Send(data)
   171  		t.AssertNil(err)
   172  		time.Sleep(100 * time.Millisecond)
   173  		result, err := conn.RecvTill([]byte("\n"))
   174  		t.AssertNil(err)
   175  		t.Assert(result, data)
   176  	})
   177  }
   178  
   179  func Test_Pool_RecvWithTimeout(t *testing.T) {
   180  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
   181  		for {
   182  			data, err := conn.Recv(-1)
   183  			if err != nil {
   184  				break
   185  			}
   186  			conn.Send(data)
   187  		}
   188  	})
   189  	go s.Run()
   190  	defer s.Close()
   191  	time.Sleep(100 * time.Millisecond)
   192  	gtest.C(t, func(t *gtest.T) {
   193  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
   194  		t.AssertNil(err)
   195  		defer conn.Close()
   196  		data := []byte("9999")
   197  		err = conn.Send(data)
   198  		t.AssertNil(err)
   199  		time.Sleep(100 * time.Millisecond)
   200  		result, err := conn.RecvWithTimeout(-1, time.Millisecond*500)
   201  		t.AssertNil(err)
   202  		t.Assert(data, result)
   203  	})
   204  }
   205  
   206  func Test_Pool_SendWithTimeout(t *testing.T) {
   207  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
   208  		for {
   209  			data, err := conn.Recv(-1)
   210  			if err != nil {
   211  				break
   212  			}
   213  			conn.Send(data)
   214  		}
   215  	})
   216  	go s.Run()
   217  	defer s.Close()
   218  	time.Sleep(100 * time.Millisecond)
   219  	gtest.C(t, func(t *gtest.T) {
   220  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
   221  		t.AssertNil(err)
   222  		defer conn.Close()
   223  		data := []byte("9999")
   224  		err = conn.SendWithTimeout(data, time.Millisecond*500)
   225  		t.AssertNil(err)
   226  		result, err := conn.Recv(-1)
   227  		t.AssertNil(err)
   228  		t.Assert(data, result)
   229  	})
   230  }
   231  
   232  func Test_Pool_SendRecvWithTimeout(t *testing.T) {
   233  	s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
   234  		for {
   235  			data, err := conn.Recv(-1)
   236  			if err != nil {
   237  				break
   238  			}
   239  			conn.Send(data)
   240  		}
   241  	})
   242  	go s.Run()
   243  	defer s.Close()
   244  	time.Sleep(100 * time.Millisecond)
   245  	gtest.C(t, func(t *gtest.T) {
   246  		conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
   247  		t.AssertNil(err)
   248  		defer conn.Close()
   249  		data := []byte("9999")
   250  		result, err := conn.SendRecvWithTimeout(data, -1, time.Millisecond*500)
   251  		t.AssertNil(err)
   252  		t.Assert(data, result)
   253  	})
   254  }