github.com/songzhibin97/gkit@v1.2.13/container/group/group_test.go (about) 1 package group 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestGroupGet(t *testing.T) { 11 count := 0 12 g := NewGroup(func() interface{} { 13 count++ 14 return count 15 }) 16 v := g.Get("user") 17 assert.Equal(t, 1, v.(int)) 18 19 v = g.Get("avatar") 20 assert.Equal(t, 2, v.(int)) 21 22 v = g.Get("user") 23 assert.Equal(t, 1, v.(int)) 24 assert.Equal(t, 2, count) 25 } 26 27 func TestGroupReset(t *testing.T) { 28 g := NewGroup(func() interface{} { 29 return 1 30 }) 31 g.Get("user") 32 call := false 33 g.ReSet(func() interface{} { 34 call = true 35 return 1 36 }) 37 38 length := 0 39 for range g.(*Group).objs { 40 length++ 41 } 42 43 assert.Equal(t, 0, length) 44 45 g.Get("user") 46 assert.Equal(t, true, call) 47 } 48 49 func TestGroupClear(t *testing.T) { 50 g := NewGroup(func() interface{} { 51 return 1 52 }) 53 g.Get("user") 54 length := 0 55 for range g.(*Group).objs { 56 length++ 57 } 58 assert.Equal(t, 1, length) 59 60 g.Clear() 61 length = 0 62 for range g.(*Group).objs { 63 length++ 64 } 65 assert.Equal(t, 0, length) 66 } 67 68 func BenchmarkGroupGet(b *testing.B) { 69 g := NewGroup(func() interface{} { 70 return 1 71 }) 72 for i := 0; i < b.N; i++ { 73 g.Get(strconv.Itoa(i)) 74 } 75 }