github.com/cloudwego/kitex@v0.9.0/pkg/remote/default_bytebuf_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package remote 18 19 import ( 20 "testing" 21 22 "github.com/cloudwego/kitex/internal/test" 23 ) 24 25 func TestDefaultByteBuffer(t *testing.T) { 26 buf1 := NewReaderWriterBuffer(-1) 27 checkWritable(t, buf1) 28 checkReadable(t, buf1) 29 30 buf2 := NewReaderWriterBuffer(1024) 31 checkWritable(t, buf2) 32 checkReadable(t, buf2) 33 34 buf3 := buf2.NewBuffer() 35 checkWritable(t, buf3) 36 checkUnreadable(t, buf3) 37 38 buf4 := NewReaderWriterBuffer(-1) 39 _, err := buf3.Bytes() 40 test.Assert(t, err == nil, err) 41 err = buf4.AppendBuffer(buf3) 42 test.Assert(t, err == nil) 43 checkReadable(t, buf4) 44 } 45 46 func TestDefaultWriterBuffer(t *testing.T) { 47 buf := NewWriterBuffer(-1) 48 checkWritable(t, buf) 49 checkUnreadable(t, buf) 50 } 51 52 func TestDefaultReaderBuffer(t *testing.T) { 53 msg := "hello world" 54 b := []byte(msg + msg + msg + msg + msg) 55 buf := NewReaderBuffer(b) 56 checkUnwritable(t, buf) 57 checkReadable(t, buf) 58 } 59 60 func checkWritable(t *testing.T, buf ByteBuffer) { 61 msg := "hello world" 62 63 p, err := buf.Malloc(len(msg)) 64 test.Assert(t, err == nil, err) 65 test.Assert(t, len(p) == len(msg)) 66 copy(p, msg) 67 l := buf.MallocLen() 68 test.Assert(t, l == len(msg)) 69 l, err = buf.WriteString(msg) 70 test.Assert(t, err == nil, err) 71 test.Assert(t, l == len(msg)) 72 l, err = buf.WriteBinary([]byte(msg)) 73 test.Assert(t, err == nil, err) 74 test.Assert(t, l == len(msg)) 75 l, err = buf.Write([]byte(msg)) 76 test.Assert(t, err == nil, err) 77 test.Assert(t, l == len(msg)) 78 err = buf.Flush() 79 test.Assert(t, err == nil, err) 80 var n int 81 n, err = buf.Write([]byte(msg)) 82 test.Assert(t, err == nil, err) 83 test.Assert(t, n == len(msg)) 84 b, err := buf.Bytes() 85 test.Assert(t, err == nil, err) 86 test.Assert(t, string(b) == msg+msg+msg+msg+msg, string(b)) 87 } 88 89 func checkReadable(t *testing.T, buf ByteBuffer) { 90 msg := "hello world" 91 92 p, err := buf.Peek(len(msg)) 93 test.Assert(t, err == nil, err) 94 test.Assert(t, string(p) == msg) 95 err = buf.Skip(1 + len(msg)) 96 test.Assert(t, err == nil, err) 97 p, err = buf.Next(len(msg) - 1) 98 test.Assert(t, err == nil, err) 99 test.Assert(t, string(p) == msg[1:]) 100 n := buf.ReadableLen() 101 test.Assert(t, n == 3*len(msg), n) 102 n = buf.ReadLen() 103 test.Assert(t, n == 2*len(msg), n) 104 105 var s string 106 s, err = buf.ReadString(len(msg)) 107 test.Assert(t, err == nil, err) 108 test.Assert(t, s == msg) 109 p, err = buf.ReadBinary(len(msg)) 110 test.Assert(t, err == nil, err) 111 test.Assert(t, string(p) == msg) 112 p = make([]byte, len(msg)) 113 n, err = buf.Read(p) 114 test.Assert(t, err == nil, err) 115 test.Assert(t, string(p) == msg) 116 test.Assert(t, n == 11, n) 117 } 118 119 func checkUnwritable(t *testing.T, buf ByteBuffer) { 120 msg := "hello world" 121 _, err := buf.Malloc(len(msg)) 122 test.Assert(t, err != nil) 123 l := buf.MallocLen() 124 test.Assert(t, l == -1, l) 125 _, err = buf.WriteString(msg) 126 test.Assert(t, err != nil) 127 _, err = buf.WriteBinary([]byte(msg)) 128 test.Assert(t, err != nil) 129 err = buf.Flush() 130 test.Assert(t, err != nil) 131 var n int 132 n, err = buf.Write([]byte(msg)) 133 test.Assert(t, err != nil) 134 test.Assert(t, n == -1, n) 135 } 136 137 func checkUnreadable(t *testing.T, buf ByteBuffer) { 138 msg := "hello world" 139 _, err := buf.Peek(len(msg)) 140 test.Assert(t, err != nil) 141 err = buf.Skip(1) 142 test.Assert(t, err != nil) 143 _, err = buf.Next(len(msg) - 1) 144 test.Assert(t, err != nil) 145 n := buf.ReadableLen() 146 test.Assert(t, n == -1) 147 n = buf.ReadLen() 148 test.Assert(t, n == 0) 149 _, err = buf.ReadString(len(msg)) 150 test.Assert(t, err != nil) 151 _, err = buf.ReadBinary(len(msg)) 152 test.Assert(t, err != nil) 153 p := make([]byte, len(msg)) 154 n, err = buf.Read(p) 155 test.Assert(t, err != nil) 156 test.Assert(t, n == -1, n) 157 }