Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 8d9596e

Browse files
committed
fix the bug in unit tests about sync.Pool
Signed-off-by: SataQiu <[email protected]>
1 parent 60051a9 commit 8d9596e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pkg/pool/buffer_pool_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// BufferPool is no-op under race detector, so all these tests do not work.
2+
// +build !race
3+
14
/*
25
* Copyright The Dragonfly Authors.
36
*
@@ -18,6 +21,7 @@ package pool
1821

1922
import (
2023
"fmt"
24+
"runtime"
2125
"testing"
2226

2327
"github.com/stretchr/testify/suite"
@@ -51,6 +55,11 @@ func (s *BufferPoolTestSuite) TestAcquireBuffer() {
5155
}
5256

5357
func (s *BufferPoolTestSuite) TestReleaseBuffer() {
58+
// Limit to 1 processor to make sure that the goroutine doesn't migrate
59+
// to another P between AcquireBuffer and ReleaseBuffer calls.
60+
prev := runtime.GOMAXPROCS(1)
61+
defer runtime.GOMAXPROCS(prev)
62+
5463
buf1 := AcquireBuffer()
5564
ReleaseBuffer(buf1)
5665
ReleaseBuffer(nil)

pkg/pool/writer_pool_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// WriterPool is no-op under race detector, so all these tests do not work.
2+
// +build !race
3+
14
/*
25
* Copyright The Dragonfly Authors.
36
*
@@ -19,13 +22,19 @@ package pool
1922
import (
2023
"bytes"
2124
"io/ioutil"
25+
"runtime"
2226
"sync"
2327
"testing"
2428

2529
"github.com/stretchr/testify/require"
2630
)
2731

2832
func TestWriter(t *testing.T) {
33+
// Limit to 1 processor to make sure that the goroutine doesn't migrate
34+
// to another P between AcquireWriter and ReleaseWriter calls.
35+
prev := runtime.GOMAXPROCS(1)
36+
defer runtime.GOMAXPROCS(prev)
37+
2938
tmp := writerPool
3039
writerPool = &sync.Pool{}
3140

0 commit comments

Comments
 (0)