kafkatest

package
v2.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2023 License: MIT Imports: 4 Imported by: 2

README

KafkaTest

This package contains mocks intended to be used by users of this library for testing.

Empty mocks

If you require to implement your own mock functionality, you can use the empty mocks, which are created using moq to implement the interfaces kafka.IConsumerGroup, kafka.IProducer and kafka.Message

These interfaces expose the same methods as the real Producer and ConsumerGroup structs. You can instantiate the mocks like so:

consumer := kafkatest.IConsumerGroupMock{...}
producer := kafkatest.IProducerMock{...}
message := kafkatest.MessageMock{...}

Functional mocks

The previous mocks have been extended by implementing functionality that emulates a real Producer, Consumer and message; but without communicating with any real Kafka broker. If you require a functional mock to test how you interact with kafka, you can use these mocks (kafkatest.MessageConsumer, kafaktest.MessageProducer and kafkatest.Message) like so:

consumer := kafkatest.NewMessageConsumer(true)
producer := kafkatest.NewMessageProducer(true)
message := kafkatest.NewMessage(data, offset)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IConsumerGroupMock

type IConsumerGroupMock struct {
	// ChannelsFunc mocks the Channels method.
	ChannelsFunc func() *kafka.ConsumerGroupChannels

	// CheckerFunc mocks the Checker method.
	CheckerFunc func(ctx context.Context, state *health.CheckState) error

	// CloseFunc mocks the Close method.
	CloseFunc func(ctx context.Context, optFuncs ...kafka.OptFunc) error

	// InitialiseFunc mocks the Initialise method.
	InitialiseFunc func(ctx context.Context) error

	// IsInitialisedFunc mocks the IsInitialised method.
	IsInitialisedFunc func() bool

	// StopListeningToConsumerFunc mocks the StopListeningToConsumer method.
	StopListeningToConsumerFunc func(ctx context.Context) error
	// contains filtered or unexported fields
}

IConsumerGroupMock is a mock implementation of kafka.IConsumerGroup.

func TestSomethingThatUsesIConsumerGroup(t *testing.T) {

	// make and configure a mocked kafka.IConsumerGroup
	mockedIConsumerGroup := &IConsumerGroupMock{
		ChannelsFunc: func() *kafka.ConsumerGroupChannels {
			panic("mock out the Channels method")
		},
		CheckerFunc: func(ctx context.Context, state *health.CheckState) error {
			panic("mock out the Checker method")
		},
		CloseFunc: func(ctx context.Context, optFuncs ...kafka.OptFunc) error {
			panic("mock out the Close method")
		},
		InitialiseFunc: func(ctx context.Context) error {
			panic("mock out the Initialise method")
		},
		IsInitialisedFunc: func() bool {
			panic("mock out the IsInitialised method")
		},
		StopListeningToConsumerFunc: func(ctx context.Context) error {
			panic("mock out the StopListeningToConsumer method")
		},
	}

	// use mockedIConsumerGroup in code that requires kafka.IConsumerGroup
	// and then make assertions.

}

func (*IConsumerGroupMock) Channels

func (mock *IConsumerGroupMock) Channels() *kafka.ConsumerGroupChannels

Channels calls ChannelsFunc.

func (*IConsumerGroupMock) ChannelsCalls

func (mock *IConsumerGroupMock) ChannelsCalls() []struct {
}

ChannelsCalls gets all the calls that were made to Channels. Check the length with:

len(mockedIConsumerGroup.ChannelsCalls())

func (*IConsumerGroupMock) Checker

func (mock *IConsumerGroupMock) Checker(ctx context.Context, state *health.CheckState) error

Checker calls CheckerFunc.

func (*IConsumerGroupMock) CheckerCalls

func (mock *IConsumerGroupMock) CheckerCalls() []struct {
	Ctx   context.Context
	State *health.CheckState
}

CheckerCalls gets all the calls that were made to Checker. Check the length with:

len(mockedIConsumerGroup.CheckerCalls())

func (*IConsumerGroupMock) Close

func (mock *IConsumerGroupMock) Close(ctx context.Context, optFuncs ...kafka.OptFunc) error

Close calls CloseFunc.

func (*IConsumerGroupMock) CloseCalls

func (mock *IConsumerGroupMock) CloseCalls() []struct {
	Ctx      context.Context
	OptFuncs []kafka.OptFunc
}

CloseCalls gets all the calls that were made to Close. Check the length with:

len(mockedIConsumerGroup.CloseCalls())

func (*IConsumerGroupMock) Initialise

func (mock *IConsumerGroupMock) Initialise(ctx context.Context) error

