Ai
1 Star 3 Fork 0

TimAndy/routine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_thread_local_test.go 15.58 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
package routine
import (
"math/rand"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
const (
concurrency = 500
loopTimes = 200
)
func TestSupplier(t *testing.T) {
var supplier Supplier[string] = func() string {
return "Hello"
}
assert.Equal(t, "Hello", supplier())
//
var fun func() string = supplier
assert.Equal(t, "Hello", fun())
}
//===
func TestNewThreadLocal_Single(t *testing.T) {
tls := NewThreadLocal[string]()
tls.Set("Hello")
assert.Equal(t, "Hello", tls.Get())
//
tls2 := NewThreadLocal[int]()
assert.Equal(t, "Hello", tls.Get())
tls2.Set(22)
assert.Equal(t, 22, tls2.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "", tls.Get())
assert.Equal(t, 0, tls2.Get())
})
task.Get()
}
func TestNewThreadLocal_Multi(t *testing.T) {
tls := NewThreadLocal[string]()
tls2 := NewThreadLocal[int]()
tls.Set("Hello")
tls2.Set(22)
assert.Equal(t, 22, tls2.Get())
assert.Equal(t, "Hello", tls.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "", tls.Get())
assert.Equal(t, 0, tls2.Get())
})
task.Get()
}
func TestNewThreadLocal_Concurrency(t *testing.T) {
tls := NewThreadLocal[uint64]()
tls2 := NewThreadLocal[uint64]()
//
tls2.Set(33)
assert.Equal(t, uint64(33), tls2.Get())
//
wg := &sync.WaitGroup{}
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
assert.Equal(t, uint64(0), tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
Go(func() {
assert.Equal(t, uint64(0), tls.Get())
assert.Equal(t, uint64(0), tls2.Get())
v := rand.Uint64()
v2 := rand.Uint64()
for j := 0; j < loopTimes; j++ {
tls.Set(v)
tmp := tls.Get()
assert.Equal(t, v, tmp)
//
tls2.Set(v2)
tmp2 := tls2.Get()
assert.Equal(t, v2, tmp2)
}
wg.Done()
})
}
wg.Wait()
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, uint64(0), tls.Get())
assert.Equal(t, uint64(0), tls2.Get())
})
task.Get()
}
func TestNewThreadLocal_Interface(t *testing.T) {
tls := NewThreadLocal[Cloneable]()
tls2 := NewThreadLocal[Cloneable]()
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
func TestNewThreadLocal_Pointer(t *testing.T) {
tls := NewThreadLocal[*personCloneable]()
tls2 := NewThreadLocal[*personCloneable]()
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
//===
func TestNewThreadLocalWithInitial_Single(t *testing.T) {
tls := NewThreadLocalWithInitial[string](func() string {
return "Hello"
})
assert.Equal(t, "Hello", tls.Get())
//
tls2 := NewThreadLocalWithInitial[int](func() int {
return 22
})
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 22, tls2.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 22, tls2.Get())
})
task.Get()
}
func TestNewThreadLocalWithInitial_Multi(t *testing.T) {
tls := NewThreadLocalWithInitial[string](func() string {
return "Hello"
})
tls2 := NewThreadLocalWithInitial[int](func() int {
return 22
})
tls.Set("Hello")
tls2.Set(22)
assert.Equal(t, 22, tls2.Get())
assert.Equal(t, "Hello", tls.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 22, tls2.Get())
})
task.Get()
}
func TestNewThreadLocalWithInitial_Concurrency(t *testing.T) {
tls := NewThreadLocalWithInitial[any](func() any {
return "Hello"
})
tls2 := NewThreadLocalWithInitial[uint64](func() uint64 {
return uint64(22)
})
//
tls2.Set(33)
assert.Equal(t, uint64(33), tls2.Get())
//
wg := &sync.WaitGroup{}
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
Go(func() {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, uint64(22), tls2.Get())
v := rand.Uint64()
v2 := rand.Uint64()
for j := 0; j < loopTimes; j++ {
tls.Set(v)
tmp := tls.Get()
assert.Equal(t, v, tmp.(uint64))
//
tls2.Set(v2)
tmp2 := tls2.Get()
assert.Equal(t, v2, tmp2)
}
wg.Done()
})
}
wg.Wait()
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, uint64(22), tls2.Get())
})
task.Get()
}
func TestNewThreadLocalWithInitial_Interface(t *testing.T) {
tls := NewThreadLocalWithInitial[Cloneable](func() Cloneable {
return nil
})
tls2 := NewThreadLocalWithInitial[Cloneable](func() Cloneable {
return nil
})
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
func TestNewThreadLocalWithInitial_Pointer(t *testing.T) {
tls := NewThreadLocalWithInitial[*personCloneable](func() *personCloneable {
return nil
})
tls2 := NewThreadLocalWithInitial[*personCloneable](func() *personCloneable {
return nil
})
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
//===
func TestNewInheritableThreadLocal_Single(t *testing.T) {
tls := NewInheritableThreadLocal[string]()
tls.Set("Hello")
assert.Equal(t, "Hello", tls.Get())
//
tls2 := NewInheritableThreadLocal[int]()
assert.Equal(t, "Hello", tls.Get())
tls2.Set(22)
assert.Equal(t, 22, tls2.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 33, tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocal_Multi(t *testing.T) {
tls := NewInheritableThreadLocal[string]()
tls2 := NewInheritableThreadLocal[int]()
tls.Set("Hello")
tls2.Set(22)
assert.Equal(t, 22, tls2.Get())
assert.Equal(t, "Hello", tls.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 33, tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocal_Concurrency(t *testing.T) {
tls := NewInheritableThreadLocal[uint64]()
tls2 := NewInheritableThreadLocal[uint64]()
//
tls2.Set(33)
assert.Equal(t, uint64(33), tls2.Get())
//
wg := &sync.WaitGroup{}
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
assert.Equal(t, uint64(0), tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
Go(func() {
assert.Equal(t, uint64(0), tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
v := rand.Uint64()
v2 := rand.Uint64()
for j := 0; j < loopTimes; j++ {
tls.Set(v)
tmp := tls.Get()
assert.Equal(t, v, tmp)
//
tls2.Set(v2)
tmp2 := tls2.Get()
assert.Equal(t, v2, tmp2)
}
wg.Done()
})
}
wg.Wait()
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, uint64(0), tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocal_Interface(t *testing.T) {
tls := NewInheritableThreadLocal[Cloneable]()
tls2 := NewInheritableThreadLocal[Cloneable]()
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocal_Pointer(t *testing.T) {
tls := NewInheritableThreadLocal[*personCloneable]()
tls2 := NewInheritableThreadLocal[*personCloneable]()
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
//===
func TestNewInheritableThreadLocalWithInitial_Single(t *testing.T) {
tls := NewInheritableThreadLocalWithInitial[string](func() string {
return "Hello"
})
assert.Equal(t, "Hello", tls.Get())
//
tls2 := NewInheritableThreadLocalWithInitial[int](func() int {
return 22
})
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 22, tls2.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 33, tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocalWithInitial_Multi(t *testing.T) {
tls := NewInheritableThreadLocalWithInitial[string](func() string {
return "Hello"
})
tls2 := NewInheritableThreadLocalWithInitial[int](func() int {
return 22
})
tls.Set("Hello")
tls2.Set(22)
assert.Equal(t, 22, tls2.Get())
assert.Equal(t, "Hello", tls.Get())
//
tls2.Set(33)
assert.Equal(t, 33, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, 33, tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocalWithInitial_Concurrency(t *testing.T) {
tls := NewInheritableThreadLocalWithInitial[any](func() any {
return "Hello"
})
tls2 := NewInheritableThreadLocalWithInitial[uint64](func() uint64 {
return uint64(22)
})
//
tls2.Set(33)
assert.Equal(t, uint64(33), tls2.Get())
//
wg := &sync.WaitGroup{}
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
Go(func() {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
v := rand.Uint64()
v2 := rand.Uint64()
for j := 0; j < loopTimes; j++ {
tls.Set(v)
tmp := tls.Get()
assert.Equal(t, v, tmp.(uint64))
//
tls2.Set(v2)
tmp2 := tls2.Get()
assert.Equal(t, v2, tmp2)
}
wg.Done()
})
}
wg.Wait()
//
task := GoWait(func(token CancelToken) {
assert.Equal(t, "Hello", tls.Get())
assert.Equal(t, uint64(33), tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocalWithInitial_Interface(t *testing.T) {
tls := NewInheritableThreadLocalWithInitial[Cloneable](func() Cloneable {
return nil
})
tls2 := NewInheritableThreadLocalWithInitial[Cloneable](func() Cloneable {
return nil
})
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
func TestNewInheritableThreadLocalWithInitial_Pointer(t *testing.T) {
tls := NewInheritableThreadLocalWithInitial[*personCloneable](func() *personCloneable {
return nil
})
tls2 := NewInheritableThreadLocalWithInitial[*personCloneable](func() *personCloneable {
return nil
})
//
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(nil)
tls2.Set(nil)
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
tls.Set(&personCloneable{Id: 1, Name: "Hello"})
tls2.Set(&personCloneable{Id: 1, Name: "Hello"})
assert.NotNil(t, tls.Get())
assert.NotNil(t, tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
//
task := GoWait(func(token CancelToken) {
assert.Nil(t, tls.Get())
assert.Nil(t, tls2.Get())
})
task.Get()
}
//===
// BenchmarkThreadLocal-8 13636471 94.17 ns/op 7 B/op 0 allocs/op
func BenchmarkThreadLocal(b *testing.B) {
tlsCount := 100
tlsSlice := make([]ThreadLocal[int], tlsCount)
for i := 0; i < tlsCount; i++ {
tlsSlice[i] = NewThreadLocal[int]()
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
index := i % tlsCount
tls := tlsSlice[index]
initValue := tls.Get()
if initValue != 0 {
b.Fail()
}
tls.Set(i)
if tls.Get() != i {
b.Fail()
}
tls.Remove()
}
}
// BenchmarkThreadLocalWithInitial-8 13674153 86.76 ns/op 7 B/op 0 allocs/op
func BenchmarkThreadLocalWithInitial(b *testing.B) {
tlsCount := 100
tlsSlice := make([]ThreadLocal[int], tlsCount)
for i := 0; i < tlsCount; i++ {
index := i
tlsSlice[i] = NewThreadLocalWithInitial[int](func() int {
return index
})
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
index := i % tlsCount
tls := tlsSlice[index]
initValue := tls.Get()
if initValue != index {
b.Fail()
}
tls.Set(i)
if tls.Get() != i {
b.Fail()
}
tls.Remove()
}
}
// BenchmarkInheritableThreadLocal-8 13917819 84.27 ns/op 7 B/op 0 allocs/op
func BenchmarkInheritableThreadLocal(b *testing.B) {
tlsCount := 100
tlsSlice := make([]ThreadLocal[int], tlsCount)
for i := 0; i < tlsCount; i++ {
tlsSlice[i] = NewInheritableThreadLocal[int]()
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
index := i % tlsCount
tls := tlsSlice[index]
initValue := tls.Get()
if initValue != 0 {
b.Fail()
}
tls.Set(i)
if tls.Get() != i {
b.Fail()
}
tls.Remove()
}
}
// BenchmarkInheritableThreadLocalWithInitial-8 13483130 90.03 ns/op 7 B/op 0 allocs/op
func BenchmarkInheritableThreadLocalWithInitial(b *testing.B) {
tlsCount := 100
tlsSlice := make([]ThreadLocal[int], tlsCount)
for i := 0; i < tlsCount; i++ {
index := i
tlsSlice[i] = NewInheritableThreadLocalWithInitial[int](func() int {
return index
})
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
index := i % tlsCount
tls := tlsSlice[index]
initValue := tls.Get()
if initValue != index {
b.Fail()
}
tls.Set(i)
if tls.Get() != i {
b.Fail()
}
tls.Remove()
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/timandy/routine.git
git@gitee.com:timandy/routine.git
timandy
routine
routine
main

搜索帮助