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

     1  package actor
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	testutil "github.com/awesome-flow/flow/pkg/util/test"
    11  )
    12  
    13  func TestSinkHeadTCPConnect(t *testing.T) {
    14  	conn := newTestConn(
    15  		newTestAddr("tcp", "127.0.0.1:12345"),
    16  		newTestAddr("tcp", "127.0.0.1:23456"),
    17  	)
    18  	connbuilder := func(addr *net.TCPAddr, timeout time.Duration) (net.Conn, error) {
    19  		return conn, nil
    20  	}
    21  	addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:12345")
    22  	if err != nil {
    23  		t.Fatalf("failed to resolve tcp addr: %s", err)
    24  	}
    25  	head, err := NewSinkHeadTCP(addr)
    26  	if err != nil {
    27  		t.Fatalf("failed to initialize tcp sink: %s", err)
    28  	}
    29  	head.connbuilder = connbuilder
    30  	if err := head.Connect(); err != nil {
    31  		t.Fatalf("failed to connect: %s", err)
    32  	}
    33  	if !reflect.DeepEqual(head.conn, conn) {
    34  		t.Fatalf("unexpected head conn: got: %+v, want: %+v", head.conn, conn)
    35  	}
    36  }
    37  
    38  func TestSinkHeadTCPConnectFail(t *testing.T) {
    39  	wanterr := fmt.Errorf("expected error")
    40  	connbuilder := func(addr *net.TCPAddr, timeout time.Duration) (net.Conn, error) {
    41  		return nil, wanterr
    42  	}
    43  	addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:12345")
    44  	if err != nil {
    45  		t.Fatalf("failed to resolve tcp addr: %s", err)
    46  	}
    47  	head, err := NewSinkHeadTCP(addr)
    48  	if err != nil {
    49  		t.Fatalf("failed to initialize tcp sink: %s", err)
    50  	}
    51  	head.connbuilder = connbuilder
    52  	err = head.Connect()
    53  	if !reflect.DeepEqual(err, wanterr) {
    54  		t.Fatalf("unexpected error from head conn: got: %s, want: %s", err, wanterr)
    55  	}
    56  }
    57  
    58  func TestSinkHeadTCPWrite(t *testing.T) {
    59  	conn := newTestConn(
    60  		newTestAddr("tcp", "127.0.0.1:12345"),
    61  		newTestAddr("tcp", "127.0.0.1:23456"),
    62  	)
    63  	addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:12345")
    64  	if err != nil {
    65  		t.Fatalf("failed to resolve tcp addr: %s", err)
    66  	}
    67  	head, err := NewSinkHeadTCP(addr)
    68  	if err != nil {
    69  		t.Fatalf("failed to initialize tcp sink: %s", err)
    70  	}
    71  	head.conn = conn
    72  	data := testutil.RandBytes(1024)
    73  	n, err, rec := head.Write(data)
    74  	expn := len(data) + 2
    75  	if n != expn {
    76  		t.Fatalf("unexpected bytes written count: got: %d, want: %d", n, expn)
    77  	}
    78  	if err != nil {
    79  		t.Fatalf("unexpected error: %s", err)
    80  	}
    81  	if rec {
    82  		t.Fatalf("unexpected reconnect flag set to %t", rec)
    83  	}
    84  	expdata := append(data, '\r', '\n')
    85  	if !reflect.DeepEqual(conn.buf, expdata) {
    86  		t.Fatalf("unexpected data in conn buffer: got: %q, want: %q", string(conn.buf), string(expdata))
    87  	}
    88  }