github.com/ergo-services/ergo@v1.999.224/tests/tcp_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"testing"
     7  
     8  	"github.com/ergo-services/ergo"
     9  	"github.com/ergo-services/ergo/etf"
    10  	"github.com/ergo-services/ergo/gen"
    11  	"github.com/ergo-services/ergo/node"
    12  )
    13  
    14  var (
    15  	resChan = make(chan interface{}, 2)
    16  )
    17  
    18  type testTCPHandler struct {
    19  	gen.TCPHandler
    20  }
    21  
    22  type messageTestTCPConnect struct{}
    23  type messageTestTCPStatusNext struct {
    24  	left  int
    25  	await int
    26  }
    27  
    28  func (r *testTCPHandler) HandleConnect(process *gen.TCPHandlerProcess, conn *gen.TCPConnection) gen.TCPHandlerStatus {
    29  	resChan <- messageTestTCPConnect{}
    30  	return gen.TCPHandlerStatusOK
    31  }
    32  
    33  func (r *testTCPHandler) HandlePacket(process *gen.TCPHandlerProcess, packet []byte, conn *gen.TCPConnection) (int, int, gen.TCPHandlerStatus) {
    34  	l := len(packet)
    35  	//fmt.Println("GOT", process.Self(), packet, l, "bytes", l%10)
    36  	if l < 10 {
    37  		resChan <- messageTestTCPStatusNext{
    38  			left:  l,
    39  			await: 10 - l,
    40  		}
    41  		return l, 10 - l, gen.TCPHandlerStatusOK
    42  	}
    43  	if l > 10 {
    44  		resChan <- messageTestTCPStatusNext{
    45  			left:  l % 10,
    46  			await: 10 - (l % 10),
    47  		}
    48  		return l % 10, 10 - (l % 10), gen.TCPHandlerStatusOK
    49  	}
    50  	resChan <- packet
    51  	return 0, 0, gen.TCPHandlerStatusOK
    52  }
    53  
    54  type testTCPServer struct {
    55  	gen.TCP
    56  }
    57  
    58  func (ts *testTCPServer) InitTCP(process *gen.TCPProcess, args ...etf.Term) (gen.TCPOptions, error) {
    59  	var options gen.TCPOptions
    60  	options.Handler = &testTCPHandler{}
    61  	options.Port = 10101
    62  
    63  	return options, nil
    64  }
    65  
    66  func TestTCP(t *testing.T) {
    67  	fmt.Printf("\n=== Test TCP Server\n")
    68  	fmt.Printf("Starting nodes: nodeTCP1@localhost: ")
    69  	node1, err := ergo.StartNode("nodeTCP1@localhost", "cookies", node.Options{})
    70  	defer node1.Stop()
    71  	if err != nil {
    72  		t.Fatal("can't start node", err)
    73  	} else {
    74  		fmt.Println("OK")
    75  	}
    76  
    77  	fmt.Printf("...starting process (gen.TCP): ")
    78  	tcpProcess, err := node1.Spawn("tcp", gen.ProcessOptions{}, &testTCPServer{})
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	fmt.Println("OK")
    83  
    84  	fmt.Printf("...makeing a new connection: ")
    85  	conn, err := net.Dial("tcp", "localhost:10101")
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	waitForResultWithValue(t, resChan, messageTestTCPConnect{})
    90  
    91  	fmt.Printf("...send/recv data (10 bytes as 1 logic dataframe): ")
    92  	testData1 := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    93  	if _, err := conn.Write(testData1); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	waitForResultWithValue(t, resChan, testData1)
    97  
    98  	fmt.Printf("...send/recv data (7 bytes as a part of logic dataframe): ")
    99  	testData2 := []byte{11, 12, 13, 14, 15, 16, 17}
   100  	if _, err := conn.Write(testData2); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	value := messageTestTCPStatusNext{
   105  		left:  7,
   106  		await: 3,
   107  	}
   108  
   109  	waitForResultWithValue(t, resChan, value)
   110  
   111  	fmt.Printf("...send/recv data (5 bytes, must be 1 logic dataframe + extra 2 bytes): ")
   112  	testData2 = []byte{18, 19, 20, 21, 22}
   113  	if _, err := conn.Write(testData2); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	value = messageTestTCPStatusNext{
   117  		left:  2,
   118  		await: 8,
   119  	}
   120  	waitForResultWithValue(t, resChan, value)
   121  
   122  	fmt.Printf("...send/recv data (8 bytes, must be 1 logic dataframe): ")
   123  	testData2 = []byte{23, 24, 25, 26, 27, 28, 29, 30}
   124  	if _, err := conn.Write(testData2); err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	waitForResultWithValue(t, resChan, []byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30})
   128  
   129  	tcpProcess.Kill()
   130  	tcpProcess.Wait()
   131  
   132  	fmt.Printf("...stopping process (gen.TCP): ")
   133  	if _, err := net.Dial("tcp", "localhost:10101"); err == nil {
   134  		t.Fatal("error must be here")
   135  	}
   136  	fmt.Println("OK")
   137  }