Ver Fonte

Test metdata requests

Evan Huus há 12 anos atrás
pai
commit
bb51920130

+ 19 - 0
protocol/encoder_decoder_test.go

@@ -0,0 +1,19 @@
+package protocol
+
+import (
+	"bytes"
+	"testing"
+)
+
+// 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) {
+	packet, err := encode(in)
+	if err != nil {
+		t.Error(err)
+	}
+	if !bytes.Equal(packet, result) {
+		t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", result)
+	}
+}

+ 2 - 15
protocol/fetch_request_test.go

@@ -1,9 +1,6 @@
 package protocol
 
-import (
-	"bytes"
-	"testing"
-)
+import "testing"
 
 var (
 	fetchRequestNoBlocks = []byte{
@@ -22,17 +19,7 @@ var (
 		0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x56}
 )
 
-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, result) {
-		t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", result)
-	}
-}
-
-func TestFetchRequestEncoding(t *testing.T) {
+func TestFetchRequest(t *testing.T) {
 	request := new(FetchRequest)
 	testEncodable(t, "no blocks", request, fetchRequestNoBlocks)
 

+ 29 - 0
protocol/metadata_request_test.go

@@ -0,0 +1,29 @@
+package protocol
+
+import "testing"
+
+var (
+	metadataRequestNoTopics = []byte{
+		0x00, 0x00, 0x00, 0x00}
+
+	metadataRequestOneTopic = []byte{
+		0x00, 0x00, 0x00, 0x01,
+		0x00, 0x06,  't',  'o',  'p',  'i',  'c',  '1'}
+
+	metadataRequestThreeTopics = []byte{
+		0x00, 0x00, 0x00, 0x03,
+		0x00, 0x03,  'f',  'o',  'o',
+		0x00, 0x03,  'b',  'a',  'r',
+		0x00, 0x03,  'b',  'a',  'z'}
+)
+
+func TestMetadataRequest(t *testing.T) {
+	request := new(MetadataRequest)
+	testEncodable(t, "no topics", request, metadataRequestNoTopics)
+
+	request.Topics = []string{"topic1"}
+	testEncodable(t, "one topic", request, metadataRequestOneTopic)
+
+	request.Topics = []string{"foo", "bar", "baz"}
+	testEncodable(t, "three topics", request, metadataRequestThreeTopics)
+}