github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/throttle_test.go (about) 1 package processor 2 3 import ( 4 "testing" 5 "time" 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 ) 11 12 func TestThrottle(t *testing.T) { 13 conf := NewConfig() 14 conf.Type = TypeThrottle 15 conf.Throttle.Period = "1ns" 16 17 throt, err := New(conf, nil, log.Noop(), metrics.Noop()) 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 msgIn := message.New(nil) 23 msgsOut, res := throt.ProcessMessage(msgIn) 24 if res != nil { 25 t.Fatal(res.Error()) 26 } 27 28 if exp, act := msgIn, msgsOut[0]; exp != act { 29 t.Errorf("Wrong message returned: %v != %v", act, exp) 30 } 31 } 32 33 func TestThrottle200Millisecond(t *testing.T) { 34 conf := NewConfig() 35 conf.Type = TypeThrottle 36 conf.Throttle.Period = "200ms" 37 38 throt, err := New(conf, nil, log.Noop(), metrics.Noop()) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 tBefore := time.Now() 44 throt.ProcessMessage(message.New(nil)) 45 tBetween := time.Now() 46 throt.ProcessMessage(message.New(nil)) 47 tAfter := time.Now() 48 49 if dur := tBetween.Sub(tBefore); dur > (time.Millisecond * 50) { 50 t.Errorf("First message took too long") 51 } 52 if dur := tAfter.Sub(tBetween); dur < (time.Millisecond * 200) { 53 t.Errorf("First message didn't take long enough") 54 } 55 } 56 57 func TestThrottleBadPeriod(t *testing.T) { 58 conf := NewConfig() 59 conf.Type = TypeThrottle 60 conf.Throttle.Period = "1gfdfgfdns" 61 62 _, err := New(conf, nil, log.Noop(), metrics.Noop()) 63 if err == nil { 64 t.Error("Expected error from bad duration") 65 } 66 }