github.com/Jeffail/benthos/v3@v3.65.0/lib/output/try_test.go (about) 1 package output 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 "time" 8 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/Jeffail/benthos/v3/lib/processor" 13 "github.com/Jeffail/benthos/v3/lib/types" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestTryOutputBasic(t *testing.T) { 18 dir := t.TempDir() 19 20 outOne, outTwo, outThree := NewConfig(), NewConfig(), NewConfig() 21 outOne.Type, outTwo.Type, outThree.Type = TypeHTTPClient, TypeFiles, TypeFile 22 outOne.HTTPClient.URL = "http://localhost:11111111/badurl" 23 outOne.HTTPClient.NumRetries = 1 24 outOne.HTTPClient.Retry = "1ms" 25 outTwo.Files.Path = filepath.Join(dir, "two", `bar-${!count("tofoo")}-${!count("tobar")}.txt`) 26 outThree.File.Path = "/dev/null" 27 28 procOne, procTwo, procThree := processor.NewConfig(), processor.NewConfig(), processor.NewConfig() 29 procOne.Type, procTwo.Type, procThree.Type = processor.TypeText, processor.TypeText, processor.TypeText 30 procOne.Text.Operator = "prepend" 31 procOne.Text.Value = "this-should-never-appear ${!count(\"tofoo\")}" 32 procTwo.Text.Operator = "prepend" 33 procTwo.Text.Value = "two-" 34 procThree.Text.Operator = "prepend" 35 procThree.Text.Value = "this-should-never-appear ${!count(\"tobar\")}" 36 37 outOne.Processors = append(outOne.Processors, procOne) 38 outTwo.Processors = append(outTwo.Processors, procTwo) 39 outThree.Processors = append(outThree.Processors, procThree) 40 41 conf := NewConfig() 42 conf.Type = TypeTry 43 conf.Try = append(conf.Try, outOne, outTwo, outThree) 44 45 s, err := New(conf, nil, log.Noop(), metrics.Noop()) 46 require.NoError(t, err) 47 48 sendChan := make(chan types.Transaction) 49 resChan := make(chan types.Response) 50 require.NoError(t, s.Consume(sendChan)) 51 52 t.Cleanup(func() { 53 s.CloseAsync() 54 require.NoError(t, s.WaitForClose(time.Second)) 55 }) 56 57 inputs := []string{ 58 "first", "second", "third", "fourth", 59 } 60 expFiles := map[string]string{ 61 "./two/bar-2-1.txt": "two-first", 62 "./two/bar-4-2.txt": "two-second", 63 "./two/bar-6-3.txt": "two-third", 64 "./two/bar-8-4.txt": "two-fourth", 65 } 66 67 for _, input := range inputs { 68 testMsg := message.New([][]byte{[]byte(input)}) 69 select { 70 case sendChan <- types.NewTransaction(testMsg, resChan): 71 case <-time.After(time.Second * 2): 72 t.Fatal("Action timed out") 73 } 74 75 select { 76 case res := <-resChan: 77 if res.Error() != nil { 78 t.Fatal(res.Error()) 79 } 80 case <-time.After(time.Second * 2): 81 t.Fatal("Action timed out") 82 } 83 } 84 85 for k, exp := range expFiles { 86 k = filepath.Join(dir, k) 87 fileBytes, err := os.ReadFile(k) 88 if err != nil { 89 t.Errorf("Expected file '%v' could not be read: %v", k, err) 90 continue 91 } 92 if act := string(fileBytes); exp != act { 93 t.Errorf("Wrong contents for file '%v': %v != %v", k, act, exp) 94 } 95 } 96 }