github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/for_each_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  //------------------------------------------------------------------------------
    14  
    15  func TestForEachEmpty(t *testing.T) {
    16  	conf := NewConfig()
    17  	conf.Type = "for_each"
    18  
    19  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	exp := [][]byte{
    25  		[]byte("foo bar baz"),
    26  	}
    27  	msgs, res := proc.ProcessMessage(message.New(exp))
    28  	if res != nil {
    29  		t.Fatal(res.Error())
    30  	}
    31  
    32  	if len(msgs) != 1 {
    33  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
    34  	}
    35  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
    36  		t.Errorf("Wrong results: %s != %s", act, exp)
    37  	}
    38  }
    39  
    40  func TestForEachBasic(t *testing.T) {
    41  	encodeConf := NewConfig()
    42  	encodeConf.Type = "encode"
    43  	encodeConf.Encode.Parts = []int{0}
    44  
    45  	conf := NewConfig()
    46  	conf.Type = "for_each"
    47  	conf.ForEach = append(conf.ForEach, encodeConf)
    48  
    49  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	parts := [][]byte{
    55  		[]byte("foo bar baz"),
    56  		[]byte("1 2 3 4"),
    57  		[]byte("hello foo world"),
    58  	}
    59  	exp := [][]byte{
    60  		[]byte("Zm9vIGJhciBiYXo="),
    61  		[]byte("MSAyIDMgNA=="),
    62  		[]byte("aGVsbG8gZm9vIHdvcmxk"),
    63  	}
    64  	msgs, res := proc.ProcessMessage(message.New(parts))
    65  	if res != nil {
    66  		t.Fatal(res.Error())
    67  	}
    68  
    69  	if len(msgs) != 1 {
    70  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
    71  	}
    72  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
    73  		t.Errorf("Wrong results: %s != %s", act, exp)
    74  	}
    75  }
    76  
    77  func TestForEachFilterSome(t *testing.T) {
    78  	cond := condition.NewConfig()
    79  	cond.Type = "text"
    80  	cond.Text.Arg = "foo"
    81  	cond.Text.Operator = "contains"
    82  
    83  	filterConf := NewConfig()
    84  	filterConf.Type = "filter"
    85  	filterConf.Filter.Config = cond
    86  
    87  	conf := NewConfig()
    88  	conf.Type = "for_each"
    89  	conf.ForEach = append(conf.ForEach, filterConf)
    90  
    91  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	parts := [][]byte{
    97  		[]byte("foo bar baz"),
    98  		[]byte("1 2 3 4"),
    99  		[]byte("hello foo world"),
   100  	}
   101  	exp := [][]byte{
   102  		[]byte("foo bar baz"),
   103  		[]byte("hello foo world"),
   104  	}
   105  	msgs, res := proc.ProcessMessage(message.New(parts))
   106  	if res != nil {
   107  		t.Fatal(res.Error())
   108  	}
   109  
   110  	if len(msgs) != 1 {
   111  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   112  	}
   113  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
   114  		t.Errorf("Wrong results: %s != %s", act, exp)
   115  	}
   116  }
   117  
   118  func TestForEachMultiProcs(t *testing.T) {
   119  	encodeConf := NewConfig()
   120  	encodeConf.Type = "encode"
   121  	encodeConf.Encode.Parts = []int{0}
   122  
   123  	cond := condition.NewConfig()
   124  	cond.Type = "text"
   125  	cond.Text.Arg = "foo"
   126  	cond.Text.Operator = "contains"
   127  
   128  	filterConf := NewConfig()
   129  	filterConf.Type = "filter"
   130  	filterConf.Filter.Config = cond
   131  
   132  	conf := NewConfig()
   133  	conf.Type = "for_each"
   134  	conf.ForEach = append(conf.ForEach, filterConf, encodeConf)
   135  
   136  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  
   141  	parts := [][]byte{
   142  		[]byte("foo bar baz"),
   143  		[]byte("1 2 3 4"),
   144  		[]byte("hello foo world"),
   145  	}
   146  	exp := [][]byte{
   147  		[]byte("Zm9vIGJhciBiYXo="),
   148  		[]byte("aGVsbG8gZm9vIHdvcmxk"),
   149  	}
   150  	msgs, res := proc.ProcessMessage(message.New(parts))
   151  	if res != nil {
   152  		t.Fatal(res.Error())
   153  	}
   154  
   155  	if len(msgs) != 1 {
   156  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   157  	}
   158  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
   159  		t.Errorf("Wrong results: %s != %s", act, exp)
   160  	}
   161  }
   162  
   163  func TestForEachFilterAll(t *testing.T) {
   164  	cond := condition.NewConfig()
   165  	cond.Type = "text"
   166  	cond.Text.Arg = "foo"
   167  	cond.Text.Operator = "contains"
   168  
   169  	filterConf := NewConfig()
   170  	filterConf.Type = "filter"
   171  	filterConf.Filter.Config = cond
   172  
   173  	conf := NewConfig()
   174  	conf.Type = "for_each"
   175  	conf.ForEach = append(conf.ForEach, filterConf)
   176  
   177  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  
   182  	parts := [][]byte{
   183  		[]byte("bar baz"),
   184  		[]byte("1 2 3 4"),
   185  		[]byte("hello world"),
   186  	}
   187  	msgs, res := proc.ProcessMessage(message.New(parts))
   188  	if res == nil {
   189  		t.Fatal("expected empty response")
   190  	}
   191  	if err = res.Error(); err != nil {
   192  		t.Error(err)
   193  	}
   194  	if len(msgs) != 0 {
   195  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   196  	}
   197  }
   198  
   199  //------------------------------------------------------------------------------
   200  
   201  func TestProcessBatchEmpty(t *testing.T) {
   202  	conf := NewConfig()
   203  	conf.Type = "process_batch"
   204  
   205  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  
   210  	exp := [][]byte{
   211  		[]byte("foo bar baz"),
   212  	}
   213  	msgs, res := proc.ProcessMessage(message.New(exp))
   214  	if res != nil {
   215  		t.Fatal(res.Error())
   216  	}
   217  
   218  	if len(msgs) != 1 {
   219  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   220  	}
   221  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
   222  		t.Errorf("Wrong results: %s != %s", act, exp)
   223  	}
   224  }
   225  
   226  func TestProcessBatchBasic(t *testing.T) {
   227  	encodeConf := NewConfig()
   228  	encodeConf.Type = "encode"
   229  	encodeConf.Encode.Parts = []int{0}
   230  
   231  	conf := NewConfig()
   232  	conf.Type = "process_batch"
   233  	conf.ProcessBatch = append(conf.ProcessBatch, encodeConf)
   234  
   235  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   236  	if err != nil {
   237  		t.Fatal(err)
   238  	}
   239  
   240  	parts := [][]byte{
   241  		[]byte("foo bar baz"),
   242  		[]byte("1 2 3 4"),
   243  		[]byte("hello foo world"),
   244  	}
   245  	exp := [][]byte{
   246  		[]byte("Zm9vIGJhciBiYXo="),
   247  		[]byte("MSAyIDMgNA=="),
   248  		[]byte("aGVsbG8gZm9vIHdvcmxk"),
   249  	}
   250  	msgs, res := proc.ProcessMessage(message.New(parts))
   251  	if res != nil {
   252  		t.Fatal(res.Error())
   253  	}
   254  
   255  	if len(msgs) != 1 {
   256  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   257  	}
   258  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
   259  		t.Errorf("Wrong results: %s != %s", act, exp)
   260  	}
   261  }
   262  
   263  func TestProcessBatchFilterSome(t *testing.T) {
   264  	cond := condition.NewConfig()
   265  	cond.Type = "text"
   266  	cond.Text.Arg = "foo"
   267  	cond.Text.Operator = "contains"
   268  
   269  	filterConf := NewConfig()
   270  	filterConf.Type = "filter"
   271  	filterConf.Filter.Config = cond
   272  
   273  	conf := NewConfig()
   274  	conf.Type = "process_batch"
   275  	conf.ProcessBatch = append(conf.ProcessBatch, filterConf)
   276  
   277  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   278  	if err != nil {
   279  		t.Fatal(err)
   280  	}
   281  
   282  	parts := [][]byte{
   283  		[]byte("foo bar baz"),
   284  		[]byte("1 2 3 4"),
   285  		[]byte("hello foo world"),
   286  	}
   287  	exp := [][]byte{
   288  		[]byte("foo bar baz"),
   289  		[]byte("hello foo world"),
   290  	}
   291  	msgs, res := proc.ProcessMessage(message.New(parts))
   292  	if res != nil {
   293  		t.Fatal(res.Error())
   294  	}
   295  
   296  	if len(msgs) != 1 {
   297  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   298  	}
   299  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
   300  		t.Errorf("Wrong results: %s != %s", act, exp)
   301  	}
   302  }
   303  
   304  func TestProcessBatchMultiProcs(t *testing.T) {
   305  	encodeConf := NewConfig()
   306  	encodeConf.Type = "encode"
   307  	encodeConf.Encode.Parts = []int{0}
   308  
   309  	cond := condition.NewConfig()
   310  	cond.Type = "text"
   311  	cond.Text.Arg = "foo"
   312  	cond.Text.Operator = "contains"
   313  
   314  	filterConf := NewConfig()
   315  	filterConf.Type = "filter"
   316  	filterConf.Filter.Config = cond
   317  
   318  	conf := NewConfig()
   319  	conf.Type = "process_batch"
   320  	conf.ProcessBatch = append(conf.ProcessBatch, filterConf, encodeConf)
   321  
   322  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   323  	if err != nil {
   324  		t.Fatal(err)
   325  	}
   326  
   327  	parts := [][]byte{
   328  		[]byte("foo bar baz"),
   329  		[]byte("1 2 3 4"),
   330  		[]byte("hello foo world"),
   331  	}
   332  	exp := [][]byte{
   333  		[]byte("Zm9vIGJhciBiYXo="),
   334  		[]byte("aGVsbG8gZm9vIHdvcmxk"),
   335  	}
   336  	msgs, res := proc.ProcessMessage(message.New(parts))
   337  	if res != nil {
   338  		t.Fatal(res.Error())
   339  	}
   340  
   341  	if len(msgs) != 1 {
   342  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   343  	}
   344  	if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) {
   345  		t.Errorf("Wrong results: %s != %s", act, exp)
   346  	}
   347  }
   348  
   349  func TestProcessBatchFilterAll(t *testing.T) {
   350  	cond := condition.NewConfig()
   351  	cond.Type = "text"
   352  	cond.Text.Arg = "foo"
   353  	cond.Text.Operator = "contains"
   354  
   355  	filterConf := NewConfig()
   356  	filterConf.Type = "filter"
   357  	filterConf.Filter.Config = cond
   358  
   359  	conf := NewConfig()
   360  	conf.Type = "process_batch"
   361  	conf.ProcessBatch = append(conf.ProcessBatch, filterConf)
   362  
   363  	proc, err := New(conf, nil, log.Noop(), metrics.Noop())
   364  	if err != nil {
   365  		t.Fatal(err)
   366  	}
   367  
   368  	parts := [][]byte{
   369  		[]byte("bar baz"),
   370  		[]byte("1 2 3 4"),
   371  		[]byte("hello world"),
   372  	}
   373  	msgs, res := proc.ProcessMessage(message.New(parts))
   374  	if res == nil {
   375  		t.Fatal("expected empty response")
   376  	}
   377  	if err = res.Error(); err != nil {
   378  		t.Error(err)
   379  	}
   380  	if len(msgs) != 0 {
   381  		t.Errorf("Wrong count of result msgs: %v", len(msgs))
   382  	}
   383  }
   384  
   385  //------------------------------------------------------------------------------