From 8d9596e7571c54bedabb4783e5731da255e94e55 Mon Sep 17 00:00:00 2001 From: SataQiu <1527062125@qq.com> Date: Mon, 13 Jul 2020 15:12:02 +0800 Subject: [PATCH] fix the bug in unit tests about sync.Pool Signed-off-by: SataQiu <1527062125@qq.com> --- pkg/pool/buffer_pool_test.go | 9 +++++++++ pkg/pool/writer_pool_test.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/pool/buffer_pool_test.go b/pkg/pool/buffer_pool_test.go index e499dcae1..ceffefd8b 100644 --- a/pkg/pool/buffer_pool_test.go +++ b/pkg/pool/buffer_pool_test.go @@ -1,3 +1,6 @@ +// BufferPool is no-op under race detector, so all these tests do not work. +// +build !race + /* * Copyright The Dragonfly Authors. * @@ -18,6 +21,7 @@ package pool import ( "fmt" + "runtime" "testing" "github.com/stretchr/testify/suite" @@ -51,6 +55,11 @@ func (s *BufferPoolTestSuite) TestAcquireBuffer() { } func (s *BufferPoolTestSuite) TestReleaseBuffer() { + // Limit to 1 processor to make sure that the goroutine doesn't migrate + // to another P between AcquireBuffer and ReleaseBuffer calls. + prev := runtime.GOMAXPROCS(1) + defer runtime.GOMAXPROCS(prev) + buf1 := AcquireBuffer() ReleaseBuffer(buf1) ReleaseBuffer(nil) diff --git a/pkg/pool/writer_pool_test.go b/pkg/pool/writer_pool_test.go index 8d1fbb3b7..ee7f198bf 100644 --- a/pkg/pool/writer_pool_test.go +++ b/pkg/pool/writer_pool_test.go @@ -1,3 +1,6 @@ +// WriterPool is no-op under race detector, so all these tests do not work. +// +build !race + /* * Copyright The Dragonfly Authors. * @@ -19,6 +22,7 @@ package pool import ( "bytes" "io/ioutil" + "runtime" "sync" "testing" @@ -26,6 +30,11 @@ import ( ) func TestWriter(t *testing.T) { + // Limit to 1 processor to make sure that the goroutine doesn't migrate + // to another P between AcquireWriter and ReleaseWriter calls. + prev := runtime.GOMAXPROCS(1) + defer runtime.GOMAXPROCS(prev) + tmp := writerPool writerPool = &sync.Pool{}