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

     1  package processor
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/message"
     9  	"github.com/Jeffail/benthos/v3/lib/metrics"
    10  	"github.com/Jeffail/benthos/v3/lib/response"
    11  )
    12  
    13  func TestBoundsCheck(t *testing.T) {
    14  	conf := NewConfig()
    15  	conf.BoundsCheck.MinParts = 2
    16  	conf.BoundsCheck.MaxParts = 3
    17  	conf.BoundsCheck.MaxPartSize = 10
    18  	conf.BoundsCheck.MinPartSize = 1
    19  
    20  	testLog := log.Noop()
    21  	proc, err := NewBoundsCheck(conf, nil, testLog, metrics.Noop())
    22  	if err != nil {
    23  		t.Error(err)
    24  		return
    25  	}
    26  
    27  	goodParts := [][][]byte{
    28  		{
    29  			[]byte("hello"),
    30  			[]byte("world"),
    31  		},
    32  		{
    33  			[]byte("helloworld"),
    34  			[]byte("helloworld"),
    35  		},
    36  		{
    37  			[]byte("hello"),
    38  			[]byte("world"),
    39  			[]byte("!"),
    40  		},
    41  		{
    42  			[]byte("helloworld"),
    43  			[]byte("helloworld"),
    44  			[]byte("helloworld"),
    45  		},
    46  	}
    47  
    48  	badParts := [][][]byte{
    49  		{
    50  			[]byte("hello world"),
    51  		},
    52  		{
    53  			[]byte("hello world"),
    54  			[]byte("hello world this exceeds max part size"),
    55  		},
    56  		{
    57  			[]byte("hello"),
    58  			[]byte("world"),
    59  			[]byte("this"),
    60  			[]byte("exceeds"),
    61  			[]byte("max"),
    62  			[]byte("num"),
    63  			[]byte("parts"),
    64  		},
    65  		{
    66  			[]byte("hello"),
    67  			[]byte(""),
    68  		},
    69  	}
    70  
    71  	for _, parts := range goodParts {
    72  		msg := message.New(parts)
    73  		if msgs, _ := proc.ProcessMessage(msg); len(msgs) == 0 {
    74  			t.Errorf("Bounds check failed on: %s", parts)
    75  		} else if !reflect.DeepEqual(msgs[0], msg) {
    76  			t.Error("Wrong message returned (expected same)")
    77  		}
    78  	}
    79  
    80  	for _, parts := range badParts {
    81  		if msgs, res := proc.ProcessMessage(message.New(parts)); len(msgs) > 0 {
    82  			t.Errorf("Bounds check didnt fail on: %s", parts)
    83  		} else if _, ok := res.(response.Ack); !ok {
    84  			t.Error("Expected simple response from bad message")
    85  		}
    86  	}
    87  }