github.com/wangyougui/gf/v2@v2.6.5/net/gudp/gudp_z_unit_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/wangyougui/gf.
     6  
     7  package gudp_test
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/wangyougui/gf/v2/net/gudp"
    16  	"github.com/wangyougui/gf/v2/os/glog"
    17  	"github.com/wangyougui/gf/v2/test/gtest"
    18  	"github.com/wangyougui/gf/v2/util/gconv"
    19  )
    20  
    21  var (
    22  	simpleTimeout = time.Millisecond * 100
    23  	sendData      = []byte("hello")
    24  )
    25  
    26  func startUDPServer(addr string) *gudp.Server {
    27  	s := gudp.NewServer(addr, func(conn *gudp.Conn) {
    28  		defer conn.Close()
    29  		for {
    30  			data, err := conn.Recv(-1)
    31  			if err != nil {
    32  				break
    33  			}
    34  			conn.Send(data)
    35  		}
    36  	})
    37  	go s.Run()
    38  	time.Sleep(simpleTimeout)
    39  	return s
    40  }
    41  
    42  func Test_Basic(t *testing.T) {
    43  	var ctx = context.TODO()
    44  	s := gudp.NewServer(gudp.FreePortAddress, func(conn *gudp.Conn) {
    45  		defer conn.Close()
    46  		for {
    47  			data, err := conn.Recv(-1)
    48  			if len(data) > 0 {
    49  				if err := conn.Send(append([]byte("> "), data...)); err != nil {
    50  					glog.Error(ctx, err)
    51  				}
    52  			}
    53  			if err != nil {
    54  				break
    55  			}
    56  		}
    57  	})
    58  	go s.Run()
    59  	defer s.Close()
    60  	time.Sleep(100 * time.Millisecond)
    61  	// gudp.Conn.Send
    62  	gtest.C(t, func(t *gtest.T) {
    63  		for i := 0; i < 100; i++ {
    64  			conn, err := gudp.NewConn(s.GetListenedAddress())
    65  			t.AssertNil(err)
    66  			t.Assert(conn.Send([]byte(gconv.String(i))), nil)
    67  			t.AssertNil(conn.RemoteAddr())
    68  			result, err := conn.Recv(-1)
    69  			t.AssertNil(err)
    70  			t.AssertNE(conn.RemoteAddr(), nil)
    71  			t.Assert(string(result), fmt.Sprintf(`> %d`, i))
    72  			conn.Close()
    73  		}
    74  	})
    75  	// gudp.Conn.SendRecv
    76  	gtest.C(t, func(t *gtest.T) {
    77  		for i := 0; i < 100; i++ {
    78  			conn, err := gudp.NewConn(s.GetListenedAddress())
    79  			t.AssertNil(err)
    80  			_, err = conn.SendRecv([]byte(gconv.String(i)), -1)
    81  			t.AssertNil(err)
    82  			//t.Assert(string(result), fmt.Sprintf(`> %d`, i))
    83  			conn.Close()
    84  		}
    85  	})
    86  	// gudp.Conn.SendWithTimeout
    87  	gtest.C(t, func(t *gtest.T) {
    88  		for i := 0; i < 100; i++ {
    89  			conn, err := gudp.NewConn(s.GetListenedAddress())
    90  			t.AssertNil(err)
    91  			err = conn.SendWithTimeout([]byte(gconv.String(i)), time.Second)
    92  			t.AssertNil(err)
    93  			conn.Close()
    94  		}
    95  	})
    96  	// gudp.Conn.RecvWithTimeout
    97  	gtest.C(t, func(t *gtest.T) {
    98  		for i := 0; i < 100; i++ {
    99  			conn, err := gudp.NewConn(s.GetListenedAddress())
   100  			t.AssertNil(err)
   101  			err = conn.Send([]byte(gconv.String(i)))
   102  			t.AssertNil(err)
   103  			conn.SetBufferWaitRecv(time.Millisecond * 100)
   104  			result, err := conn.RecvWithTimeout(-1, time.Second)
   105  			t.AssertNil(err)
   106  			t.Assert(string(result), fmt.Sprintf(`> %d`, i))
   107  			conn.Close()
   108  		}
   109  	})
   110  	// gudp.Conn.SendRecvWithTimeout
   111  	gtest.C(t, func(t *gtest.T) {
   112  		for i := 0; i < 100; i++ {
   113  			conn, err := gudp.NewConn(s.GetListenedAddress())
   114  			t.AssertNil(err)
   115  			result, err := conn.SendRecvWithTimeout([]byte(gconv.String(i)), -1, time.Second)
   116  			t.AssertNil(err)
   117  			t.Assert(string(result), fmt.Sprintf(`> %d`, i))
   118  			conn.Close()
   119  		}
   120  	})
   121  	// gudp.Send
   122  	gtest.C(t, func(t *gtest.T) {
   123  		for i := 0; i < 100; i++ {
   124  			err := gudp.Send(s.GetListenedAddress(), []byte(gconv.String(i)))
   125  			t.AssertNil(err)
   126  		}
   127  	})
   128  	// gudp.SendRecv
   129  	gtest.C(t, func(t *gtest.T) {
   130  		for i := 0; i < 100; i++ {
   131  			result, err := gudp.SendRecv(s.GetListenedAddress(), []byte(gconv.String(i)), -1)
   132  			t.AssertNil(err)
   133  			t.Assert(string(result), fmt.Sprintf(`> %d`, i))
   134  		}
   135  	})
   136  }
   137  
   138  // If the read buffer size is less than the sent package size,
   139  // the rest data would be dropped.
   140  func Test_Buffer(t *testing.T) {
   141  	var ctx = context.TODO()
   142  	s := gudp.NewServer(gudp.FreePortAddress, func(conn *gudp.Conn) {
   143  		defer conn.Close()
   144  		for {
   145  			data, err := conn.Recv(1)
   146  			if len(data) > 0 {
   147  				if err := conn.Send(data); err != nil {
   148  					glog.Error(ctx, err)
   149  				}
   150  			}
   151  			if err != nil {
   152  				break
   153  			}
   154  		}
   155  	})
   156  	go s.Run()
   157  	defer s.Close()
   158  	time.Sleep(100 * time.Millisecond)
   159  	gtest.C(t, func(t *gtest.T) {
   160  		result, err := gudp.SendRecv(s.GetListenedAddress(), []byte("123"), -1)
   161  		t.AssertNil(err)
   162  		t.Assert(string(result), "1")
   163  	})
   164  	gtest.C(t, func(t *gtest.T) {
   165  		result, err := gudp.SendRecv(s.GetListenedAddress(), []byte("456"), -1)
   166  		t.AssertNil(err)
   167  		t.Assert(string(result), "4")
   168  	})
   169  }
   170  
   171  func Test_NewConn(t *testing.T) {
   172  	s := startUDPServer(gudp.FreePortAddress)
   173  
   174  	gtest.C(t, func(t *gtest.T) {
   175  		conn, err := gudp.NewConn(s.GetListenedAddress(), fmt.Sprintf("127.0.0.1:%d", gudp.MustGetFreePort()))
   176  		t.AssertNil(err)
   177  		conn.SetDeadline(time.Now().Add(time.Second))
   178  		t.Assert(conn.Send(sendData), nil)
   179  		conn.Close()
   180  	})
   181  
   182  	gtest.C(t, func(t *gtest.T) {
   183  		conn, err := gudp.NewConn(s.GetListenedAddress(), fmt.Sprintf("127.0.0.1:%d", 99999))
   184  		t.AssertNil(conn)
   185  		t.AssertNE(err, nil)
   186  	})
   187  
   188  	gtest.C(t, func(t *gtest.T) {
   189  		conn, err := gudp.NewConn(fmt.Sprintf("127.0.0.1:%d", 99999))
   190  		t.AssertNil(conn)
   191  		t.AssertNE(err, nil)
   192  	})
   193  }
   194  
   195  func Test_GetFreePorts(t *testing.T) {
   196  	gtest.C(t, func(t *gtest.T) {
   197  		ports, err := gudp.GetFreePorts(2)
   198  		t.AssertNil(err)
   199  		t.AssertEQ(len(ports), 2)
   200  	})
   201  }
   202  
   203  func Test_Server(t *testing.T) {
   204  	gudp.NewServer(gudp.FreePortAddress, func(conn *gudp.Conn) {
   205  		defer conn.Close()
   206  		for {
   207  			data, err := conn.Recv(1)
   208  			if len(data) > 0 {
   209  				conn.Send(data)
   210  			}
   211  			if err != nil {
   212  				break
   213  			}
   214  		}
   215  	}, "GoFrameUDPServer")
   216  
   217  	gtest.C(t, func(t *gtest.T) {
   218  		server := gudp.GetServer("GoFrameUDPServer")
   219  		t.AssertNE(server, nil)
   220  		server = gudp.GetServer("TestUDPServer")
   221  		t.AssertNE(server, nil)
   222  		server.SetAddress("127.0.0.1:8888")
   223  		server.SetHandler(func(conn *gudp.Conn) {
   224  			defer conn.Close()
   225  			for {
   226  				conn.Send([]byte("OtherHandle"))
   227  			}
   228  		})
   229  	})
   230  }