浏览代码

a few more tests

Evan Huus 12 年之前
父节点
当前提交
e6ec3441c4
共有 1 个文件被更改,包括 31 次插入7 次删除
  1. 31 7
      protocol/fetch_request_test.go

+ 31 - 7
protocol/fetch_request_test.go

@@ -7,17 +7,41 @@ import (
 
 var (
 	fetchRequestNoBlocks = []byte{
-		0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
-		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
+		0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00}
+
+	fetchRequestWithProperties = []byte{
+		0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xEF,
+		0x00, 0x00, 0x00, 0x00}
+
+	fetchRequestOneBlock = []byte{
+		0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x05, 't', 'o', 'p', 'i', 'c',
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x56}
 )
 
-func TestFetchRequestEncoding(t *testing.T) {
-	request := new(FetchRequest)
-	packet, err := encode(request)
+func testEncodable(t *testing.T, name string, in encoder, result []byte) {
+	packet, err := encode(in)
 	if err != nil {
 		t.Error(err)
 	}
-	if !bytes.Equal(packet, fetchRequestNoBlocks) {
-		t.Error("Encoding no blocks failed, got ", packet)
+	if !bytes.Equal(packet, result) {
+		t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", result)
 	}
 }
+
+func TestFetchRequestEncoding(t *testing.T) {
+	request := new(FetchRequest)
+	testEncodable(t, "no blocks", request, fetchRequestNoBlocks)
+
+	request.MaxWaitTime = 0x20
+	request.MinBytes = 0xEF
+	testEncodable(t, "with properties", request, fetchRequestWithProperties)
+
+	request.MaxWaitTime = 0
+	request.MinBytes = 0
+	request.AddBlock("topic", 0x12, 0x34, 0x56)
+	testEncodable(t, "one block", request, fetchRequestOneBlock)
+}