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
|
package mocks
import (
"errors"
"sync"
"github.com/IBM/sarama"
)
// SyncProducer implements sarama's SyncProducer interface for testing purposes.
// Before you can use it, you have to set expectations on the mock SyncProducer
// to tell it how to handle calls to SendMessage, so you can easily test success
// and failure scenarios.
type SyncProducer struct {
l sync.Mutex
t ErrorReporter
expectations []*producerExpectation
lastOffset int64
*TopicConfig
newPartitioner sarama.PartitionerConstructor
partitioners map[string]sarama.Partitioner
isTransactional bool
txnLock sync.Mutex
txnStatus sarama.ProducerTxnStatusFlag
}
// NewSyncProducer instantiates a new SyncProducer mock. The t argument should
// be the *testing.T instance of your test method. An error will be written to it if
// an expectation is violated. The config argument is validated and used to handle
// partitioning.
func NewSyncProducer(t ErrorReporter, config *sarama.Config) *SyncProducer {
if config == nil {
config = sarama.NewConfig()
}
if err := config.Validate(); err != nil {
t.Errorf("Invalid mock configuration provided: %s", err.Error())
}
return &SyncProducer{
t: t,
expectations: make([]*producerExpectation, 0),
TopicConfig: NewTopicConfig(),
newPartitioner: config.Producer.Partitioner,
partitioners: make(map[string]sarama.Partitioner, 1),
isTransactional: config.Producer.Transaction.ID != "",
txnStatus: sarama.ProducerTxnFlagReady,
}
}
////////////////////////////////////////////////
// Implement SyncProducer interface
////////////////////////////////////////////////
// SendMessage corresponds with the SendMessage method of sarama's SyncProducer implementation.
// You have to set expectations on the mock producer before calling SendMessage, so it knows
// how to handle them. You can set a function in each expectation so that the message value
// checked by this function and an error is returned if the match fails.
// If there is no more remaining expectation when SendMessage is called,
// the mock producer will write an error to the test state object.
func (sp *SyncProducer) SendMessage(msg *sarama.ProducerMessage) (partition int32, offset int64, err error) {
sp.l.Lock()
defer sp.l.Unlock()
if sp.IsTransactional() && sp.txnStatus&sarama.ProducerTxnFlagInTransaction == 0 {
sp.t.Errorf("attempt to send message when transaction is not started or is in ending state.")
return -1, -1, errors.New("attempt to send message when transaction is not started or is in ending state")
}
if len(sp.expectations) > 0 {
expectation := sp.expectations[0]
sp.expectations = sp.expectations[1:]
topic := msg.Topic
partition, err := sp.partitioner(topic).Partition(msg, sp.partitions(topic))
if err != nil {
sp.t.Errorf("Partitioner returned an error: %s", err.Error())
return -1, -1, err
}
msg.Partition = partition
if expectation.CheckFunction != nil {
errCheck := expectation.CheckFunction(msg)
if errCheck != nil {
sp.t.Errorf("Check function returned an error: %s", errCheck.Error())
return -1, -1, errCheck
}
}
if errors.Is(expectation.Result, errProduceSuccess) {
sp.lastOffset++
msg.Offset = sp.lastOffset
return 0, msg.Offset, nil
}
return -1, -1, expectation.Result
}
sp.t.Errorf("No more expectation set on this mock producer to handle the input message.")
return -1, -1, errOutOfExpectations
}
// SendMessages corresponds with the SendMessages method of sarama's SyncProducer implementation.
// You have to set expectations on the mock producer before calling SendMessages, so it knows
// how to handle them. If there is no more remaining expectations when SendMessages is called,
// the mock producer will write an error to the test state object.
func (sp *SyncProducer) SendMessages(msgs []*sarama.ProducerMessage) error {
sp.l.Lock()
defer sp.l.Unlock()
if len(sp.expectations) >= len(msgs) {
expectations := sp.expectations[0:len(msgs)]
sp.expectations = sp.expectations[len(msgs):]
for i, expectation := range expectations {
topic := msgs[i].Topic
partition, err := sp.partitioner(topic).Partition(msgs[i], sp.partitions(topic))
if err != nil {
sp.t.Errorf("Partitioner returned an error: %s", err.Error())
return err
}
msgs[i].Partition = partition
if expectation.CheckFunction != nil {
errCheck := expectation.CheckFunction(msgs[i])
if errCheck != nil {
sp.t.Errorf("Check function returned an error: %s", errCheck.Error())
return errCheck
}
}
if !errors.Is(expectation.Result, errProduceSuccess) {
return expectation.Result
}
sp.lastOffset++
msgs[i].Offset = sp.lastOffset
}
return nil
}
sp.t.Errorf("Insufficient expectations set on this mock producer to handle the input messages.")
return errOutOfExpectations
}
func (sp *SyncProducer) partitioner(topic string) sarama.Partitioner {
partitioner := sp.partitioners[topic]
if partitioner == nil {
partitioner = sp.newPartitioner(topic)
sp.partitioners[topic] = partitioner
}
return partitioner
}
// Close corresponds with the Close method of sarama's SyncProducer implementation.
// By closing a mock syncproducer, you also tell it that no more SendMessage calls will follow,
// so it will write an error to the test state if there's any remaining expectations.
func (sp *SyncProducer) Close() error {
sp.l.Lock()
defer sp.l.Unlock()
if len(sp.expectations) > 0 {
sp.t.Errorf("Expected to exhaust all expectations, but %d are left.", len(sp.expectations))
}
return nil
}
////////////////////////////////////////////////
// Setting expectations
////////////////////////////////////////////////
// ExpectSendMessageWithMessageCheckerFunctionAndSucceed sets an expectation on the mock producer
// that SendMessage will be called. The mock producer will first call the given function to check
// the message. It will cascade the error of the function, if any, or handle the message as if it
// produced successfully, i.e. by returning a valid partition, and offset, and a nil error.
func (sp *SyncProducer) ExpectSendMessageWithMessageCheckerFunctionAndSucceed(cf MessageChecker) *SyncProducer {
sp.l.Lock()
defer sp.l.Unlock()
sp.expectations = append(sp.expectations, &producerExpectation{Result: errProduceSuccess, CheckFunction: cf})
return sp
}
// ExpectSendMessageWithMessageCheckerFunctionAndFail sets an expectation on the mock producer that
// SendMessage will be called. The mock producer will first call the given function to check the
// message. It will cascade the error of the function, if any, or handle the message as if it
// failed to produce successfully, i.e. by returning the provided error.
func (sp *SyncProducer) ExpectSendMessageWithMessageCheckerFunctionAndFail(cf MessageChecker, err error) *SyncProducer {
sp.l.Lock()
defer sp.l.Unlock()
sp.expectations = append(sp.expectations, &producerExpectation{Result: err, CheckFunction: cf})
return sp
}
// ExpectSendMessageWithCheckerFunctionAndSucceed sets an expectation on the mock producer that SendMessage
// will be called. The mock producer will first call the given function to check the message value.
// It will cascade the error of the function, if any, or handle the message as if it produced
// successfully, i.e. by returning a valid partition, and offset, and a nil error.
func (sp *SyncProducer) ExpectSendMessageWithCheckerFunctionAndSucceed(cf ValueChecker) *SyncProducer {
sp.ExpectSendMessageWithMessageCheckerFunctionAndSucceed(messageValueChecker(cf))
return sp
}
// ExpectSendMessageWithCheckerFunctionAndFail sets an expectation on the mock producer that SendMessage will be
// called. The mock producer will first call the given function to check the message value.
// It will cascade the error of the function, if any, or handle the message as if it failed
// to produce successfully, i.e. by returning the provided error.
func (sp *SyncProducer) ExpectSendMessageWithCheckerFunctionAndFail(cf ValueChecker, err error) *SyncProducer {
sp.ExpectSendMessageWithMessageCheckerFunctionAndFail(messageValueChecker(cf), err)
return sp
}
// ExpectSendMessageAndSucceed sets an expectation on the mock producer that SendMessage will be
// called. The mock producer will handle the message as if it produced successfully, i.e. by
// returning a valid partition, and offset, and a nil error.
func (sp *SyncProducer) ExpectSendMessageAndSucceed() *SyncProducer {
sp.ExpectSendMessageWithMessageCheckerFunctionAndSucceed(nil)
return sp
}
// ExpectSendMessageAndFail sets an expectation on the mock producer that SendMessage will be
// called. The mock producer will handle the message as if it failed to produce
// successfully, i.e. by returning the provided error.
func (sp *SyncProducer) ExpectSendMessageAndFail(err error) *SyncProducer {
sp.ExpectSendMessageWithMessageCheckerFunctionAndFail(nil, err)
return sp
}
func (sp *SyncProducer) IsTransactional() bool {
return sp.isTransactional
}
func (sp *SyncProducer) BeginTxn() error {
sp.txnLock.Lock()
defer sp.txnLock.Unlock()
sp.txnStatus = sarama.ProducerTxnFlagInTransaction
return nil
}
func (sp *SyncProducer) CommitTxn() error {
sp.txnLock.Lock()
defer sp.txnLock.Unlock()
sp.txnStatus = sarama.ProducerTxnFlagReady
return nil
}
func (sp *SyncProducer) AbortTxn() error {
sp.txnLock.Lock()
defer sp.txnLock.Unlock()
sp.txnStatus = sarama.ProducerTxnFlagReady
return nil
}
func (sp *SyncProducer) TxnStatus() sarama.ProducerTxnStatusFlag {
return sp.txnStatus
}
func (sp *SyncProducer) AddOffsetsToTxn(offsets map[string][]*sarama.PartitionOffsetMetadata, groupId string) error {
return nil
}
func (sp *SyncProducer) AddMessageToTxn(msg *sarama.ConsumerMessage, groupId string, metadata *string) error {
return nil
}
|