Initialise calls InitialiseFunc.

func (*IConsumerGroupMock) InitialiseCalls

func (mock *IConsumerGroupMock) InitialiseCalls() []struct {
	Ctx context.Context
}

InitialiseCalls gets all the calls that were made to Initialise. Check the length with:

len(mockedIConsumerGroup.InitialiseCalls())

func (*IConsumerGroupMock) IsInitialised

func (mock *IConsumerGroupMock) IsInitialised() bool

IsInitialised calls IsInitialisedFunc.

func (*IConsumerGroupMock) IsInitialisedCalls

func (mock *IConsumerGroupMock) IsInitialisedCalls() []struct {
}

IsInitialisedCalls gets all the calls that were made to IsInitialised. Check the length with:

len(mockedIConsumerGroup.IsInitialisedCalls())

func (*IConsumerGroupMock) StopListeningToConsumer

func (mock *IConsumerGroupMock) StopListeningToConsumer(ctx context.Context) error

StopListeningToConsumer calls StopListeningToConsumerFunc.

func (*IConsumerGroupMock) StopListeningToConsumerCalls

func (mock *IConsumerGroupMock) StopListeningToConsumerCalls() []struct {
	Ctx context.Context
}

StopListeningToConsumerCalls gets all the calls that were made to StopListeningToConsumer. Check the length with:

len(mockedIConsumerGroup.StopListeningToConsumerCalls())

type IProducerMock

type IProducerMock struct {
	// AddHeaderFunc mocks the AddHeader method.
	AddHeaderFunc func(key string, value string)

	// ChannelsFunc mocks the Channels method.
	ChannelsFunc func() *kafka.ProducerChannels

	// CheckerFunc mocks the Checker method.
	CheckerFunc func(ctx context.Context, state *health.CheckState) error

	// CloseFunc mocks the Close method.
	CloseFunc func(ctx context.Context) error

	// InitialiseFunc mocks the Initialise method.
	InitialiseFunc func(ctx context.Context) error

	// IsInitialisedFunc mocks the IsInitialised method.
	IsInitialisedFunc func() bool
	// contains filtered or unexported fields
}

IProducerMock is a mock implementation of kafka.IProducer.

func TestSomethingThatUsesIProducer(t *testing.T) {

	// make and configure a mocked kafka.IProducer
	mockedIProducer := &IProducerMock{
		AddHeaderFunc: func(key string, value string)  {
			panic("mock out the AddHeader method")
		},
		ChannelsFunc: func() *kafka.ProducerChannels {
			panic("mock out the Channels method")
		},
		CheckerFunc: func(ctx context.Context, state *health.CheckState) error {
			panic("mock out the Checker method")
		},
		CloseFunc: func(ctx context.Context) error {
			panic("mock out the Close method")
		},
		InitialiseFunc: func(ctx context.Context) error {
			panic("mock out the Initialise method")
		},
		IsInitialisedFunc: func() bool {
			panic("mock out the IsInitialised method")
		},
	}

	// use mockedIProducer in code that requires kafka.IProducer
	// and then make assertions.

}

func (*IProducerMock) AddHeader added in v2.7.1

func (mock *IProducerMock) AddHeader(key string, value string)

AddHeader calls AddHeaderFunc.

func (*IProducerMock) AddHeaderCalls added in v2.7.1

func (mock *IProducerMock) AddHeaderCalls() []struct {
	Key   string
	Value string
}

AddHeaderCalls gets all the calls that were made to AddHeader. Check the length with:

len(mockedIProducer.AddHeaderCalls())

func (*IProducerMock) Channels

func (mock *IProducerMock) Channels() *kafka.ProducerChannels

Channels calls ChannelsFunc.

func (*IProducerMock) ChannelsCalls

func (mock *IProducerMock) ChannelsCalls() []struct {
}

ChannelsCalls gets all the calls that were made to Channels. Check the length with:

len(mockedIProducer.ChannelsCalls())

func (*IProducerMock) Checker

func (mock *IProducerMock) Checker(ctx context.Context, state *health.CheckState) error

Checker calls CheckerFunc.

func (*IProducerMock) CheckerCalls

func (mock *IProducerMock) CheckerCalls() []struct {
	Ctx   context.Context
	State *health.CheckState
}

CheckerCalls gets all the calls that were made to Checker. Check the length with:

len(mockedIProducer.CheckerCalls())

func (*IProducerMock) Close

func (mock *IProducerMock) Close(ctx context.Context) error

Close calls CloseFunc.

func (*IProducerMock) CloseCalls

func (mock *IProducerMock) CloseCalls() []struct {
	Ctx context.Context
}

