|
|
@@ -1,12 +1,16 @@
|
|
|
-package protocol
|
|
|
+package kafka
|
|
|
|
|
|
import (
|
|
|
+ "encoding/binary"
|
|
|
"fmt"
|
|
|
- "sarama/mock"
|
|
|
- "sarama/types"
|
|
|
+ "io"
|
|
|
+ "net"
|
|
|
+ "strconv"
|
|
|
"testing"
|
|
|
+ "time"
|
|
|
)
|
|
|
-// Broker is a mock Kafka broker. It consists of a TCP server on a kernel-selected localhost port that
|
|
|
+
|
|
|
+// MockBroker is a mock Kafka broker. It consists of a TCP server on a kernel-selected localhost port that
|
|
|
// accepts a single connection. It reads Kafka requests from that connection and returns each response
|
|
|
// from the channel provided at creation-time (if a response has a len of 0, nothing is sent, if a response
|
|
|
// the server sleeps for 250ms instead of reading a request).
|
|
|
@@ -16,7 +20,7 @@ import (
|
|
|
//
|
|
|
// It is not necessary to prefix message length or correlation ID to your response bytes, the server does that
|
|
|
// automatically as a convenience.
|
|
|
-type Broker struct {
|
|
|
+type MockBroker struct {
|
|
|
port int32
|
|
|
stopper chan bool
|
|
|
responses chan []byte
|
|
|
@@ -25,18 +29,18 @@ type Broker struct {
|
|
|
}
|
|
|
|
|
|
// Port is the kernel-select TCP port the broker is listening on.
|
|
|
-func (b *Broker) Port() int32 {
|
|
|
+func (b *MockBroker) Port() int32 {
|
|
|
return b.port
|
|
|
}
|
|
|
|
|
|
// Close closes the response channel originally provided, then waits to make sure
|
|
|
// that all requests/responses matched up before exiting.
|
|
|
-func (b *Broker) Close() {
|
|
|
+func (b *MockBroker) Close() {
|
|
|
close(b.responses)
|
|
|
<-b.stopper
|
|
|
}
|
|
|
|
|
|
-func (b *Broker) serverLoop() {
|
|
|
+func (b *MockBroker) serverLoop() {
|
|
|
defer close(b.stopper)
|
|
|
conn, err := b.listener.Accept()
|
|
|
if err != nil {
|
|
|
@@ -106,12 +110,12 @@ func (b *Broker) serverLoop() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// NewBroker launches a fake Kafka broker. It takes a testing.T as provided by the test framework and a channel of responses to use.
|
|
|
+// NewMockBroker launches a fake Kafka broker. It takes a testing.T as provided by the test framework and a channel of responses to use.
|
|
|
// If an error occurs it is simply logged to the testing.T and the broker exits.
|
|
|
-func NewBroker(t *testing.T, responses chan []byte) *Broker {
|
|
|
+func NewMockBroker(t *testing.T, responses chan []byte) *MockBroker {
|
|
|
var err error
|
|
|
|
|
|
- broker := new(Broker)
|
|
|
+ broker := new(MockBroker)
|
|
|
broker.stopper = make(chan bool)
|
|
|
broker.responses = responses
|
|
|
broker.t = t
|
|
|
@@ -209,7 +213,7 @@ func TestBrokerID(t *testing.T) {
|
|
|
|
|
|
func TestSimpleBrokerCommunication(t *testing.T) {
|
|
|
responses := make(chan []byte)
|
|
|
- mockBroker := mock.NewBroker(t, responses)
|
|
|
+ mockBroker := NewMockBroker(t, responses)
|
|
|
defer mockBroker.Close()
|
|
|
|
|
|
broker := NewBroker("localhost", mockBroker.Port())
|
|
|
@@ -253,7 +257,7 @@ var brokerTestTable = []struct {
|
|
|
{[]byte{},
|
|
|
func(t *testing.T, broker *Broker) {
|
|
|
request := ProduceRequest{}
|
|
|
- request.RequiredAcks = types.NO_RESPONSE
|
|
|
+ request.RequiredAcks = NO_RESPONSE
|
|
|
response, err := broker.Produce("clientID", &request)
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
@@ -266,7 +270,7 @@ var brokerTestTable = []struct {
|
|
|
{[]byte{0x00, 0x00, 0x00, 0x00},
|
|
|
func(t *testing.T, broker *Broker) {
|
|
|
request := ProduceRequest{}
|
|
|
- request.RequiredAcks = types.WAIT_FOR_LOCAL
|
|
|
+ request.RequiredAcks = WAIT_FOR_LOCAL
|
|
|
response, err := broker.Produce("clientID", &request)
|
|
|
if err != nil {
|
|
|
t.Error(err)
|