github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/protocol/prototest/request.go (about)

     1  package prototest
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"io"
     9  	"testing"
    10  
    11  	"github.com/segmentio/kafka-go/protocol"
    12  )
    13  
    14  func TestRequest(t *testing.T, version int16, msg protocol.Message) {
    15  	reset := load(msg)
    16  
    17  	t.Run(fmt.Sprintf("v%d", version), func(t *testing.T) {
    18  		b := &bytes.Buffer{}
    19  
    20  		if err := protocol.WriteRequest(b, version, 1234, "me", msg); err != nil {
    21  			t.Fatal(err)
    22  		}
    23  
    24  		reset()
    25  
    26  		t.Logf("\n%s\n", hex.Dump(b.Bytes()))
    27  
    28  		apiVersion, correlationID, clientID, req, err := protocol.ReadRequest(b)
    29  		if err != nil {
    30  			t.Fatal(err)
    31  		}
    32  		if apiVersion != version {
    33  			t.Errorf("api version mismatch: %d != %d", apiVersion, version)
    34  		}
    35  		if correlationID != 1234 {
    36  			t.Errorf("correlation id mismatch: %d != %d", correlationID, 1234)
    37  		}
    38  		if clientID != "me" {
    39  			t.Errorf("client id mismatch: %q != %q", clientID, "me")
    40  		}
    41  		if !deepEqual(msg, req) {
    42  			t.Errorf("request message mismatch:")
    43  			t.Logf("expected: %+v", msg)
    44  			t.Logf("found:    %+v", req)
    45  		}
    46  	})
    47  }
    48  
    49  // TestRequestWithOverride validates requests that have an overridden type. For requests with type overrides, we
    50  // double-serialize the request to ensure the resulting encoding of the overridden and original type are identical.
    51  func TestRequestWithOverride(t *testing.T, version int16, msg protocol.Message) {
    52  	reset := load(msg)
    53  
    54  	t.Run(fmt.Sprintf("v%d", version), func(t *testing.T) {
    55  		b1 := &bytes.Buffer{}
    56  
    57  		if err := protocol.WriteRequest(b1, version, 1234, "me", msg); err != nil {
    58  			t.Fatal(err)
    59  		}
    60  
    61  		reset()
    62  		t.Logf("\n%s\n", hex.Dump(b1.Bytes()))
    63  
    64  		_, _, _, req, err := protocol.ReadRequest(b1)
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  
    69  		b2 := &bytes.Buffer{}
    70  		if err := protocol.WriteRequest(b2, version, 1234, "me", req); err != nil {
    71  			t.Fatal(err)
    72  		}
    73  
    74  		if !deepEqual(b1, b2) {
    75  			t.Errorf("request message mismatch:")
    76  			t.Logf("expected: %+v", hex.Dump(b1.Bytes()))
    77  			t.Logf("found:    %+v", hex.Dump(b2.Bytes()))
    78  		}
    79  	})
    80  }
    81  
    82  func BenchmarkRequest(b *testing.B, version int16, msg protocol.Message) {
    83  	reset := load(msg)
    84  
    85  	b.Run(fmt.Sprintf("v%d", version), func(b *testing.B) {
    86  		buffer := &bytes.Buffer{}
    87  		buffer.Grow(1024)
    88  
    89  		b.Run("read", func(b *testing.B) {
    90  			w := io.Writer(buffer)
    91  
    92  			if err := protocol.WriteRequest(w, version, 1234, "client", msg); err != nil {
    93  				b.Fatal(err)
    94  			}
    95  
    96  			reset()
    97  
    98  			p := buffer.Bytes()
    99  			x := bytes.NewReader(p)
   100  			r := bufio.NewReader(x)
   101  
   102  			for i := 0; i < b.N; i++ {
   103  				_, _, _, req, err := protocol.ReadRequest(r)
   104  				if err != nil {
   105  					b.Fatal(err)
   106  				}
   107  				closeMessage(req)
   108  				x.Reset(p)
   109  				r.Reset(x)
   110  			}
   111  
   112  			b.SetBytes(int64(len(p)))
   113  			buffer.Reset()
   114  		})
   115  
   116  		b.Run("write", func(b *testing.B) {
   117  			w := io.Writer(buffer)
   118  			n := int64(0)
   119  
   120  			for i := 0; i < b.N; i++ {
   121  				if err := protocol.WriteRequest(w, version, 1234, "client", msg); err != nil {
   122  					b.Fatal(err)
   123  				}
   124  				reset()
   125  				n = int64(buffer.Len())
   126  				buffer.Reset()
   127  			}
   128  
   129  			b.SetBytes(n)
   130  		})
   131  	})
   132  }