github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/actor/receiver_tcp_test.go (about)

     1  package actor
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
     9  	coretest "github.com/awesome-flow/flow/pkg/corev1alpha1/test"
    10  	testutil "github.com/awesome-flow/flow/pkg/util/test"
    11  	flowtest "github.com/awesome-flow/flow/pkg/util/test/corev1alpha1"
    12  )
    13  
    14  func TestTCPHandleConn(t *testing.T) {
    15  	nthreads := 4
    16  	ctx, err := coretest.NewContextWithConfig(map[string]interface{}{})
    17  	if err != nil {
    18  		t.Fatalf("failed to create context: %s", err)
    19  	}
    20  	if err := ctx.Start(); err != nil {
    21  		t.Fatalf("failed to start context: %s", err)
    22  	}
    23  
    24  	type testStruct struct {
    25  		name    string
    26  		status  core.MsgStatus
    27  		sync    bool
    28  		expresp []byte
    29  	}
    30  
    31  	tests := make([]testStruct, 0, len(MsgStatusToTcpResp))
    32  	for status, resp := range MsgStatusToTcpResp {
    33  		tests = append(tests, testStruct{
    34  			name:    fmt.Sprintf("response with %s", sts2name(status)),
    35  			status:  status,
    36  			sync:    true,
    37  			expresp: resp,
    38  		})
    39  	}
    40  
    41  	t.Parallel()
    42  
    43  	for _, testCase := range tests {
    44  		t.Run(testCase.name, func(t *testing.T) {
    45  			conn := newTestConn(
    46  				newTestAddr("tcp", "127.0.0.1:12345"),
    47  				newTestAddr("tcp", "127.0.0.1:23456"),
    48  			)
    49  			params := core.Params{
    50  				"bind":     "127.0.0.1:12345",
    51  				"sync":     testCase.sync,
    52  				"buf_size": 8 * 1024,
    53  			}
    54  			rcv, err := NewReceiverTCP("receiver", ctx, params)
    55  			if err != nil {
    56  				t.Fatalf("failed to create receiver: %s", err)
    57  			}
    58  
    59  			peer, err := flowtest.NewTestActor("test-actor", ctx, core.Params{})
    60  			if err != nil {
    61  				t.Fatalf("failed to create test actor: %s", err)
    62  			}
    63  			if err := rcv.Connect(nthreads, peer); err != nil {
    64  				t.Fatalf("failed to connect test receiver: %s", err)
    65  			}
    66  
    67  			mailbox := make(chan *core.Message, 1)
    68  			defer close(mailbox)
    69  
    70  			peer.(*flowtest.TestActor).OnReceive(func(msg *core.Message) {
    71  				mailbox <- msg
    72  				msg.Complete(testCase.status)
    73  				peer.(*flowtest.TestActor).Flush()
    74  			})
    75  
    76  			body := testutil.RandBytes(8 * 1024)
    77  			if _, err := conn.Write(body); err != nil {
    78  				t.Fatalf("failed to write body data to test conn: %s", err)
    79  			}
    80  
    81  			rcv.(*ReceiverTCP).handleConn(conn)
    82  			if !reflect.DeepEqual(conn.buf, testCase.expresp) {
    83  				t.Fatalf("unexpected conn buf: got: %s, want: %s", string(conn.buf), testCase.expresp)
    84  			}
    85  
    86  			msg := <-mailbox
    87  			if !reflect.DeepEqual(msg.Body(), body) {
    88  				t.Fatalf("unexpected message body: got: %s, want: %s", string(msg.Body()), string(body))
    89  			}
    90  		})
    91  	}
    92  
    93  }