github.com/sunvim/utils@v0.1.0/netpoll/net_test.go (about)

     1  // Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
     2  // This package is licensed under a MIT license that can be found in the LICENSE file.
     3  
     4  package netpoll
     5  
     6  import (
     7  	"net"
     8  	"strings"
     9  	"sync"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/oxtoacart/bpool"
    14  )
    15  
    16  func TestListenAndServe(t *testing.T) {
    17  	var handler = &DataHandler{
    18  		Pool: bpool.NewBytePool(1024, 12*1024),
    19  		HandlerFunc: func(req []byte) (res []byte) {
    20  			res = req
    21  			return
    22  		},
    23  	}
    24  	err := ListenAndServe("", "", handler)
    25  	if err == nil {
    26  		t.Error("Unexpected")
    27  	}
    28  	network := "tcp"
    29  	addr := ":8888"
    30  	go ListenAndServe(network, addr, handler)
    31  	time.Sleep(time.Millisecond * 10)
    32  }
    33  
    34  func TestServe(t *testing.T) {
    35  	var handler = &DataHandler{
    36  		Pool: bpool.NewBytePool(1024, 12*1024),
    37  		HandlerFunc: func(req []byte) (res []byte) {
    38  			res = req
    39  			return
    40  		},
    41  	}
    42  	err := Serve(nil, handler)
    43  	if err == nil {
    44  		t.Error("Unexpected")
    45  	}
    46  	network := "tcp"
    47  	addr := ":8889"
    48  	l, _ := net.Listen(network, addr)
    49  	go Serve(l, handler)
    50  	time.Sleep(time.Millisecond * 10)
    51  }
    52  
    53  func TestNetServer(t *testing.T) {
    54  	var handler = &DataHandler{
    55  		Pool: bpool.NewBytePool(1024, 12*1024),
    56  		HandlerFunc: func(req []byte) (res []byte) {
    57  			res = req
    58  			return
    59  		},
    60  	}
    61  	server := &netServer{
    62  		Handler: handler,
    63  	}
    64  	network := "tcp"
    65  	addr := ":9999"
    66  	l, _ := net.Listen(network, addr)
    67  	wg := sync.WaitGroup{}
    68  	wg.Add(1)
    69  	go func() {
    70  		defer wg.Done()
    71  		if err := server.Serve(l); err == nil {
    72  			t.Error(err)
    73  		}
    74  	}()
    75  	conn, _ := net.Dial(network, addr)
    76  	msg := "Hello World"
    77  	msg = strings.Repeat(msg, 50)
    78  	if n, err := conn.Write([]byte(msg)); err != nil {
    79  		t.Error(err)
    80  	} else if n != len(msg) {
    81  		t.Error(n)
    82  	}
    83  	buf := make([]byte, len(msg))
    84  	if n, err := conn.Read(buf); err != nil {
    85  		t.Error(err)
    86  	} else if n != len(msg) {
    87  		t.Error(n)
    88  	} else if string(buf) != msg {
    89  		t.Error(string(buf))
    90  	}
    91  	conn.Close()
    92  	server.Close()
    93  	wg.Wait()
    94  }
    95  
    96  func TestNetServerUpgrade(t *testing.T) {
    97  	var handler = &ConnHandler{}
    98  	server := &netServer{
    99  		Handler: handler,
   100  	}
   101  	network := "tcp"
   102  	addr := ":9999"
   103  	l, _ := net.Listen(network, addr)
   104  	wg := sync.WaitGroup{}
   105  	wg.Add(1)
   106  	go func() {
   107  		defer wg.Done()
   108  		if err := server.Serve(l); err == nil {
   109  			t.Error(err)
   110  		}
   111  	}()
   112  	conn, _ := net.Dial(network, addr)
   113  	time.Sleep(time.Millisecond * 10)
   114  	conn.Close()
   115  	server.Close()
   116  	wg.Wait()
   117  }