github.com/cloudwego/hertz@v0.9.3/pkg/common/bytebufferpool/pool_test.go (about) 1 /* 2 * Copyright 2022 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 * The MIT License (MIT) 17 * 18 * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy 21 * of this software and associated documentation files (the "Software"), to deal 22 * in the Software without restriction, including without limitation the rights 23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 24 * copies of the Software, and to permit persons to whom the Software is 25 * furnished to do so, subject to the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included in 28 * all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 * THE SOFTWARE. 37 * 38 * This file may have been modified by CloudWeGo authors. All CloudWeGo 39 * Modifications are Copyright 2022 CloudWeGo Authors. 40 */ 41 42 package bytebufferpool 43 44 import ( 45 "math/rand" 46 "testing" 47 "time" 48 ) 49 50 func TestIndex(t *testing.T) { 51 testIndex(t, 0, 0) 52 testIndex(t, 1, 0) 53 54 testIndex(t, minSize-1, 0) 55 testIndex(t, minSize, 0) 56 testIndex(t, minSize+1, 1) 57 58 testIndex(t, 2*minSize-1, 1) 59 testIndex(t, 2*minSize, 1) 60 testIndex(t, 2*minSize+1, 2) 61 62 testIndex(t, maxSize-1, steps-1) 63 testIndex(t, maxSize, steps-1) 64 testIndex(t, maxSize+1, steps-1) 65 } 66 67 func testIndex(t *testing.T, n, expectedIdx int) { 68 idx := index(n) 69 if idx != expectedIdx { 70 t.Fatalf("unexpected idx for n=%d: %d. Expecting %d", n, idx, expectedIdx) 71 } 72 } 73 74 func TestPoolCalibrate(t *testing.T) { 75 for i := 0; i < steps*calibrateCallsThreshold; i++ { 76 n := 1004 77 if i%15 == 0 { 78 n = rand.Intn(15234) 79 } 80 testGetPut(t, n) 81 } 82 } 83 84 func TestPoolVariousSizesSerial(t *testing.T) { 85 testPoolVariousSizes(t) 86 } 87 88 func TestPoolVariousSizesConcurrent(t *testing.T) { 89 concurrency := 5 90 ch := make(chan struct{}) 91 for i := 0; i < concurrency; i++ { 92 go func() { 93 testPoolVariousSizes(t) 94 ch <- struct{}{} 95 }() 96 } 97 for i := 0; i < concurrency; i++ { 98 select { 99 case <-ch: 100 case <-time.After(10 * time.Second): 101 t.Fatalf("timeout") 102 } 103 } 104 } 105 106 func testPoolVariousSizes(t *testing.T) { 107 for i := 0; i < steps+1; i++ { 108 n := 1 << uint32(i) 109 110 testGetPut(t, n) 111 testGetPut(t, n+1) 112 testGetPut(t, n-1) 113 114 for j := 0; j < 10; j++ { 115 testGetPut(t, j+n) 116 } 117 } 118 } 119 120 func testGetPut(t *testing.T, n int) { 121 bb := Get() 122 if len(bb.B) > 0 { 123 t.Fatalf("non-empty byte buffer returned from acquire") 124 } 125 bb.B = allocNBytes(bb.B, n) 126 Put(bb) 127 } 128 129 func allocNBytes(dst []byte, n int) []byte { 130 diff := n - cap(dst) 131 if diff <= 0 { 132 return dst[:n] 133 } 134 return append(dst, make([]byte, diff)...) 135 }