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