github.com/QuangHoangHao/kafka-go@v0.4.36/protocol/describeconfigs/describeconfigs_test.go (about)

     1  package describeconfigs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/QuangHoangHao/kafka-go/protocol"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestResponse_Merge(t *testing.T) {
    15  	t.Run("happy path", func(t *testing.T) {
    16  		r := &Response{}
    17  
    18  		r1 := &Response{
    19  			Resources: []ResponseResource{
    20  				{ResourceName: "r1"},
    21  			},
    22  		}
    23  		r2 := &Response{
    24  			Resources: []ResponseResource{
    25  				{ResourceName: "r2"},
    26  			},
    27  		}
    28  
    29  		got, err := r.Merge([]protocol.Message{&Request{}}, []interface{}{r1, r2})
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  
    34  		want := &Response{
    35  			Resources: []ResponseResource{
    36  				{ResourceName: "r1"},
    37  				{ResourceName: "r2"},
    38  			},
    39  		}
    40  
    41  		if !reflect.DeepEqual(want, got) {
    42  			t.Fatalf("wanted response: \n%+v, got \n%+v", want, got)
    43  		}
    44  	})
    45  
    46  	t.Run("with errors", func(t *testing.T) {
    47  		r := &Response{}
    48  
    49  		r1 := &Response{
    50  			Resources: []ResponseResource{
    51  				{ResourceName: "r1"},
    52  			},
    53  		}
    54  
    55  		_, err := r.Merge([]protocol.Message{&Request{}}, []interface{}{r1, io.EOF})
    56  		if !errors.Is(err, io.EOF) {
    57  			t.Fatalf("wanted err io.EOF, got %v", err)
    58  		}
    59  	})
    60  
    61  	t.Run("panic with unexpected type", func(t *testing.T) {
    62  		defer func() {
    63  			msg := recover()
    64  			require.Equal(t, "BUG: result must be a message or an error but not string", fmt.Sprintf("%s", msg))
    65  		}()
    66  		r := &Response{}
    67  
    68  		r1 := &Response{
    69  			Resources: []ResponseResource{
    70  				{ResourceName: "r1"},
    71  			},
    72  		}
    73  
    74  		_, _ = r.Merge([]protocol.Message{&Request{}}, []interface{}{r1, "how did a string got here"})
    75  		t.Fatal("did not panic")
    76  	})
    77  }