Bläddra i källkod

ProduceResponse decoding test

Evan Huus 12 år sedan
förälder
incheckning
02ac6ca64c
2 ändrade filer med 76 tillägg och 3 borttagningar
  1. 9 3
      protocol/encoder_decoder_test.go
  2. 67 0
      protocol/produce_response_test.go

+ 9 - 3
protocol/encoder_decoder_test.go

@@ -8,12 +8,18 @@ import (
 // no actual tests, just helper functions for testing structures that
 // implement the encoder or decoder interfaces
 
-func testEncodable(t *testing.T, name string, in encoder, result []byte) {
+func testEncodable(t *testing.T, name string, in encoder, expect []byte) {
 	packet, err := encode(in)
 	if err != nil {
 		t.Error(err)
+	} else if !bytes.Equal(packet, expect) {
+		t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", expect)
 	}
-	if !bytes.Equal(packet, result) {
-		t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", result)
+}
+
+func testDecodable(t *testing.T, name string, out decoder, in []byte) {
+	err := decode(in, out)
+	if err != nil {
+		t.Error("Decoding", name, "failed:", err)
 	}
 }

+ 67 - 0
protocol/produce_response_test.go

@@ -0,0 +1,67 @@
+package protocol
+
+import "testing"
+
+var (
+	produceResponseNoBlocks = []byte{
+		0x00, 0x00, 0x00, 0x00}
+
+	produceResponseManyBlocks = []byte{
+		0x00, 0x00, 0x00, 0x02,
+
+		0x00, 0x03, 'f', 'o', 'o',
+		0x00, 0x00, 0x00, 0x00,
+
+		0x00, 0x03, 'b', 'a', 'r',
+		0x00, 0x00, 0x00, 0x02,
+
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
+
+		0x00, 0x00, 0x00, 0x02,
+		0x00, 0x02,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
+)
+
+func TestProduceResponse(t *testing.T) {
+	response := ProduceResponse{}
+
+	testDecodable(t, "no blocks", &response, produceResponseNoBlocks)
+	if len(response.Blocks) != 0 {
+		t.Error("Decoding produced", len(response.Blocks), "topics where there were none")
+	}
+
+	testDecodable(t, "many blocks", &response, produceResponseManyBlocks)
+	if len(response.Blocks) != 2 {
+		t.Error("Decoding produced", len(response.Blocks), "topics where there were 2")
+	}
+	if len(response.Blocks["foo"]) != 0 {
+		t.Error("Decoding produced", len(response.Blocks["foo"]), "partitions for 'foo' where there were none")
+	}
+	if len(response.Blocks["bar"]) != 2 {
+		t.Error("Decoding produced", len(response.Blocks["bar"]), "partitions for 'bar' where there were two")
+	}
+	block := response.GetBlock("bar", 1)
+	if block == nil {
+		t.Error("Decoding did not produce a block for bar/1")
+	} else {
+		if block.Err != NO_ERROR {
+			t.Error("Decoding failed for bar/1/Err, got:", int16(block.Err))
+		}
+		if block.Offset != 0xFF {
+			t.Error("Decoding failed for bar/1/Offset, got:", block.Offset)
+		}
+	}
+	block = response.GetBlock("bar", 2)
+	if block == nil {
+		t.Error("Decoding did not produce a block for bar/2")
+	} else {
+		if block.Err != INVALID_MESSAGE {
+			t.Error("Decoding failed for bar/2/Err, got:", int16(block.Err))
+		}
+		if block.Offset != 0 {
+			t.Error("Decoding failed for bar/2/Offset, got:", block.Offset)
+		}
+	}
+}