github.com/deanMdreon/kafka-go@v0.4.32/syncgroup_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"io"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestGroupAssignment(t *testing.T) {
    12  	item := groupAssignment{
    13  		Version: 1,
    14  		Topics: map[string][]int32{
    15  			"a": {1, 2, 3},
    16  			"b": {4, 5},
    17  		},
    18  		UserData: []byte(`blah`),
    19  	}
    20  
    21  	b := bytes.NewBuffer(nil)
    22  	w := &writeBuffer{w: b}
    23  	item.writeTo(w)
    24  
    25  	var found groupAssignment
    26  	remain, err := (&found).readFrom(bufio.NewReader(b), b.Len())
    27  	if err != nil {
    28  		t.Error(err)
    29  		t.FailNow()
    30  	}
    31  	if remain != 0 {
    32  		t.Errorf("expected 0 remain, got %v", remain)
    33  		t.FailNow()
    34  	}
    35  	if !reflect.DeepEqual(item, found) {
    36  		t.Error("expected item and found to be the same")
    37  		t.FailNow()
    38  	}
    39  }
    40  
    41  func TestGroupAssignmentReadsFromZeroSize(t *testing.T) {
    42  	var item groupAssignment
    43  	remain, err := (&item).readFrom(bufio.NewReader(bytes.NewReader(nil)), 0)
    44  	if err != nil {
    45  		t.Error(err)
    46  		t.FailNow()
    47  	}
    48  	if remain != 0 {
    49  		t.Errorf("expected 0 remain, got %v", remain)
    50  		t.FailNow()
    51  	}
    52  	if item.Topics == nil {
    53  		t.Error("expected non nil Topics to be assigned")
    54  	}
    55  }
    56  
    57  func TestSyncGroupResponseV0(t *testing.T) {
    58  	item := syncGroupResponseV0{
    59  		ErrorCode:         2,
    60  		MemberAssignments: []byte(`blah`),
    61  	}
    62  
    63  	b := bytes.NewBuffer(nil)
    64  	w := &writeBuffer{w: b}
    65  	item.writeTo(w)
    66  
    67  	var found syncGroupResponseV0
    68  	remain, err := (&found).readFrom(bufio.NewReader(b), b.Len())
    69  	if err != nil {
    70  		t.Error(err)
    71  		t.FailNow()
    72  	}
    73  	if remain != 0 {
    74  		t.Errorf("expected 0 remain, got %v", remain)
    75  		t.FailNow()
    76  	}
    77  	if !reflect.DeepEqual(item, found) {
    78  		t.Error("expected item and found to be the same")
    79  		t.FailNow()
    80  	}
    81  }
    82  
    83  func BenchmarkSyncGroupResponseV0(t *testing.B) {
    84  	item := syncGroupResponseV0{
    85  		ErrorCode:         2,
    86  		MemberAssignments: []byte(`blah`),
    87  	}
    88  
    89  	b := bytes.NewBuffer(nil)
    90  	w := &writeBuffer{w: b}
    91  	item.writeTo(w)
    92  
    93  	r := bytes.NewReader(b.Bytes())
    94  	reader := bufio.NewReader(r)
    95  	size := b.Len()
    96  
    97  	for i := 0; i < t.N; i++ {
    98  		r.Seek(0, io.SeekStart)
    99  		var found syncGroupResponseV0
   100  		remain, err := (&found).readFrom(reader, size)
   101  		if err != nil {
   102  			t.Error(err)
   103  			t.FailNow()
   104  		}
   105  		if remain != 0 {
   106  			t.Errorf("expected 0 remain, got %v", remain)
   107  			t.FailNow()
   108  		}
   109  	}
   110  }