1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
//go:build !functional
package mocks
import (
"errors"
"fmt"
"regexp"
"strings"
"testing"
"github.com/IBM/sarama"
)
func generateRegexpChecker(re string) func([]byte) error {
return func(val []byte) error {
matched, err := regexp.MatchString(re, string(val))
if err != nil {
return errors.New("Error while trying to match the input message with the expected pattern: " + err.Error())
}
if !matched {
return fmt.Errorf("No match between input value \"%s\" and expected pattern \"%s\"", val, re)
}
return nil
}
}
type testReporterMock struct {
errors []string
}
func newTestReporterMock() *testReporterMock {
return &testReporterMock{errors: make([]string, 0)}
}
func (trm *testReporterMock) Errorf(format string, args ...interface{}) {
trm.errors = append(trm.errors, fmt.Sprintf(format, args...))
}
func TestMockAsyncProducerImplementsAsyncProducerInterface(t *testing.T) {
var mp interface{} = &AsyncProducer{}
if _, ok := mp.(sarama.AsyncProducer); !ok {
t.Error("The mock producer should implement the sarama.Producer interface.")
}
}
func TestProducerReturnsExpectationsToChannels(t *testing.T) {
config := NewTestConfig()
config.Producer.Return.Successes = true
mp := NewAsyncProducer(t, config).
ExpectInputAndSucceed().
ExpectInputAndSucceed().
ExpectInputAndFail(sarama.ErrOutOfBrokers)
mp.Input() <- &sarama.ProducerMessage{Topic: "test 1"}
mp.Input() <- &sarama.ProducerMessage{Topic: "test 2"}
mp.Input() <- &sarama.ProducerMessage{Topic: "test 3"}
msg1 := <-mp.Successes()
msg2 := <-mp.Successes()
err1 := <-mp.Errors()
if msg1.Topic != "test 1" {
t.Error("Expected message 1 to be returned first")
}
if msg2.Topic != "test 2" {
t.Error("Expected message 2 to be returned second")
}
if err1.Msg.Topic != "test 3" || !errors.Is(err1, sarama.ErrOutOfBrokers) {
t.Error("Expected message 3 to be returned as error")
}
if err := mp.Close(); err != nil {
t.Error(err)
}
}
func TestProducerWithTooFewExpectations(t *testing.T) {
trm := newTestReporterMock()
mp := NewAsyncProducer(trm, nil)
mp.ExpectInputAndSucceed()
mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(trm.errors) != 1 {
t.Error("Expected to report an error")
}
}
func TestProducerWithTooManyExpectations(t *testing.T) {
trm := newTestReporterMock()
mp := NewAsyncProducer(trm, nil).
ExpectInputAndSucceed().
ExpectInputAndFail(sarama.ErrOutOfBrokers)
mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(trm.errors) != 1 {
t.Error("Expected to report an error")
}
}
func TestProducerFailTxn(t *testing.T) {
config := NewTestConfig()
config.Producer.Transaction.ID = "test"
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Retry.Backoff = 0
config.Producer.Idempotent = true
config.Net.MaxOpenRequests = 1
config.Version = sarama.V0_11_0_0
trm := newTestReporterMock()
mp := NewAsyncProducer(trm, config)
mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
_ = mp.Close()
if len(trm.errors) != 1 {
t.Error("must have fail with txn begin error")
}
}
func TestProducerWithTxn(t *testing.T) {
config := NewTestConfig()
config.Producer.Transaction.ID = "test"
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Retry.Backoff = 0
config.Producer.Idempotent = true
config.Net.MaxOpenRequests = 1
config.Version = sarama.V0_11_0_0
trm := newTestReporterMock()
mp := NewAsyncProducer(trm, config).
ExpectInputAndSucceed()
if !mp.IsTransactional() {
t.Error("producer must be transactional")
}
if err := mp.BeginTxn(); err != nil {
t.Error(err)
}
if mp.TxnStatus()&sarama.ProducerTxnFlagInTransaction == 0 {
t.Error("transaction must be started")
}
mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
if err := mp.AddMessageToTxn(&sarama.ConsumerMessage{
Topic: "original-topic",
Partition: 0,
Offset: 123,
}, "test-group", nil); err != nil {
t.Error(err)
}
if err := mp.AddOffsetsToTxn(map[string][]*sarama.PartitionOffsetMetadata{
"original-topic": {
{
Partition: 1,
Offset: 321,
},
},
}, "test-group"); err != nil {
t.Error(err)
}
if err := mp.CommitTxn(); err != nil {
t.Error(err)
}
if err := mp.Close(); err != nil {
t.Error(err)
}
}
func TestProducerWithCheckerFunction(t *testing.T) {
trm := newTestReporterMock()
mp := NewAsyncProducer(trm, nil).
ExpectInputWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes")).
ExpectInputWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes$"))
mp.Input() <- &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
mp.Input() <- &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(mp.Errors()) != 1 {
t.Error("Expected to report an error")
}
err1 := <-mp.Errors()
if !strings.HasPrefix(err1.Err.Error(), "No match") {
t.Error("Expected to report a value check error, found: ", err1.Err)
}
}
func TestProducerWithBrokenPartitioner(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.Producer.Partitioner = func(string) sarama.Partitioner {
return brokePartitioner{}
}
mp := NewAsyncProducer(trm, config)
mp.ExpectInputWithMessageCheckerFunctionAndSucceed(func(msg *sarama.ProducerMessage) error {
if msg.Partition != 15 {
t.Error("Expected partition 15, found: ", msg.Partition)
}
if msg.Topic != "test" {
t.Errorf(`Expected topic "test", found: %q`, msg.Topic)
}
return nil
})
mp.ExpectInputAndSucceed() // should actually fail in partitioning
mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
mp.Input() <- &sarama.ProducerMessage{Topic: "not-test"}
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(trm.errors) != 1 || !strings.Contains(trm.errors[0], "partitioning unavailable") {
t.Error("Expected to report partitioning unavailable, found", trm.errors)
}
}
// brokeProducer refuses to partition anything not on the “test” topic, and sends everything on
// that topic to partition 15.
type brokePartitioner struct{}
func (brokePartitioner) Partition(msg *sarama.ProducerMessage, n int32) (int32, error) {
if msg.Topic == "test" {
return 15, nil
}
return 0, errors.New("partitioning unavailable")
}
func (brokePartitioner) RequiresConsistency() bool { return false }
func TestProducerWithInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.Version = sarama.V0_11_0_2
config.ClientID = "not a valid producer ID"
mp := NewAsyncProducer(trm, config)
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], `ClientID value "not a valid producer ID" is not valid for Kafka versions before 1.0.0`) {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}
|