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