Browse Source

Another client test

Evan Huus 12 years ago
parent
commit
e72806c9a3
1 changed files with 63 additions and 0 deletions
  1. 63 0
      kafka/client_test.go

+ 63 - 0
kafka/client_test.go

@@ -92,3 +92,66 @@ func TestClientMetadata(t *testing.T) {
 
 	client.Close()
 }
+
+func TestClientRefreshBehaviour(t *testing.T) {
+	responses := make(chan []byte, 3)
+	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, 0xaa,
+		0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00}
+	binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
+	responses <- response
+	responses <- []byte{
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x00,
+		0x00, 0x07, 'm', 'y', 'T', 'o', 'p', 'i', 'c',
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x05,
+		0x00, 0x00, 0x00, 0x0e,
+		0xFF, 0xFF, 0xFF, 0xFF,
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00}
+	responses <- []byte{
+		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, 0x0b,
+		0x00, 0x00, 0x00, 0xaa,
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00}
+
+	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] != 0xb {
+		t.Error("Client returned incorrect partitions for myTopic:", parts)
+	}
+
+	tst, err := client.leader("myTopic", 0xb)
+	if err != nil {
+		t.Error(err)
+	} else if tst.ID() != 0xaa {
+		t.Error("Leader for myTopic had incorrect ID.")
+	}
+
+	client.disconnectBroker(tst)
+
+	client.Close()
+}