github.com/Jeffail/benthos/v3@v3.65.0/lib/broker/greedy_test.go (about) 1 package broker 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/Jeffail/benthos/v3/lib/message" 10 "github.com/Jeffail/benthos/v3/lib/response" 11 "github.com/Jeffail/benthos/v3/lib/types" 12 ) 13 14 var _ types.Consumer = &Greedy{} 15 var _ types.Closable = &Greedy{} 16 17 func TestGreedyDoubleClose(t *testing.T) { 18 oTM, err := NewGreedy([]types.Output{}) 19 if err != nil { 20 t.Error(err) 21 return 22 } 23 24 // This shouldn't cause a panic 25 oTM.CloseAsync() 26 oTM.CloseAsync() 27 } 28 29 //------------------------------------------------------------------------------ 30 31 func TestBasicGreedy(t *testing.T) { 32 nMsgs := 1000 33 34 outputs := []types.Output{} 35 mockOutputs := []*MockOutputType{ 36 {}, 37 {}, 38 {}, 39 } 40 41 for _, o := range mockOutputs { 42 outputs = append(outputs, o) 43 } 44 45 readChan := make(chan types.Transaction) 46 resChan := make(chan types.Response) 47 48 oTM, err := NewGreedy(outputs) 49 if err != nil { 50 t.Error(err) 51 return 52 } 53 if err = oTM.Consume(readChan); err != nil { 54 t.Error(err) 55 return 56 } 57 58 // Only read from a single output. 59 for i := 0; i < nMsgs; i++ { 60 content := [][]byte{[]byte(fmt.Sprintf("hello world %v", i))} 61 go func() { 62 var ts types.Transaction 63 select { 64 case ts = <-mockOutputs[0].TChan: 65 if !bytes.Equal(ts.Payload.Get(0).Get(), content[0]) { 66 t.Errorf("Wrong content returned %s != %s", ts.Payload.Get(0).Get(), content[0]) 67 } 68 case <-time.After(time.Second): 69 t.Errorf("Timed out waiting for broker propagate") 70 return 71 } 72 73 select { 74 case ts.ResponseChan <- response.NewAck(): 75 case <-time.After(time.Second): 76 t.Errorf("Timed out responding to broker") 77 return 78 } 79 }() 80 81 select { 82 case readChan <- types.NewTransaction(message.New(content), resChan): 83 case <-time.After(time.Second): 84 t.Errorf("Timed out waiting for broker send") 85 return 86 } 87 88 select { 89 case res := <-resChan: 90 if res.Error() != nil { 91 t.Errorf("Received unexpected errors from broker: %v", res.Error()) 92 } 93 case <-time.After(time.Second): 94 t.Errorf("Timed out responding to broker") 95 return 96 } 97 } 98 99 oTM.CloseAsync() 100 if err := oTM.WaitForClose(time.Second * 10); err != nil { 101 t.Error(err) 102 } 103 } 104 105 //------------------------------------------------------------------------------