github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/batch_test.go (about) 1 package processor 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/Jeffail/benthos/v3/lib/condition" 8 "github.com/Jeffail/benthos/v3/lib/log" 9 "github.com/Jeffail/benthos/v3/lib/message" 10 "github.com/Jeffail/benthos/v3/lib/metrics" 11 ) 12 13 func TestBatchBasic(t *testing.T) { 14 conf := NewConfig() 15 conf.Batch.Count = 2 16 conf.Batch.ByteSize = 0 17 18 testLog := log.Noop() 19 proc, err := NewBatch(conf, nil, testLog, metrics.Noop()) 20 if err != nil { 21 t.Error(err) 22 return 23 } 24 25 exp := [][]byte{[]byte("foo"), []byte("bar")} 26 27 msgs, res := proc.ProcessMessage(message.New(exp)) 28 if len(msgs) != 1 { 29 t.Error("Expected success") 30 } 31 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 32 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 33 } 34 if res != nil { 35 t.Error("Expected nil res") 36 } 37 } 38 39 func TestBatchTwoParts(t *testing.T) { 40 conf := NewConfig() 41 conf.Batch.ByteSize = 6 42 43 testLog := log.Noop() 44 proc, err := NewBatch(conf, nil, testLog, metrics.Noop()) 45 if err != nil { 46 t.Error(err) 47 return 48 } 49 50 exp := [][]byte{[]byte("foo"), []byte("bar")} 51 52 msgs, res := proc.ProcessMessage(message.New(exp)) 53 if len(msgs) != 1 { 54 t.Error("Expected success") 55 } 56 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 57 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 58 } 59 if res != nil { 60 t.Error("Expected nil res") 61 } 62 } 63 64 func TestBatchLotsOfParts(t *testing.T) { 65 conf := NewConfig() 66 conf.Batch.ByteSize = 1 67 68 testLog := log.Noop() 69 proc, err := NewBatch(conf, nil, testLog, metrics.Noop()) 70 if err != nil { 71 t.Error(err) 72 return 73 } 74 75 input := [][]byte{ 76 []byte("foo"), []byte("bar"), []byte("bar2"), 77 []byte("bar3"), []byte("bar4"), []byte("bar5"), 78 } 79 80 msgs, res := proc.ProcessMessage(message.New(input)) 81 if len(msgs) != 1 { 82 t.Error("Expected success") 83 } 84 if !reflect.DeepEqual(input, message.GetAllBytes(msgs[0])) { 85 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), input) 86 } 87 if res != nil { 88 t.Error("Expected nil res") 89 } 90 } 91 92 func TestBatchTwoSingleParts(t *testing.T) { 93 conf := NewConfig() 94 conf.Batch.ByteSize = 5 95 96 testLog := log.Noop() 97 proc, err := NewBatch(conf, nil, testLog, metrics.Noop()) 98 if err != nil { 99 t.Error(err) 100 return 101 } 102 103 exp := [][]byte{[]byte("foo1"), []byte("bar1")} 104 105 inMsg := message.New([][]byte{exp[0]}) 106 inMsg.Get(0).Metadata().Set("foo", "bar1") 107 108 msgs, res := proc.ProcessMessage(inMsg) 109 if len(msgs) != 0 { 110 t.Error("Expected fail on one part") 111 } 112 if !res.SkipAck() { 113 t.Error("Expected skip ack") 114 } 115 116 inMsg = message.New([][]byte{exp[1]}) 117 inMsg.Get(0).Metadata().Set("foo", "bar2") 118 119 msgs, res = proc.ProcessMessage(inMsg) 120 if len(msgs) != 1 { 121 t.Error("Expected success") 122 } 123 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 124 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 125 } 126 if exp, act := "bar1", msgs[0].Get(0).Metadata().Get("foo"); exp != act { 127 t.Errorf("Wrong metadata: %v != %v", act, exp) 128 } 129 if exp, act := "bar2", msgs[0].Get(1).Metadata().Get("foo"); exp != act { 130 t.Errorf("Wrong metadata: %v != %v", act, exp) 131 } 132 if res != nil { 133 t.Error("Expected nil res") 134 } 135 136 exp = [][]byte{[]byte("foo2"), []byte("bar2")} 137 138 msgs, res = proc.ProcessMessage(message.New([][]byte{exp[0]})) 139 if len(msgs) != 0 { 140 t.Error("Expected fail on one part") 141 } 142 if !res.SkipAck() { 143 t.Error("Expected skip ack") 144 } 145 146 msgs, res = proc.ProcessMessage(message.New([][]byte{exp[1]})) 147 if len(msgs) != 1 { 148 t.Error("Expected success") 149 } 150 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 151 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 152 } 153 if res != nil { 154 t.Error("Expected nil res") 155 } 156 } 157 158 func TestBatchTwoDiffParts(t *testing.T) { 159 conf := NewConfig() 160 conf.Batch.ByteSize = 5 161 162 testLog := log.Noop() 163 proc, err := NewBatch(conf, nil, testLog, metrics.Noop()) 164 if err != nil { 165 t.Error(err) 166 return 167 } 168 169 input := [][]byte{[]byte("foo1"), []byte("bar1")} 170 exp := [][]byte{[]byte("foo1"), []byte("bar1"), []byte("foo1")} 171 172 msgs, res := proc.ProcessMessage(message.New([][]byte{input[0]})) 173 if len(msgs) != 0 { 174 t.Error("Expected fail on one part") 175 } 176 if !res.SkipAck() { 177 t.Error("Expected skip ack") 178 } 179 180 msgs, res = proc.ProcessMessage(message.New([][]byte{input[1], input[0]})) 181 if len(msgs) != 1 { 182 t.Error("Expected success") 183 } 184 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 185 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 186 } 187 if res != nil { 188 t.Error("Expected nil res") 189 } 190 191 exp = [][]byte{[]byte("bar1"), []byte("foo1")} 192 193 msgs, res = proc.ProcessMessage(message.New([][]byte{input[1], input[0]})) 194 if len(msgs) != 1 { 195 t.Error("Expected success") 196 } 197 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 198 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 199 } 200 if res != nil { 201 t.Error("Expected nil res") 202 } 203 204 exp = [][]byte{[]byte("bar1"), []byte("foo1"), []byte("bar1")} 205 206 msgs, res = proc.ProcessMessage(message.New([][]byte{input[1], input[0], input[1]})) 207 if len(msgs) != 1 { 208 t.Error("Expected success") 209 } 210 if !reflect.DeepEqual(exp, message.GetAllBytes(msgs[0])) { 211 t.Errorf("Wrong result: %s != %s", message.GetAllBytes(msgs[0]), exp) 212 } 213 if res != nil { 214 t.Error("Expected nil res") 215 } 216 } 217 218 func TestBatchCondition(t *testing.T) { 219 condConf := condition.NewConfig() 220 condConf.Type = "text" 221 condConf.Text.Operator = "contains" 222 condConf.Text.Arg = "end_batch" 223 224 conf := NewConfig() 225 conf.Batch.ByteSize = 1000 226 conf.Batch.Condition = condConf 227 228 testLog := log.Noop() 229 proc, err := NewBatch(conf, nil, testLog, metrics.Noop()) 230 if err != nil { 231 t.Error(err) 232 return 233 } 234 235 msgs, res := proc.ProcessMessage(message.New([][]byte{[]byte("foo")})) 236 if len(msgs) != 0 { 237 t.Error("Expected no batch") 238 } 239 if !res.SkipAck() { 240 t.Error("Expected skip ack") 241 } 242 243 msgs, res = proc.ProcessMessage(message.New([][]byte{[]byte("bar")})) 244 if len(msgs) != 0 { 245 t.Error("Expected no batch") 246 } 247 if !res.SkipAck() { 248 t.Error("Expected skip ack") 249 } 250 251 msgs, _ = proc.ProcessMessage(message.New([][]byte{[]byte("baz: end_batch")})) 252 if len(msgs) != 1 { 253 t.Fatal("Expected batch") 254 } 255 256 exp := [][]byte{ 257 []byte("foo"), 258 []byte("bar"), 259 []byte("baz: end_batch"), 260 } 261 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(act, exp) { 262 t.Errorf("Wrong batch contents: %s != %s", act, exp) 263 } 264 }