github.com/Jeffail/benthos/v3@v3.65.0/lib/message/locked_message_test.go (about)

     1  package message
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestLockedMessageGet(t *testing.T) {
     9  	m := Lock(New([][]byte{
    10  		[]byte("hello"),
    11  		[]byte("world"),
    12  		[]byte("12345"),
    13  	}), 0)
    14  
    15  	exp := []byte("hello")
    16  	if act := m.Get(0).Get(); !reflect.DeepEqual(exp, act) {
    17  		t.Errorf("Messages not equal: %s != %s", exp, act)
    18  	}
    19  
    20  	m = Lock(New([][]byte{
    21  		[]byte("hello"),
    22  		[]byte("world"),
    23  		[]byte("12345"),
    24  	}), 1)
    25  
    26  	exp = []byte("world")
    27  	if act := m.Get(0).Get(); !reflect.DeepEqual(exp, act) {
    28  		t.Errorf("Messages not equal: %s != %s", exp, act)
    29  	}
    30  }
    31  
    32  func TestLockedMessageCopy(t *testing.T) {
    33  	root := New([][]byte{
    34  		[]byte("hello"),
    35  		[]byte("world"),
    36  		[]byte("12345"),
    37  	})
    38  	m := Lock(root, 1)
    39  	copied := m.Copy()
    40  	root.Get(1).Get()[2] = '@'
    41  
    42  	exp := []byte("wo@ld")
    43  	if act := copied.Get(0).Get(); !reflect.DeepEqual(exp, act) {
    44  		t.Errorf("Messages not equal: %s != %s", exp, act)
    45  	}
    46  
    47  	root.Get(1).Set([]byte("world"))
    48  	exp = []byte("wo@ld")
    49  	if act := copied.Get(0).Get(); !reflect.DeepEqual(exp, act) {
    50  		t.Errorf("Messages not equal: %s != %s", exp, act)
    51  	}
    52  }
    53  
    54  func TestLockedMessageDeepCopy(t *testing.T) {
    55  	root := New([][]byte{
    56  		[]byte("hello"),
    57  		[]byte("world"),
    58  		[]byte("12345"),
    59  	})
    60  	m := Lock(root, 1)
    61  	copied := m.DeepCopy()
    62  	root.Get(1).Get()[2] = '@'
    63  
    64  	exp := []byte("world")
    65  	if act := copied.Get(0).Get(); !reflect.DeepEqual(exp, act) {
    66  		t.Errorf("Messages not equal: %s != %s", exp, act)
    67  	}
    68  }
    69  
    70  func TestLockedMessageGetAll(t *testing.T) {
    71  	m := Lock(New([][]byte{
    72  		[]byte("hello"),
    73  		[]byte("world"),
    74  		[]byte("12345"),
    75  	}), 0)
    76  
    77  	exp := [][]byte{
    78  		[]byte("hello"),
    79  	}
    80  	if act := GetAllBytes(m); !reflect.DeepEqual(exp, act) {
    81  		t.Errorf("Messages not equal: %s != %s", exp, act)
    82  	}
    83  
    84  	m = Lock(New([][]byte{
    85  		[]byte("hello"),
    86  		[]byte("world"),
    87  		[]byte("12345"),
    88  	}), 1)
    89  
    90  	exp = [][]byte{
    91  		[]byte("world"),
    92  	}
    93  	if act := GetAllBytes(m); !reflect.DeepEqual(exp, act) {
    94  		t.Errorf("Messages not equal: %s != %s", exp, act)
    95  	}
    96  }
    97  
    98  func TestLockNew(t *testing.T) {
    99  	m := Lock(New(nil), 0)
   100  	if act := m.Len(); act > 0 {
   101  		t.Errorf("New returned more than zero message parts: %v", act)
   102  	}
   103  }
   104  
   105  func TestLockedMessageJSONGet(t *testing.T) {
   106  	msg := Lock(New(
   107  		[][]byte{[]byte(`{"foo":{"bar":"baz"}}`)},
   108  	), 0)
   109  
   110  	if _, err := msg.Get(1).JSON(); err != ErrMessagePartNotExist {
   111  		t.Errorf("Wrong error returned on bad part: %v != %v", err, ErrMessagePartNotExist)
   112  	}
   113  
   114  	jObj, err := msg.Get(0).JSON()
   115  	if err != nil {
   116  		t.Error(err)
   117  	}
   118  
   119  	exp := map[string]interface{}{
   120  		"foo": map[string]interface{}{
   121  			"bar": "baz",
   122  		},
   123  	}
   124  	if act := jObj; !reflect.DeepEqual(act, exp) {
   125  		t.Errorf("Wrong output from jsonGet: %v != %v", act, exp)
   126  	}
   127  }
   128  
   129  /*
   130  func TestLockedMessageConditionCaching(t *testing.T) {
   131  	msg := Lock(New([][]byte{
   132  		[]byte(`foo`),
   133  	}), 0)
   134  
   135  	dummyCond1 := &dummyCond{
   136  		call: func(m types.Message) bool {
   137  			return string(m.Get(0).Get()) == "foo"
   138  		},
   139  	}
   140  	dummyCond2 := &dummyCond{
   141  		call: func(m types.Message) bool {
   142  			return string(m.Get(0).Get()) == "bar"
   143  		},
   144  	}
   145  
   146  	if !msg.LazyCondition("1", dummyCond1) {
   147  		t.Error("Wrong result from cond 1")
   148  	}
   149  	if !msg.LazyCondition("1", dummyCond1) {
   150  		t.Error("Wrong result from cached cond 1")
   151  	}
   152  	if !msg.LazyCondition("1", dummyCond2) {
   153  		t.Error("Wrong result from cached cond 1 with cond 2")
   154  	}
   155  
   156  	if msg.LazyCondition("2", dummyCond2) {
   157  		t.Error("Wrong result from cond 2")
   158  	}
   159  	if msg.LazyCondition("2", dummyCond2) {
   160  		t.Error("Wrong result from cached cond 2")
   161  	}
   162  
   163  	if exp, act := 1, dummyCond1.calls; exp != act {
   164  		t.Errorf("Wrong count of calls for cond 1: %v != %v", act, exp)
   165  	}
   166  	if exp, act := 1, dummyCond2.calls; exp != act {
   167  		t.Errorf("Wrong count of calls for cond 2: %v != %v", act, exp)
   168  	}
   169  }
   170  */