github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/common/transport/transport_test.go (about)

     1  package transport
     2  
     3  import (
     4  	"github.com/nyan233/littlerpc/core/common/logger"
     5  	"log"
     6  	"testing"
     7  )
     8  
     9  func tcpOnMessage(conn ConnAdapter, data []byte) {
    10  	_, _ = conn.Write(data)
    11  }
    12  
    13  func tcpClientOnMessage(conn ConnAdapter, data []byte) {
    14  	log.Println(string(data))
    15  }
    16  
    17  func TestTcpTransport(t *testing.T) {
    18  	// 关闭服务器烦人的日志
    19  	logger.SetOpenLogger(false)
    20  	builder := NewNBioTcpServer(NetworkServerConfig{
    21  		Addrs: []string{"127.0.0.1:9090", "127.0.0.2:9090"},
    22  	})
    23  	eventD := builder.EventDriveInter()
    24  	eventD.OnMessage(tcpOnMessage)
    25  	server := builder.Server()
    26  	err := server.Start()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer server.Stop()
    31  	clientBuilder := NewNBioTcpClient()
    32  	clientBuilder.EventDriveInter().OnMessage(tcpClientOnMessage)
    33  	err = clientBuilder.Client().Start()
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	defer clientBuilder.Client().Stop()
    38  	conn, err := clientBuilder.Client().NewConn(NetworkClientConfig{
    39  		ServerAddr: "127.0.0.1:9090",
    40  		KeepAlive:  false,
    41  	})
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	_, err = conn.Write([]byte("hello world!"))
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  }
    50  
    51  func wsOnMessage(conn ConnAdapter, data []byte) {
    52  	_, _ = conn.Write(data)
    53  }
    54  
    55  func TestWebSocketTransport(t *testing.T) {
    56  	// 关闭服务器烦人的日志
    57  	logger.SetOpenLogger(false)
    58  	builder := NewNBioWebsocketServer(NetworkServerConfig{
    59  		Addrs:     []string{"127.0.0.1:8083", "127.0.0.2:8054"},
    60  		KeepAlive: false,
    61  	})
    62  	builder.EventDriveInter().OnMessage(wsOnMessage)
    63  	server := builder.Server()
    64  	err := server.Start()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	defer server.Stop()
    69  	clientBuilder := NewNBioWebsocketClient()
    70  	clientBuilder.EventDriveInter().OnMessage(tcpClientOnMessage)
    71  	clientBuilder.Client().Start()
    72  	defer clientBuilder.Client().Stop()
    73  	conn, err := clientBuilder.Client().NewConn(NetworkClientConfig{
    74  		ServerAddr: "127.0.0.1:8083",
    75  		KeepAlive:  false,
    76  	})
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	_, err = conn.Write([]byte("hello world!"))
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  }
    85  
    86  func TestStdTcpTransport(t *testing.T) {
    87  	// 关闭服务器烦人的日志
    88  	logger.SetOpenLogger(false)
    89  	builder := NewStdTcpServer(NetworkServerConfig{
    90  		Addrs: []string{"127.0.0.1:9090", "127.0.0.2:9090"},
    91  	})
    92  	eventD := builder.EventDriveInter()
    93  	eventD.OnMessage(tcpOnMessage)
    94  	server := builder.Server()
    95  	err := server.Start()
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	defer server.Stop()
   100  	clientBuilder := NewStdTcpClient()
   101  	clientBuilder.EventDriveInter().OnMessage(tcpClientOnMessage)
   102  	err = clientBuilder.Client().Start()
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	defer clientBuilder.Client().Stop()
   107  	conn, err := clientBuilder.Client().NewConn(NetworkClientConfig{
   108  		ServerAddr: "127.0.0.1:9090",
   109  		KeepAlive:  false,
   110  	})
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	_, err = conn.Write([]byte("hello world!"))
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  }