github.com/simonmittag/ws@v1.1.0-rc.5.0.20210419231947-82b846128245/wsflate/cbuf_test.go (about) 1 package wsflate 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "testing" 9 ) 10 11 func TestSuffixReader(t *testing.T) { 12 for chunk := 1; chunk < 100; chunk++ { 13 var ( 14 data = []byte("hello, flate!") 15 name = fmt.Sprintf("chunk-%d", chunk) 16 ) 17 t.Run(name, func(t *testing.T) { 18 r := suffixedReader{ 19 r: bytes.NewReader(data), 20 suffix: [9]byte{ 21 1, 2, 3, 22 4, 5, 6, 23 7, 8, 9, 24 }, 25 } 26 var ( 27 act = make([]byte, 0, len(data)+len(r.suffix)) 28 p = make([]byte, chunk) 29 ) 30 for len(act) < cap(act) { 31 n, err := r.Read(p) 32 act = append(act, p[:n]...) 33 if err == io.EOF { 34 break 35 } 36 if err != nil { 37 t.Fatalf("unexpected Read() error: %v", err) 38 } 39 } 40 exp := append(data, r.suffix[:]...) 41 if !bytes.Equal(act, exp) { 42 t.Fatalf("unexpected bytes read: %#q; want %#q", act, exp) 43 } 44 }) 45 } 46 } 47 48 func TestCBuf(t *testing.T) { 49 for _, test := range []struct { 50 name string 51 stream [][]byte 52 expBody []byte 53 expTail []byte 54 }{ 55 { 56 stream: [][]byte{ 57 {1}, {2}, {3}, {4}, 58 }, 59 expTail: []byte{1, 2, 3, 4}, 60 }, 61 { 62 stream: [][]byte{ 63 {1, 2}, {3, 4}, 64 }, 65 expTail: []byte{1, 2, 3, 4}, 66 }, 67 { 68 stream: [][]byte{ 69 {1, 2, 3}, {4, 5, 6}, 70 }, 71 expBody: []byte{1, 2}, 72 expTail: []byte{3, 4, 5, 6}, 73 }, 74 { 75 stream: [][]byte{ 76 {1, 2, 3, 4}, {5, 6, 7, 8}, 77 }, 78 expBody: []byte{1, 2, 3, 4}, 79 expTail: []byte{5, 6, 7, 8}, 80 }, 81 { 82 stream: [][]byte{ 83 {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, 84 }, 85 expBody: []byte{1, 2, 3, 4, 5, 6}, 86 expTail: []byte{7, 8, 9, 10}, 87 }, 88 { 89 stream: [][]byte{ 90 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 91 }, 92 expBody: []byte{1, 2, 3, 4, 5, 6}, 93 expTail: []byte{7, 8, 9, 10}, 94 }, 95 { 96 name: "xxx", 97 stream: [][]byte{ 98 {1, 2, 3, 4, 5}, {6}, 99 }, 100 expBody: []byte{1, 2}, 101 expTail: []byte{3, 4, 5, 6}, 102 }, 103 } { 104 t.Run(test.name, func(t *testing.T) { 105 var buf bytes.Buffer 106 w := &cbuf{ 107 dst: &buf, 108 } 109 for _, bts := range test.stream { 110 n, err := w.Write(bts) 111 if err != nil { 112 t.Fatalf("unexpected error: %v", err) 113 } 114 if act, exp := n, len(bts); act != exp { 115 t.Fatalf( 116 "unexpected number of bytes written: %d; want %d", 117 act, exp, 118 ) 119 } 120 } 121 if act, exp := w.buf[:], test.expTail; !bytes.Equal(act, exp) { 122 t.Errorf( 123 "unexpected tail: %v; want %v", 124 act, exp, 125 ) 126 } 127 if act, exp := buf.Bytes(), test.expBody; !bytes.Equal(act, exp) { 128 t.Errorf( 129 "unexpected body: %v; want %v", 130 act, exp, 131 ) 132 } 133 }) 134 } 135 } 136 137 func BenchmarkCBuf(b *testing.B) { 138 for _, test := range []struct { 139 name string 140 chunk []byte 141 }{ 142 { 143 chunk: []byte{1, 2, 3, 4, 5}, 144 }, 145 { 146 chunk: []byte{1, 2, 3, 4}, 147 }, 148 } { 149 b.Run(test.name, func(b *testing.B) { 150 w := &cbuf{ 151 dst: ioutil.Discard, 152 } 153 for i := 0; i < b.N; i++ { 154 w.Write(test.chunk) 155 } 156 }) 157 } 158 }