github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/x/decoder_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 TestDecoderBase64Receive(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 dec, err := NewDecoderBase64("decoder", ctx, core.Params{}) 28 if err != nil { 29 t.Fatalf("failed to create a new decoder: %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 := dec.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 dec.Start, 53 ); err != nil { 54 t.Fatalf("failed to start actor: %s", err) 55 } 56 57 defer util.ExecEnsure( 58 dec.Stop, 59 act.Stop, 60 ) 61 62 data := testutil.RandBytes(testutil.RandInt(1024)) 63 datalen := len(data) 64 65 encoding := base64.StdEncoding 66 enclen := encoding.EncodedLen(datalen) 67 encdata := make([]byte, enclen) 68 encoding.Encode(encdata, data) 69 70 msg := core.NewMessage(encdata) 71 72 if err := dec.Receive(msg); err != nil { 73 t.Fatalf("failed to send a message: %s", err) 74 } 75 76 if status := msg.Await(); status != core.MsgStatusDone { 77 t.Fatalf("unexpected message status: got: %d, want: %d", status, core.MsgStatusDone) 78 } 79 gotmsg := <-mailbox 80 if !reflect.DeepEqual(data, gotmsg.Body()) { 81 t.Fatalf("unexpected message data: got: %q, want: %q", gotmsg.Body(), data) 82 } 83 }