CloseCalls gets all the calls that were made to Close. Check the length with:

len(mockedIProducer.CloseCalls())

func (*IProducerMock) Initialise

func (mock *IProducerMock) Initialise(ctx context.Context) error

Initialise calls InitialiseFunc.

func (*IProducerMock) InitialiseCalls

func (mock *IProducerMock) InitialiseCalls() []struct {
	Ctx context.Context
}

InitialiseCalls gets all the calls that were made to Initialise. Check the length with:

len(mockedIProducer.InitialiseCalls())

func (*IProducerMock) IsInitialised

func (mock *IProducerMock) IsInitialised() bool

IsInitialised calls IsInitialisedFunc.

func (*IProducerMock) IsInitialisedCalls

func (mock *IProducerMock) IsInitialisedCalls() []struct {
}

IsInitialisedCalls gets all the calls that were made to IsInitialised. Check the length with:

len(mockedIProducer.IsInitialisedCalls())

type Message

type Message struct {
	*MessageMock
	// contains filtered or unexported fields
}

Message allows a mock message to return the configured data, and capture whether commit has been called.

func NewMessage

func NewMessage(data []byte, offset int64, hValue ...TestHeader) *Message

NewMessage returns a new mock message containing the given data.

func (Message) ContextFunc added in v2.7.1

func (internal Message) ContextFunc() context.Context

Context returns a context with traceid.

func (Message) GetHeaderFunc added in v2.7.1

func (internal Message) GetHeaderFunc(key string) string

GetHeader takes a key for the header and returns the value if the key exist in the header.

func (Message) IsCommitted added in v2.1.0

func (internal Message) IsCommitted() bool

IsCommittedFunc returns true if the message offset was committed.

func (Message) IsMarked added in v2.1.0

func (internal Message) IsMarked() bool

IsMarked returns true if the message was marked as consumed.

type MessageConsumer

type MessageConsumer struct {
	*IConsumerGroupMock
	// contains filtered or unexported fields
}

MessageConsumer is an extension of the moq ConsumerGroup, with channels and implementation of required functions to emulate a fully functional Kafka ConsumerGroup

func NewMessageConsumer

func NewMessageConsumer(isInitialisedAtCreationTime bool) *MessageConsumer

NewMessageConsumer creates a testing consumer with new consumerGroupChannels. isInitialisedAtCreationTime determines if the consumer is initialised or not when it's created

func NewMessageConsumerWithChannels

func NewMessageConsumerWithChannels(cgChannels *kafka.ConsumerGroupChannels, isInitialisedAtCreationTime bool) *MessageConsumer

NewMessageConsumerWithChannels creates a testing consumer with the provided consumerGroupChannels isInitialisedAtCreationTime determines if the consumer is initialised or not when it's created

type MessageMock

type MessageMock struct {
	// CommitFunc mocks the Commit method.
	CommitFunc func()

	// CommitAndReleaseFunc mocks the CommitAndRelease method.
	CommitAndReleaseFunc func()

	// ContextFunc mocks the Context method.
	ContextFunc func() context.Context

	// GetDataFunc mocks the GetData method.
	GetDataFunc func() []byte

	// GetHeaderFunc mocks the GetHeader method.
	GetHeaderFunc func(key string) string

	// MarkFunc mocks the Mark method.
	MarkFunc func()

	// OffsetFunc mocks the Offset method.
	OffsetFunc func() int64

	// ReleaseFunc mocks the Release method.
	ReleaseFunc func()

	// UpstreamDoneFunc mocks the UpstreamDone method.
	UpstreamDoneFunc func() chan struct{}
	// contains filtered or unexported fields
}

MessageMock is a mock implementation of kafka.Message.

func TestSomethingThatUsesMessage(t *testing.T) {

	// make and configure a mocked kafka.Message
	mockedMessage := &MessageMock{
		CommitFunc: func()  {
			panic("mock out the Commit method")
		},
		CommitAndReleaseFunc: func()  {
			panic("mock out the CommitAndRelease method")
		},
		ContextFunc: func() context.Context {
			panic("mock out the Context method")
		},
		GetDataFunc: func() []byte {
			panic("mock out the GetData method")
		},
		GetHeaderFunc: func(key string) string {
			panic("mock out the GetHeader method")
		},
		MarkFunc: func()  {
			panic("mock out the Mark method")
		},
		OffsetFunc: func() int64 {
			panic("mock out the Offset method")
		},
		ReleaseFunc: func()  {
			panic("mock out the Release method")
		},
		UpstreamDoneFunc: func() chan struct{} {
			panic("mock out the UpstreamDone method")
		},
	}

	// use mockedMessage in code that requires kafka.Message
	// and then make assertions.

}

