1
0
mirror of https://github.com/fumiama/orbyte.git synced 2026-06-05 18:20:31 +08:00

feat(pbuf): add more test

This commit is contained in:
源文雨
2025-02-25 18:38:25 +09:00
parent 4ebfc3e836
commit cc4650333e

View File

@@ -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()
}