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

     1  package corev1alpha1
     2  
     3  import (
     4  	"math/rand"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	testutil "github.com/awesome-flow/flow/pkg/util/test"
    10  )
    11  
    12  func TestNewMessage(t *testing.T) {
    13  	body := testutil.RandBytes(1024)
    14  	msg := NewMessage(body)
    15  	if !reflect.DeepEqual(msg.body, body) {
    16  		t.Fatalf("unexpected message body: got: %s, want: %s", msg.body, body)
    17  	}
    18  	if msg.status != MsgStatusNew {
    19  		t.Fatalf("unexpected message status: got %d, want: %d", msg.status, MsgStatusNew)
    20  	}
    21  }
    22  
    23  func TestNewMessageCopyBody(t *testing.T) {
    24  	body := testutil.RandBytes(1024)
    25  	msg := NewMessage(body)
    26  	// RandBytes guarantees to place a non-zero byte at any pos
    27  	body[rand.Intn(len(body))] = '\x00'
    28  	if reflect.DeepEqual(msg.body, body) {
    29  		t.Fatalf("detected a side affect of the original body modification")
    30  	}
    31  }
    32  
    33  func TestAwait(t *testing.T) {
    34  	done := make(chan struct{})
    35  	msg := NewMessage(testutil.RandBytes(1024))
    36  	go func() {
    37  		msg.Await()
    38  		close(done)
    39  	}()
    40  	msg.Complete(MsgStatusDone)
    41  	select {
    42  	case <-done:
    43  	case <-time.After(time.Second):
    44  		t.Fatalf("timed out to await")
    45  	}
    46  }
    47  
    48  func TestAwaitChan(t *testing.T) {
    49  	msg := NewMessage(testutil.RandBytes(1024))
    50  	go func() {
    51  		msg.Complete(MsgStatusDone)
    52  	}()
    53  	select {
    54  	case <-msg.AwaitChan():
    55  	case <-time.After(time.Second):
    56  		t.Fatalf("timed out to await chan")
    57  	}
    58  }
    59  
    60  func TestMetaKeys(t *testing.T) {
    61  	msg := NewMessage(testutil.RandBytes(1024))
    62  	meta := make(map[string]interface{})
    63  	for i, max := 0, testutil.RandInt(128); i < max; i++ {
    64  		k := string(testutil.RandBytes(1024))
    65  		v := testutil.RandBytes(1024)
    66  		meta[k] = v
    67  		msg.SetMeta(k, v)
    68  	}
    69  	for k, v := range meta {
    70  		if msgv, _ := msg.Meta(k); !reflect.DeepEqual(msgv, v) {
    71  			t.Fatalf("unexpected value in msg meta: %q, want: %q", msgv, v)
    72  		}
    73  	}
    74  }
    75  
    76  func TestCopy(t *testing.T) {
    77  	msg := NewMessage(testutil.RandBytes(1024))
    78  	for i, max := 0, testutil.RandInt(128); i < max; i++ {
    79  		k := string(testutil.RandBytes(1024))
    80  		v := testutil.RandBytes(1024)
    81  		msg.SetMeta(k, v)
    82  	}
    83  	cpmsg := msg.Copy()
    84  	if !reflect.DeepEqual(msg.Body(), cpmsg.Body()) {
    85  		t.Fatalf("unexpected message body: %q, want: %q", cpmsg.Body(), msg.Body())
    86  	}
    87  	if !reflect.DeepEqual(cpmsg.meta, msg.meta) {
    88  		t.Fatalf("unexpected message meta: %v, want: %v", cpmsg.meta, msg.meta)
    89  	}
    90  }