github.com/QuangHoangHao/kafka-go@v0.4.36/kafka_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"reflect"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestMarshalUnmarshal(t *testing.T) {
    12  	values := []interface{}{
    13  		true,
    14  		false,
    15  
    16  		int8(0),
    17  		int8(1),
    18  		int8(math.MinInt8),
    19  		int8(math.MaxInt8),
    20  
    21  		int16(0),
    22  		int16(1),
    23  		int16(math.MinInt16),
    24  		int16(math.MaxInt16),
    25  
    26  		int32(0),
    27  		int32(1),
    28  		int32(math.MinInt32),
    29  		int32(math.MaxInt32),
    30  
    31  		int64(0),
    32  		int64(1),
    33  		int64(math.MinInt64),
    34  		int64(math.MaxInt64),
    35  
    36  		"",
    37  		"hello world!",
    38  
    39  		([]byte)(nil),
    40  		[]byte(""),
    41  		[]byte("hello world!"),
    42  
    43  		([]int32)(nil),
    44  		[]int32{},
    45  		[]int32{0, 1, 2, 3, 4},
    46  
    47  		struct{}{},
    48  		struct {
    49  			A int32
    50  			B string
    51  			C []byte
    52  		}{A: 1, B: "42", C: []byte{}},
    53  	}
    54  
    55  	for _, v := range values {
    56  		t.Run(fmt.Sprintf("%+v", v), func(t *testing.T) {
    57  			b, err := Marshal(v)
    58  			if err != nil {
    59  				t.Fatal("marshal error:", err)
    60  			}
    61  
    62  			x := reflect.New(reflect.TypeOf(v))
    63  
    64  			if err := Unmarshal(b, x.Interface()); err != nil {
    65  				t.Fatal("unmarshal error:", err)
    66  			}
    67  
    68  			if !reflect.DeepEqual(v, x.Elem().Interface()) {
    69  				t.Fatalf("values mismatch:\nexpected: %#v\nfound:   %#v\n", v, x.Elem().Interface())
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestVersionMarshalUnmarshal(t *testing.T) {
    76  	type T struct {
    77  		A int32  `kafka:"min=v0,max=v1"`
    78  		B string `kafka:"min=v1,max=v2"`
    79  		C []byte `kafka:"min=v2,max=v2,nullable"`
    80  	}
    81  
    82  	tests := []struct {
    83  		out T
    84  		ver Version
    85  	}{
    86  		{
    87  			out: T{A: 42},
    88  			ver: Version(0),
    89  		},
    90  	}
    91  
    92  	in := T{
    93  		A: 42,
    94  		B: "Hello World!",
    95  		C: []byte("question?"),
    96  	}
    97  
    98  	for _, test := range tests {
    99  		t.Run(strconv.Itoa(int(test.ver)), func(t *testing.T) {
   100  			b, err := test.ver.Marshal(in)
   101  			if err != nil {
   102  				t.Fatal("marshal error:", err)
   103  			}
   104  
   105  			x1 := test.out
   106  			x2 := T{}
   107  
   108  			if err := test.ver.Unmarshal(b, &x2); err != nil {
   109  				t.Fatal("unmarshal error:", err)
   110  			}
   111  
   112  			if !reflect.DeepEqual(x1, x2) {
   113  				t.Fatalf("values mismatch:\nexpected: %#v\nfound:   %#v\n", x1, x2)
   114  			}
   115  		})
   116  	}
   117  
   118  }
   119  
   120  type Struct struct {
   121  	A int32
   122  	B int32
   123  	C int32
   124  }
   125  
   126  var benchmarkValues = []interface{}{
   127  	true,
   128  	int8(1),
   129  	int16(1),
   130  	int32(1),
   131  	int64(1),
   132  	"Hello World!",
   133  	[]byte("Hello World!"),
   134  	[]int32{1, 2, 3},
   135  	Struct{A: 1, B: 2, C: 3},
   136  }
   137  
   138  func BenchmarkMarshal(b *testing.B) {
   139  	for _, v := range benchmarkValues {
   140  		b.Run(fmt.Sprintf("%T", v), func(b *testing.B) {
   141  			for i := 0; i < b.N; i++ {
   142  				_, err := Marshal(v)
   143  				if err != nil {
   144  					b.Fatal(err)
   145  				}
   146  			}
   147  		})
   148  	}
   149  }
   150  
   151  func BenchmarkUnmarshal(b *testing.B) {
   152  	for _, v := range benchmarkValues {
   153  		b.Run(fmt.Sprintf("%T", v), func(b *testing.B) {
   154  			data, err := Marshal(v)
   155  
   156  			if err != nil {
   157  				b.Fatal(err)
   158  			}
   159  
   160  			value := reflect.New(reflect.TypeOf(v))
   161  			ptr := value.Interface()
   162  			elem := value.Elem()
   163  			zero := reflect.Zero(reflect.TypeOf(v))
   164  
   165  			for i := 0; i < b.N; i++ {
   166  				if err := Unmarshal(data, ptr); err != nil {
   167  					b.Fatal(err)
   168  				}
   169  				elem.Set(zero)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  type testKafkaLogger struct {
   176  	Prefix string
   177  	T      *testing.T
   178  }
   179  
   180  func newTestKafkaLogger(t *testing.T, prefix string) Logger {
   181  	return &testKafkaLogger{Prefix: prefix, T: t}
   182  }
   183  
   184  func (l *testKafkaLogger) Printf(msg string, args ...interface{}) {
   185  	l.T.Helper()
   186  	if l.Prefix != "" {
   187  		l.T.Logf(l.Prefix+" "+msg, args...)
   188  	} else {
   189  		l.T.Logf(msg, args...)
   190  	}
   191  }