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