55 "fmt"
66 "testing"
77
8+ "github.com/Shopify/sarama"
89 "github.com/Shopify/sarama/mocks"
910 "github.com/heetch/felice/producer"
1011 "github.com/stretchr/testify/require"
@@ -39,6 +40,73 @@ func TestSendMessage(t *testing.T) {
3940 }
4041}
4142
43+ func TestSendMessages (t * testing.T ) {
44+ msgs := []* producer.Message {
45+ producer .NewMessage ("topic1" , "message1" ),
46+ & producer.Message {Topic : "topic2" , Body : "message2" },
47+ & producer.Message {Topic : "topic2" , Body : "message3" },
48+ }
49+ cfg := producer .NewConfig ("id" , producer .MessageConverterV1 ())
50+ msp := mocks .NewSyncProducer (t , & cfg .Config )
51+ p , err := producer .NewFrom (msp , cfg )
52+ require .NoError (t , err )
53+
54+ for i , msg := range msgs {
55+ i , msg := i , msg
56+ msp .ExpectSendMessageWithCheckerFunctionAndSucceed (func (val []byte ) error {
57+ if got , want := string (val ), fmt .Sprintf ("%q" , msg .Body ); got != want {
58+ return fmt .Errorf ("unexpected message %d; got %q want %q" , i , got , want )
59+ }
60+ return nil
61+ })
62+ }
63+ err = p .SendMessages (context .Background (), msgs )
64+ require .NoError (t , err )
65+
66+ // Unfortunately the Sarama mock doesn't fill out of any of the Offset, Partition
67+ // or Timestamp fields when SendMessages is called, so we can't test
68+ // that functionality here.
69+ }
70+
71+ func TestSendMessagesError (t * testing.T ) {
72+ msgs := []* producer.Message {
73+ producer .NewMessage ("topic1" , "message1" ),
74+ & producer.Message {Topic : "topic2" , Body : "message2" },
75+ & producer.Message {Topic : "topic2" , Body : "message3" },
76+ }
77+ perr1 := fmt .Errorf ("first error" )
78+ perr2 := fmt .Errorf ("second error" )
79+ cfg := producer .NewConfig ("id" , producer .MessageConverterV1 ())
80+ sendMessages := func (smsgs []* sarama.ProducerMessage ) error {
81+ return sarama.ProducerErrors {{
82+ Msg : smsgs [0 ],
83+ Err : perr1 ,
84+ }, {
85+ Msg : smsgs [2 ],
86+ Err : perr2 ,
87+ }}
88+ }
89+ p , err := producer .NewFrom (sendMessagesFunc {f : sendMessages }, cfg )
90+ require .NoError (t , err )
91+ err = p .SendMessages (context .Background (), msgs )
92+ sendErrs , ok := err .(producer.SendMessagesErrors )
93+ require .True (t , ok )
94+ require .Len (t , sendErrs , 2 )
95+
96+ if sendErrs [0 ].Msg != msgs [0 ] {
97+ t .Errorf ("unexpected message at 0: %#v" , sendErrs [0 ].Msg )
98+ }
99+ if sendErrs [0 ].Err != perr1 {
100+ t .Errorf ("unexpected error at 0: %#v" , sendErrs [0 ].Err )
101+ }
102+ if sendErrs [1 ].Msg != msgs [2 ] {
103+ t .Errorf ("unexpected message at 0: %#v" , sendErrs [0 ].Msg )
104+ }
105+ if sendErrs [1 ].Err != perr2 {
106+ t .Errorf ("unexpected error at 0: %#v" , sendErrs [0 ].Err )
107+ }
108+ }
109+
42110func TestSend (t * testing.T ) {
43111 msp := mocks .NewSyncProducer (t , nil )
44112 cfg := producer .NewConfig ("id" , producer .MessageConverterV1 ())
@@ -58,3 +126,12 @@ func TestSend(t *testing.T) {
58126 _ , err = p .Send (context .Background (), "topic" , "message" )
59127 require .EqualError (t , err , "producer: failed to send message: cannot produce message" )
60128}
129+
130+ type sendMessagesFunc struct {
131+ sarama.SyncProducer
132+ f func (msgs []* sarama.ProducerMessage ) error
133+ }
134+
135+ func (f sendMessagesFunc ) SendMessages (msgs []* sarama.ProducerMessage ) error {
136+ return f .f (msgs )
137+ }
0 commit comments