github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/while_test.go (about)

     1  package processor
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/condition"
     9  	"github.com/Jeffail/benthos/v3/lib/log"
    10  	"github.com/Jeffail/benthos/v3/lib/message"
    11  	"github.com/Jeffail/benthos/v3/lib/metrics"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestWhileErrs(t *testing.T) {
    17  	conf := NewConfig()
    18  	conf.Type = TypeWhile
    19  
    20  	_, err := New(conf, nil, log.Noop(), metrics.Noop())
    21  	require.EqualError(t, err, "a check query is required")
    22  
    23  	cond := condition.NewConfig()
    24  	cond.Text.Arg = "foobar"
    25  
    26  	conf.While.Condition = cond
    27  	conf.While.Check = "foobar"
    28  
    29  	_, err = New(conf, nil, log.Noop(), metrics.Noop())
    30  	require.EqualError(t, err, "cannot specify both a condition and a check query")
    31  }
    32  
    33  func TestWhileWithCount(t *testing.T) {
    34  	conf := NewConfig()
    35  	conf.Type = "while"
    36  	conf.While.Check = `count("while_test_1") < 3`
    37  
    38  	procConf := NewConfig()
    39  	procConf.Type = "insert_part"
    40  	procConf.InsertPart.Content = "foo"
    41  	procConf.InsertPart.Index = 0
    42  
    43  	conf.While.Processors = append(conf.While.Processors, procConf)
    44  
    45  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
    46  	require.NoError(t, err)
    47  
    48  	exp := [][]byte{
    49  		[]byte(`foo`),
    50  		[]byte(`foo`),
    51  		[]byte(`bar`),
    52  	}
    53  
    54  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
    55  	require.Nil(t, res)
    56  
    57  	assert.Equal(t, exp, message.GetAllBytes(msg[0]))
    58  }
    59  
    60  func TestWhileWithContentCheck(t *testing.T) {
    61  	conf := NewConfig()
    62  	conf.Type = "while"
    63  	conf.While.Check = "batch_size() <= 3"
    64  
    65  	procConf := NewConfig()
    66  	procConf.Type = "insert_part"
    67  	procConf.InsertPart.Content = "foo"
    68  	procConf.InsertPart.Index = 0
    69  
    70  	conf.While.Processors = append(conf.While.Processors, procConf)
    71  
    72  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	exp := [][]byte{
    78  		[]byte(`foo`),
    79  		[]byte(`foo`),
    80  		[]byte(`foo`),
    81  		[]byte(`bar`),
    82  	}
    83  
    84  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
    85  	if res != nil {
    86  		t.Error(res.Error())
    87  	}
    88  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
    89  		t.Errorf("Wrong result: %s != %s", act, exp)
    90  	}
    91  }
    92  
    93  func TestWhileWithCountALO(t *testing.T) {
    94  	conf := NewConfig()
    95  	conf.Type = "while"
    96  	conf.While.Check = `count("while_test_2") < 3`
    97  	conf.While.AtLeastOnce = true
    98  
    99  	procConf := NewConfig()
   100  	procConf.Type = "insert_part"
   101  	procConf.InsertPart.Content = "foo"
   102  	procConf.InsertPart.Index = 0
   103  
   104  	conf.While.Processors = append(conf.While.Processors, procConf)
   105  
   106  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  
   111  	exp := [][]byte{
   112  		[]byte(`foo`),
   113  		[]byte(`foo`),
   114  		[]byte(`foo`),
   115  		[]byte(`bar`),
   116  	}
   117  
   118  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   119  	if res != nil {
   120  		t.Error(res.Error())
   121  	}
   122  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
   123  		t.Errorf("Wrong result: %s != %s", act, exp)
   124  	}
   125  }
   126  
   127  func TestWhileMaxLoops(t *testing.T) {
   128  	conf := NewConfig()
   129  	conf.Type = "while"
   130  	conf.While.MaxLoops = 3
   131  	conf.While.Check = `true`
   132  
   133  	procConf := NewConfig()
   134  	procConf.Type = "insert_part"
   135  	procConf.InsertPart.Content = "foo"
   136  	procConf.InsertPart.Index = 0
   137  
   138  	conf.While.Processors = append(conf.While.Processors, procConf)
   139  
   140  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  
   145  	exp := [][]byte{
   146  		[]byte(`foo`),
   147  		[]byte(`foo`),
   148  		[]byte(`foo`),
   149  		[]byte(`bar`),
   150  	}
   151  
   152  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   153  	if res != nil {
   154  		t.Error(res.Error())
   155  	}
   156  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
   157  		t.Errorf("Wrong result: %s != %s", act, exp)
   158  	}
   159  }
   160  
   161  func TestWhileWithStaticTrue(t *testing.T) {
   162  	conf := NewConfig()
   163  	conf.Type = "while"
   164  	conf.While.Check = `true`
   165  
   166  	procConf := NewConfig()
   167  	procConf.Type = "insert_part"
   168  	procConf.InsertPart.Content = "foo"
   169  	procConf.InsertPart.Index = 0
   170  
   171  	conf.While.Processors = append(conf.While.Processors, procConf)
   172  
   173  	procConf = NewConfig()
   174  	procConf.Type = "sleep"
   175  	procConf.Sleep.Duration = "100ms"
   176  
   177  	conf.While.Processors = append(conf.While.Processors, procConf)
   178  
   179  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  
   184  	go func() {
   185  		<-time.After(time.Millisecond * 100)
   186  		c.CloseAsync()
   187  	}()
   188  
   189  	_, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   190  	if res == nil {
   191  		t.Error("Expected error response due to shut down")
   192  	}
   193  
   194  	if err := c.WaitForClose(time.Second); err != nil {
   195  		t.Error(err)
   196  	}
   197  }
   198  
   199  func TestWhileDeprecatedWithCount(t *testing.T) {
   200  	conf := NewConfig()
   201  	conf.Type = "while"
   202  	conf.While.Condition.Type = "count"
   203  	conf.While.Condition.Count.Arg = 3
   204  
   205  	procConf := NewConfig()
   206  	procConf.Type = "insert_part"
   207  	procConf.InsertPart.Content = "foo"
   208  	procConf.InsertPart.Index = 0
   209  
   210  	conf.While.Processors = append(conf.While.Processors, procConf)
   211  
   212  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   213  	if err != nil {
   214  		t.Fatal(err)
   215  	}
   216  
   217  	exp := [][]byte{
   218  		[]byte(`foo`),
   219  		[]byte(`foo`),
   220  		[]byte(`bar`),
   221  	}
   222  
   223  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   224  	if res != nil {
   225  		t.Error(res.Error())
   226  	}
   227  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
   228  		t.Errorf("Wrong result: %s != %s", act, exp)
   229  	}
   230  }
   231  
   232  func TestWhileDeprecatedWithContentCheck(t *testing.T) {
   233  	conf := NewConfig()
   234  	conf.Type = "while"
   235  	conf.While.Condition.Type = "bounds_check"
   236  	conf.While.Condition.BoundsCheck.MaxParts = 3
   237  
   238  	procConf := NewConfig()
   239  	procConf.Type = "insert_part"
   240  	procConf.InsertPart.Content = "foo"
   241  	procConf.InsertPart.Index = 0
   242  
   243  	conf.While.Processors = append(conf.While.Processors, procConf)
   244  
   245  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   246  	if err != nil {
   247  		t.Fatal(err)
   248  	}
   249  
   250  	exp := [][]byte{
   251  		[]byte(`foo`),
   252  		[]byte(`foo`),
   253  		[]byte(`foo`),
   254  		[]byte(`bar`),
   255  	}
   256  
   257  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   258  	if res != nil {
   259  		t.Error(res.Error())
   260  	}
   261  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
   262  		t.Errorf("Wrong result: %s != %s", act, exp)
   263  	}
   264  }
   265  
   266  func TestWhileDeprecatedWithCountALO(t *testing.T) {
   267  	conf := NewConfig()
   268  	conf.Type = "while"
   269  	conf.While.AtLeastOnce = true
   270  	conf.While.Condition.Type = "count"
   271  	conf.While.Condition.Count.Arg = 3
   272  
   273  	procConf := NewConfig()
   274  	procConf.Type = "insert_part"
   275  	procConf.InsertPart.Content = "foo"
   276  	procConf.InsertPart.Index = 0
   277  
   278  	conf.While.Processors = append(conf.While.Processors, procConf)
   279  
   280  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   281  	if err != nil {
   282  		t.Fatal(err)
   283  	}
   284  
   285  	exp := [][]byte{
   286  		[]byte(`foo`),
   287  		[]byte(`foo`),
   288  		[]byte(`foo`),
   289  		[]byte(`bar`),
   290  	}
   291  
   292  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   293  	if res != nil {
   294  		t.Error(res.Error())
   295  	}
   296  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
   297  		t.Errorf("Wrong result: %s != %s", act, exp)
   298  	}
   299  }
   300  
   301  func TestWhileDeprecatedMaxLoops(t *testing.T) {
   302  	conf := NewConfig()
   303  	conf.Type = "while"
   304  	conf.While.MaxLoops = 3
   305  	conf.While.Condition.Type = "static"
   306  	conf.While.Condition.Static = true
   307  
   308  	procConf := NewConfig()
   309  	procConf.Type = "insert_part"
   310  	procConf.InsertPart.Content = "foo"
   311  	procConf.InsertPart.Index = 0
   312  
   313  	conf.While.Processors = append(conf.While.Processors, procConf)
   314  
   315  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   316  	if err != nil {
   317  		t.Fatal(err)
   318  	}
   319  
   320  	exp := [][]byte{
   321  		[]byte(`foo`),
   322  		[]byte(`foo`),
   323  		[]byte(`foo`),
   324  		[]byte(`bar`),
   325  	}
   326  
   327  	msg, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   328  	if res != nil {
   329  		t.Error(res.Error())
   330  	}
   331  	if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) {
   332  		t.Errorf("Wrong result: %s != %s", act, exp)
   333  	}
   334  }
   335  
   336  func TestWhileDeprecatedWithStaticTrue(t *testing.T) {
   337  	conf := NewConfig()
   338  	conf.Type = "while"
   339  	conf.While.Condition.Type = "static"
   340  	conf.While.Condition.Static = true
   341  
   342  	procConf := NewConfig()
   343  	procConf.Type = "insert_part"
   344  	procConf.InsertPart.Content = "foo"
   345  	procConf.InsertPart.Index = 0
   346  
   347  	conf.While.Processors = append(conf.While.Processors, procConf)
   348  
   349  	procConf = NewConfig()
   350  	procConf.Type = "sleep"
   351  	procConf.Sleep.Duration = "100ms"
   352  
   353  	conf.While.Processors = append(conf.While.Processors, procConf)
   354  
   355  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
   356  	if err != nil {
   357  		t.Fatal(err)
   358  	}
   359  
   360  	go func() {
   361  		<-time.After(time.Millisecond * 100)
   362  		c.CloseAsync()
   363  	}()
   364  
   365  	_, res := c.ProcessMessage(message.New([][]byte{[]byte("bar")}))
   366  	if res == nil {
   367  		t.Error("Expected error response due to shut down")
   368  	}
   369  
   370  	if err := c.WaitForClose(time.Second); err != nil {
   371  		t.Error(err)
   372  	}
   373  }