File: bytebuffer_test.go

package info (click to toggle)
golang-github-valyala-fasthttp 20160617-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 980 kB
  • sloc: makefile: 18
file content (43 lines) | stat: -rw-r--r-- 843 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package fasthttp

import (
	"fmt"
	"testing"
	"time"
)

func TestByteBufferAcquireReleaseSerial(t *testing.T) {
	testByteBufferAcquireRelease(t)
}

func TestByteBufferAcquireReleaseConcurrent(t *testing.T) {
	concurrency := 10
	ch := make(chan struct{}, concurrency)
	for i := 0; i < concurrency; i++ {
		go func() {
			testByteBufferAcquireRelease(t)
			ch <- struct{}{}
		}()
	}

	for i := 0; i < concurrency; i++ {
		select {
		case <-ch:
		case <-time.After(time.Second):
			t.Fatalf("timeout!")
		}
	}
}

func testByteBufferAcquireRelease(t *testing.T) {
	for i := 0; i < 10; i++ {
		b := AcquireByteBuffer()
		b.B = append(b.B, "num "...)
		b.B = AppendUint(b.B, i)
		expectedS := fmt.Sprintf("num %d", i)
		if string(b.B) != expectedS {
			t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
		}
		ReleaseByteBuffer(b)
	}
}