func (*MessageMock) Commit

func (mock *MessageMock) Commit()

Commit calls CommitFunc.

func (*MessageMock) CommitAndRelease added in v2.1.0

func (mock *MessageMock) CommitAndRelease()

CommitAndRelease calls CommitAndReleaseFunc.

func (*MessageMock) CommitAndReleaseCalls added in v2.1.0

func (mock *MessageMock) CommitAndReleaseCalls() []struct {
}

CommitAndReleaseCalls gets all the calls that were made to CommitAndRelease. Check the length with:

len(mockedMessage.CommitAndReleaseCalls())

func (*MessageMock) CommitCalls

func (mock *MessageMock) CommitCalls() []struct {
}

CommitCalls gets all the calls that were made to Commit. Check the length with:

len(mockedMessage.CommitCalls())

func (*MessageMock) Context added in v2.6.0

func (mock *MessageMock) Context() context.Context

Context calls ContextFunc.

func (*MessageMock) ContextCalls added in v2.6.0

func (mock *MessageMock) ContextCalls() []struct {
}

ContextCalls gets all the calls that were made to Context. Check the length with:

len(mockedMessage.ContextCalls())

func (*MessageMock) GetData

func (mock *MessageMock) GetData() []byte

GetData calls GetDataFunc.

func (*MessageMock) GetDataCalls

func (mock *MessageMock) GetDataCalls() []struct {
}

GetDataCalls gets all the calls that were made to GetData. Check the length with:

len(mockedMessage.GetDataCalls())

func (*MessageMock) GetHeader added in v2.6.0

func (mock *MessageMock) GetHeader(key string) string

GetHeader calls GetHeaderFunc.

func (*MessageMock) GetHeaderCalls added in v2.6.0

func (mock *MessageMock) GetHeaderCalls() []struct {
	Key string
}

GetHeaderCalls gets all the calls that were made to GetHeader. Check the length with:

len(mockedMessage.GetHeaderCalls())

func (*MessageMock) Mark added in v2.1.0

func (mock *MessageMock) Mark()

Mark calls MarkFunc.

func (*MessageMock) MarkCalls added in v2.1.0

func (mock *MessageMock) MarkCalls() []struct {
}

MarkCalls gets all the calls that were made to Mark. Check the length with:

len(mockedMessage.MarkCalls())

func (*MessageMock) Offset

func (mock *MessageMock) Offset() int64

Offset calls OffsetFunc.

func (*MessageMock) OffsetCalls

func (mock *MessageMock) OffsetCalls() []struct {
}

OffsetCalls gets all the calls that were made to Offset. Check the length with:

len(mockedMessage.OffsetCalls())

func (*MessageMock) Release added in v2.1.0

func (mock *MessageMock) Release()

Release calls ReleaseFunc.

func (*MessageMock) ReleaseCalls added in v2.1.0

func (mock *MessageMock) ReleaseCalls() []struct {
}

ReleaseCalls gets all the calls that were made to Release. Check the length with:

len(mockedMessage.ReleaseCalls())

func (*MessageMock) UpstreamDone

func (mock *MessageMock) UpstreamDone() chan struct{}

UpstreamDone calls UpstreamDoneFunc.

func (*MessageMock) UpstreamDoneCalls

func (mock *MessageMock) UpstreamDoneCalls() []struct {
}

UpstreamDoneCalls gets all the calls that were made to UpstreamDone. Check the length with:

len(mockedMessage.UpstreamDoneCalls())

type MessageProducer

type MessageProducer struct {
	*IProducerMock
	// contains filtered or unexported fields
}

MessageProducer is an extension of the moq Producer, with channels and implementation of required functions to emulate a fully functional kafka Producer.

func NewMessageProducer

func NewMessageProducer(isInitialisedAtCreationTime bool) *MessageProducer

NewMessageProducer creates a testing producer with new producerChannels. isInitialisedAtCreationTime determines if the producer is initialised or not when it's created

func NewMessageProducerWithChannels

func NewMessageProducerWithChannels(pChannels *kafka.ProducerChannels, isInitialisedAtCreationTime bool) *MessageProducer

NewMessageProducerWithChannels creates a testing producer with the provided producerChannels. isInitialisedAtCreationTime determines if the producer is initialised or not when it's created

func (MessageProducer) AddHeaderFunc added in v2.7.3

func (internal MessageProducer) AddHeaderFunc(key, value string)

type TestHeader added in v2.7.2

type TestHeader map[string]string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL