github.com/cloudwego/hertz@v0.9.3/pkg/common/bytebufferpool/bytebuffer_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  	"bytes"
    46  	"fmt"
    47  	"io"
    48  	"testing"
    49  	"time"
    50  )
    51  
    52  func TestByteBufferReadFrom(t *testing.T) {
    53  	prefix := "foobar"
    54  	expectedS := "asadfsdafsadfasdfisdsdfa"
    55  	prefixLen := int64(len(prefix))
    56  	expectedN := int64(len(expectedS))
    57  
    58  	var bb ByteBuffer
    59  	bb.WriteString(prefix)
    60  
    61  	rf := (io.ReaderFrom)(&bb)
    62  	for i := 0; i < 20; i++ {
    63  		r := bytes.NewBufferString(expectedS)
    64  		n, err := rf.ReadFrom(r)
    65  		if n != expectedN {
    66  			t.Fatalf("unexpected n=%d. Expecting %d. iteration %d", n, expectedN, i)
    67  		}
    68  		if err != nil {
    69  			t.Fatalf("unexpected error: %s", err)
    70  		}
    71  		bbLen := int64(bb.Len())
    72  		expectedLen := prefixLen + int64(i+1)*expectedN
    73  		if bbLen != expectedLen {
    74  			t.Fatalf("unexpected byteBuffer length: %d. Expecting %d", bbLen, expectedLen)
    75  		}
    76  		for j := 0; j < i; j++ {
    77  			start := prefixLen + int64(j)*expectedN
    78  			b := bb.B[start : start+expectedN]
    79  			if string(b) != expectedS {
    80  				t.Fatalf("unexpected byteBuffer contents: %q. Expecting %q", b, expectedS)
    81  			}
    82  		}
    83  	}
    84  }
    85  
    86  func TestByteBufferWriteTo(t *testing.T) {
    87  	expectedS := "foobarbaz"
    88  	var bb ByteBuffer
    89  	bb.WriteString(expectedS[:3])
    90  	bb.WriteString(expectedS[3:])
    91  
    92  	wt := (io.WriterTo)(&bb)
    93  	var w bytes.Buffer
    94  	for i := 0; i < 10; i++ {
    95  		n, err := wt.WriteTo(&w)
    96  		if n != int64(len(expectedS)) {
    97  			t.Fatalf("unexpected n returned from WriteTo: %d. Expecting %d", n, len(expectedS))
    98  		}
    99  		if err != nil {
   100  			t.Fatalf("unexpected error: %s", err)
   101  		}
   102  		s := w.String()
   103  		if s != expectedS {
   104  			t.Fatalf("unexpected string written %q. Expecting %q", s, expectedS)
   105  		}
   106  		w.Reset()
   107  	}
   108  }
   109  
   110  func TestByteBufferGetPutSerial(t *testing.T) {
   111  	testByteBufferGetPut(t)
   112  }
   113  
   114  func TestByteBufferGetPutConcurrent(t *testing.T) {
   115  	concurrency := 10
   116  	ch := make(chan struct{}, concurrency)
   117  	for i := 0; i < concurrency; i++ {
   118  		go func() {
   119  			testByteBufferGetPut(t)
   120  			ch <- struct{}{}
   121  		}()
   122  	}
   123  
   124  	for i := 0; i < concurrency; i++ {
   125  		select {
   126  		case <-ch:
   127  		case <-time.After(time.Second):
   128  			t.Fatalf("timeout!")
   129  		}
   130  	}
   131  }
   132  
   133  func testByteBufferGetPut(t *testing.T) {
   134  	for i := 0; i < 10; i++ {
   135  		expectedS := fmt.Sprintf("num %d", i)
   136  		b := Get()
   137  		b.B = append(b.B, "num "...)
   138  		b.B = append(b.B, fmt.Sprintf("%d", i)...)
   139  		if string(b.B) != expectedS {
   140  			t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
   141  		}
   142  		Put(b)
   143  	}
   144  }
   145  
   146  func testByteBufferGetString(t *testing.T) {
   147  	for i := 0; i < 10; i++ {
   148  		expectedS := fmt.Sprintf("num %d", i)
   149  		b := Get()
   150  		b.SetString(expectedS)
   151  		if b.String() != expectedS {
   152  			t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
   153  		}
   154  		Put(b)
   155  	}
   156  }
   157  
   158  func TestByteBufferGetStringSerial(t *testing.T) {
   159  	testByteBufferGetString(t)
   160  }
   161  
   162  func TestByteBufferGetStringConcurrent(t *testing.T) {
   163  	concurrency := 10
   164  	ch := make(chan struct{}, concurrency)
   165  	for i := 0; i < concurrency; i++ {
   166  		go func() {
   167  			testByteBufferGetString(t)
   168  			ch <- struct{}{}
   169  		}()
   170  	}
   171  
   172  	for i := 0; i < concurrency; i++ {
   173  		select {
   174  		case <-ch:
   175  		case <-time.After(time.Second):
   176  			t.Fatalf("timeout!")
   177  		}
   178  	}
   179  }