Browse Source

Add a test, fix a bug.

Actually caught a bug with the tests now, maybe they are useful :)
Evan Huus 12 years ago
parent
commit
19409ffdc5
2 changed files with 50 additions and 2 deletions
  1. 1 1
      kafka/client.go
  2. 49 1
      kafka/client_test.go

+ 1 - 1
kafka/client.go

@@ -203,7 +203,7 @@ func (client *Client) cachedPartitions(topic string) []int32 {
 		return nil
 	}
 
-	ret := make([]int32, len(partitions))
+	ret := make([]int32, 0, len(partitions))
 	for id, _ := range partitions {
 		ret = append(ret, id)
 	}

+ 49 - 1
kafka/client_test.go

@@ -21,7 +21,7 @@ func TestSimpleClient(t *testing.T) {
 	client.Close()
 }
 
-func TestExtraBroker(t *testing.T) {
+func TestClientExtraBrokers(t *testing.T) {
 	responses := make(chan []byte, 1)
 	mockBroker := mock.NewBroker(t, responses)
 	mockExtra := mock.NewBroker(t, make(chan []byte))
@@ -44,3 +44,51 @@ func TestExtraBroker(t *testing.T) {
 	}
 	client.Close()
 }
+
+func TestClientMetadata(t *testing.T) {
+	responses := make(chan []byte, 1)
+	mockBroker := mock.NewBroker(t, responses)
+	mockExtra := mock.NewBroker(t, make(chan []byte))
+	defer mockBroker.Close()
+	defer mockExtra.Close()
+
+	// return the extra mock as another available broker
+	response := []byte{
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x00, 0x00, 0x05,
+		0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
+		0x00, 0x00, 0x00, 0x00,
+
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x00,
+		0x00, 0x07, 'm', 'y', 'T', 'o', 'p', 'i', 'c',
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x05,
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00}
+	binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
+	responses <- response
+
+	client, err := NewClient("clientID", "localhost", mockBroker.Port())
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	parts, err := client.partitions("myTopic")
+	if err != nil {
+		t.Error(err)
+	} else if len(parts) != 1 || parts[0] != 0 {
+		t.Error("Client returned incorrect partitions for myTopic:", parts)
+	}
+
+	tst, err := client.leader("myTopic", 0)
+	if err != nil {
+		t.Error(err)
+	} else if tst.ID() != 5 {
+		t.Error("Leader for myTopic had incorrect ID.")
+	}
+
+	client.Close()
+}