github.com/rbisecke/kafka-go@v0.4.27/discard_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"io"
     7  	"testing"
     8  )
     9  
    10  func TestDiscardN(t *testing.T) {
    11  	tests := []struct {
    12  		scenario string
    13  		function func(*testing.T, *bufio.Reader, int)
    14  	}{
    15  		{
    16  			scenario: "discard nothing",
    17  			function: func(t *testing.T, r *bufio.Reader, sz int) {
    18  				remain, err := discardN(r, sz, 0)
    19  				if err != nil {
    20  					t.Errorf("Expected no error, got %v", err)
    21  				}
    22  				if remain != sz {
    23  					t.Errorf("Expected all bytes remaining, got %d", remain)
    24  				}
    25  			},
    26  		},
    27  		{
    28  			scenario: "discard fewer than available",
    29  			function: func(t *testing.T, r *bufio.Reader, sz int) {
    30  				remain, err := discardN(r, sz, sz-1)
    31  				if err != nil {
    32  					t.Errorf("Expected no error, got %v", err)
    33  				}
    34  				if remain != 1 {
    35  					t.Errorf("Expected single remaining byte, got %d", remain)
    36  				}
    37  			},
    38  		},
    39  		{
    40  			scenario: "discard all available",
    41  			function: func(t *testing.T, r *bufio.Reader, sz int) {
    42  				remain, err := discardN(r, sz, sz)
    43  				if err != nil {
    44  					t.Errorf("Expected no error, got %v", err)
    45  				}
    46  				if remain != 0 {
    47  					t.Errorf("Expected no remaining bytes, got %d", remain)
    48  				}
    49  			},
    50  		},
    51  		{
    52  			scenario: "discard more than available",
    53  			function: func(t *testing.T, r *bufio.Reader, sz int) {
    54  				remain, err := discardN(r, sz, sz+1)
    55  				if err != errShortRead {
    56  					t.Errorf("Expected errShortRead, got %v", err)
    57  				}
    58  				if remain != 0 {
    59  					t.Errorf("Expected no remaining bytes, got %d", remain)
    60  				}
    61  			},
    62  		},
    63  		{
    64  			scenario: "discard returns error",
    65  			function: func(t *testing.T, r *bufio.Reader, sz int) {
    66  				remain, err := discardN(r, sz+2, sz+1)
    67  				if err != io.EOF {
    68  					t.Errorf("Expected EOF, got %v", err)
    69  				}
    70  				if remain != 2 {
    71  					t.Errorf("Expected single remaining bytes, got %d", remain)
    72  				}
    73  			},
    74  		},
    75  		{
    76  			scenario: "errShortRead doesn't mask error",
    77  			function: func(t *testing.T, r *bufio.Reader, sz int) {
    78  				remain, err := discardN(r, sz+1, sz+2)
    79  				if err != io.EOF {
    80  					t.Errorf("Expected EOF, got %v", err)
    81  				}
    82  				if remain != 1 {
    83  					t.Errorf("Expected single remaining bytes, got %d", remain)
    84  				}
    85  			},
    86  		},
    87  	}
    88  	for _, test := range tests {
    89  		t.Run(test.scenario, func(t *testing.T) {
    90  			msg := []byte("test message")
    91  			r := bufio.NewReader(bytes.NewReader(msg))
    92  			test.function(t, r, len(msg))
    93  		})
    94  	}
    95  }