From cc4650333ea2041250ae4b7579787faa7f366d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 25 Feb 2025 18:38:25 +0900 Subject: [PATCH] feat(pbuf): add more test --- pbuf/bytes_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pbuf/bytes_test.go b/pbuf/bytes_test.go index 027a1b2..21d44a6 100644 --- a/pbuf/bytes_test.go +++ b/pbuf/bytes_test.go @@ -4,8 +4,11 @@ import ( "bytes" "crypto/rand" "encoding/hex" + mrand "math/rand" "runtime" + "sync" "testing" + "time" ) // TestBytesSlice sometimes fails at first run because @@ -112,3 +115,28 @@ func TestBytesCopy(t *testing.T) { t.Fail() } } + +func TestBytesTransMultithread(t *testing.T) { + wg := sync.WaitGroup{} + for i := 0; i < 4096; i++ { + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(time.Millisecond * time.Duration(mrand.Intn(10))) + buf := NewBytes(65536) + refer := make([]byte, 65536) + rand.Read(refer) + copy(buf.Bytes(), refer) + wg.Add(1) + go func(buf Bytes) { + defer wg.Done() + time.Sleep(time.Millisecond * time.Duration(mrand.Intn(10))) + if !bytes.Equal(refer, buf.Bytes()) { + panic("unexpected") + } + buf.Destroy() + }(buf.Trans()) + }() + } + wg.Wait() +}