github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/x/encoder_base64_test.go (about) 1 package x 2 3 import ( 4 "encoding/base64" 5 "reflect" 6 "sync" 7 "testing" 8 9 core "github.com/awesome-flow/flow/pkg/corev1alpha1" 10 coretest "github.com/awesome-flow/flow/pkg/corev1alpha1/test" 11 "github.com/awesome-flow/flow/pkg/util" 12 testutil "github.com/awesome-flow/flow/pkg/util/test" 13 flowtest "github.com/awesome-flow/flow/pkg/util/test/corev1alpha1" 14 ) 15 16 func TestEncoderBase64Receive(t *testing.T) { 17 nthreads := 4 18 ctx, err := coretest.NewContextWithConfig(map[string]interface{}{}) 19 if err != nil { 20 t.Fatalf("failed to create a context: %s", err) 21 } 22 if err := ctx.Start(); err != nil { 23 t.Fatalf("failed to start context: %s", err) 24 } 25 defer ctx.Stop() 26 27 enc, err := NewEncoderBase64("encoder", ctx, core.Params{}) 28 if err != nil { 29 t.Fatalf("failed to create a new encoder: %s", err) 30 } 31 act, err := flowtest.NewTestActor("test-actor", ctx, core.Params{}) 32 if err != nil { 33 t.Fatalf("failed to create a new test actor: %s", err) 34 } 35 if err := enc.Connect(nthreads, act); err != nil { 36 t.Fatalf("failed to connect test actor to encoder: %s", err) 37 } 38 mailbox := make(chan *core.Message, 1) 39 defer close(mailbox) 40 41 var lock sync.Mutex 42 act.(*flowtest.TestActor).OnReceive(func(msg *core.Message) { 43 lock.Lock() 44 defer lock.Unlock() 45 act.(*flowtest.TestActor).Flush() 46 mailbox <- msg 47 msg.Complete(core.MsgStatusDone) 48 }) 49 50 if err := util.ExecEnsure( 51 act.Start, 52 enc.Start, 53 ); err != nil { 54 t.Fatalf("failed to start actor: %s", err) 55 } 56 57 defer util.ExecEnsure( 58 enc.Stop, 59 act.Stop, 60 ) 61 62 data := testutil.RandBytes(testutil.RandInt(1024)) 63 datalen := len(data) 64 msg := core.NewMessage(data) 65 66 encoding := base64.StdEncoding 67 explen := encoding.EncodedLen(datalen) 68 expdata := make([]byte, explen) 69 encoding.Encode(expdata, data) 70 71 if err := enc.Receive(msg); err != nil { 72 t.Fatalf("failed to send a message: %s", err) 73 } 74 75 if status := msg.Await(); status != core.MsgStatusDone { 76 t.Fatalf("unexpected message status: got: %d, want: %d", status, core.MsgStatusDone) 77 } 78 gotmsg := <-mailbox 79 if !reflect.DeepEqual(expdata, gotmsg.Body()) { 80 t.Fatalf("unexpected message data: got: %q, want: %q", gotmsg.Body(), expdata) 81 } 82 }