github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zpool/release_test.go (about) 1 package zpool 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 8 "github.com/sohaha/zlsgo" 9 "github.com/sohaha/zlsgo/zfile" 10 "github.com/sohaha/zlsgo/zutil" 11 ) 12 13 func TestPoolRelease(t *testing.T) { 14 tt := zlsgo.NewTest(t) 15 16 p := New(10) 17 _ = p.PreInit() 18 for i := 0; i < 4; i++ { 19 _ = p.Do(func() { 20 time.Sleep(time.Second) 21 }) 22 } 23 tt.Equal(uint(10), p.Cap()) 24 timer, mem := zutil.WithRunContext(func() { 25 p.Close() 26 }) 27 tt.EqualTrue(timer >= time.Second) 28 t.Log(timer.String(), zfile.SizeFormat(mem)) 29 tt.Equal(uint(0), p.Cap()) 30 } 31 32 func TestPoolAutoRelease(t *testing.T) { 33 tt := zlsgo.NewTest(t) 34 35 var g sync.WaitGroup 36 p := New(10) 37 p.releaseTime = time.Second / 2 38 _ = p.PreInit() 39 40 for i := 0; i < 4; i++ { 41 g.Add(1) 42 _ = p.Do(func() { 43 g.Done() 44 }) 45 } 46 g.Wait() 47 tt.Equal(uint(10), p.Cap()) 48 time.Sleep(time.Second) 49 tt.Equal(uint(0), p.Cap()) 50 51 for i := 0; i < 6; i++ { 52 g.Add(1) 53 _ = p.Do(func() { 54 time.Sleep(time.Second) 55 tt.Equal(uint(6), p.Cap()) 56 g.Done() 57 }) 58 } 59 g.Wait() 60 tt.Equal(uint(6), p.Cap()) 61 62 time.Sleep(time.Second) 63 64 for i := 0; i < 6; i++ { 65 g.Add(1) 66 _ = p.Do(func() { 67 g.Done() 68 }) 69 } 70 g.Wait() 71 tt.EqualTrue(p.Cap() >= uint(1)) 72 time.Sleep(time.Second) 73 tt.Equal(uint(0), p.Cap()) 74 }