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

     1  package actor
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
     8  	coretest "github.com/awesome-flow/flow/pkg/corev1alpha1/test"
     9  	testutil "github.com/awesome-flow/flow/pkg/util/test"
    10  	flowtest "github.com/awesome-flow/flow/pkg/util/test/corev1alpha1"
    11  )
    12  
    13  func TestUDPHandleConn(t *testing.T) {
    14  	nthreads := 4
    15  	ctx, err := coretest.NewContextWithConfig(map[string]interface{}{
    16  		"system.maxprocs": nthreads,
    17  	})
    18  	if err != nil {
    19  		t.Fatalf("failed to create context: %d", err)
    20  	}
    21  
    22  	rcv, err := NewReceiverUDP("receiver", ctx, core.Params{
    23  		"bind": "127.0.0.1:12345",
    24  	})
    25  	if err != nil {
    26  		t.Fatalf("failed to create UDP receiver: %s", err)
    27  	}
    28  
    29  	peer, err := flowtest.NewTestActor("test-actor", ctx, core.Params{})
    30  	if err != nil {
    31  		t.Fatalf("failed to create test actor: %s", err)
    32  	}
    33  	mailbox := make(chan *core.Message)
    34  	peer.(*flowtest.TestActor).OnReceive(func(msg *core.Message) {
    35  		msg.Complete(core.MsgStatusDone)
    36  		mailbox <- msg
    37  		peer.(*flowtest.TestActor).Flush()
    38  	})
    39  
    40  	if err := rcv.Connect(nthreads, peer); err != nil {
    41  		t.Fatalf("failed to connect test actor: %s", err)
    42  	}
    43  
    44  	conn := newTestConn(
    45  		newTestAddr("tcp", "127.0.0.1:12345"),
    46  		newTestAddr("tcp", "127.0.0.1:23456"),
    47  	)
    48  	body := testutil.RandBytes(1024)
    49  	if _, err := conn.Write(body); err != nil {
    50  		t.Fatalf("failed to write body data to test conn: %s", err)
    51  	}
    52  
    53  	rcv.(*ReceiverUDP).handleConn(conn)
    54  	msg := <-mailbox
    55  
    56  	if !reflect.DeepEqual(msg.Body(), body) {
    57  		t.Fatalf("unexpected mesage body: got: %s, want: %s", msg.Body(), body)
    58  	}
    59  }