github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/actor/sink_head_file_test.go (about) 1 package actor 2 3 import ( 4 "bytes" 5 "io" 6 "reflect" 7 "testing" 8 9 testutil "github.com/awesome-flow/flow/pkg/util/test" 10 ) 11 12 type testWriteCloser struct { 13 bytes.Buffer 14 } 15 16 var _ (io.WriteCloser) = (*testWriteCloser)(nil) 17 18 func NewTestWriteCloser() io.WriteCloser { 19 return &testWriteCloser{} 20 } 21 22 func (wc *testWriteCloser) Close() error { 23 return nil 24 } 25 26 func TestSinkHeadFileConnect(t *testing.T) { 27 out := NewTestWriteCloser() 28 opener := func(string) (io.WriteCloser, error) { 29 return out, nil 30 } 31 head, err := NewSinkHeadFile("/dev/null") 32 if err != nil { 33 t.Fatalf("failed to create file sink head: %s", err) 34 } 35 head.Opener = opener 36 if err := head.Connect(); err != nil { 37 t.Fatalf("failed to connect file sink head: %s", err) 38 } 39 if !reflect.DeepEqual(head.out, out) { 40 t.Fatalf("unexpected head out: got: %+v, want: %+v", head.out, out) 41 } 42 } 43 44 func TestSinkHeadFileWrite(t *testing.T) { 45 out := NewTestWriteCloser() 46 opener := func(string) (io.WriteCloser, error) { 47 return out, nil 48 } 49 head, err := NewSinkHeadFile("/dev/null") 50 if err != nil { 51 t.Fatalf("failed to create file sink head: %s", err) 52 } 53 head.Opener = opener 54 if err := head.Connect(); err != nil { 55 t.Fatalf("failed to connect file sink head: %s", err) 56 } 57 data := testutil.RandBytes(1024) 58 n, err, rec := head.Write(data) 59 if err != nil { 60 t.Fatalf("unexpected error: %s", err) 61 } 62 if n != len(data)+2 { 63 t.Fatalf("unexpected write bytes length: got: %d, want: %d", n, len(data)+2) 64 } 65 if rec { 66 t.Fatalf("unexpected reconnect flag: got: %t, want: false", rec) 67 } 68 gotdata := out.(*testWriteCloser).Bytes() 69 wantdata := append(data, []byte("\r\n")...) 70 if !reflect.DeepEqual(gotdata, wantdata) { 71 t.Fatalf("unexpected buf contents: got: %q, want: %q", gotdata, wantdata) 72 } 73 }