github.com/xg0n/routine@v0.0.0-20240119033701-c364deb94aee/thread_local_map_test.go (about) 1 package routine 2 3 import ( 4 "math/rand" 5 "sync" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestObject(t *testing.T) { 12 var value entry = &object{} 13 assert.NotSame(t, unset, value) 14 // 15 var value2 entry = &object{} 16 assert.NotSame(t, value, value2) 17 // 18 var value3 any = unset 19 assert.Same(t, unset, value3) 20 } 21 22 func TestCreateInheritedMap(t *testing.T) { 23 wg := &sync.WaitGroup{} 24 wg.Add(1) 25 go func() { 26 thd := currentThread(true) 27 assert.NotNil(t, thd) 28 assert.Nil(t, thd.inheritableThreadLocals) 29 thd.inheritableThreadLocals = &threadLocalMap{} 30 assert.Nil(t, thd.inheritableThreadLocals.table) 31 assert.Nil(t, createInheritedMap()) 32 // 33 wg.Done() 34 }() 35 wg.Wait() 36 } 37 38 func TestCreateInheritedMap_Nil(t *testing.T) { 39 tls := NewInheritableThreadLocal[string]() 40 tls.Set("") 41 srcValue := tls.Get() 42 assert.Equal(t, "", srcValue) 43 assert.True(t, srcValue == "") 44 45 mp := createInheritedMap() 46 assert.NotNil(t, mp) 47 getValue := entryValue[string](mp.get(tls.(*inheritableThreadLocal[string]).index)) 48 assert.Equal(t, "", getValue) 49 assert.True(t, getValue == "") 50 51 mp2 := createInheritedMap() 52 assert.NotNil(t, mp2) 53 assert.NotSame(t, mp, mp2) 54 getValue2 := entryValue[string](mp2.get(tls.(*inheritableThreadLocal[string]).index)) 55 assert.Equal(t, "", getValue2) 56 assert.True(t, getValue2 == "") 57 } 58 59 func TestCreateInheritedMap_Value(t *testing.T) { 60 tls := NewInheritableThreadLocal[uint64]() 61 value := rand.Uint64() 62 tls.Set(value) 63 srcValue := tls.Get() 64 assert.NotSame(t, &value, &srcValue) 65 assert.Equal(t, value, srcValue) 66 67 mp := createInheritedMap() 68 assert.NotNil(t, mp) 69 getValue := entryValue[uint64](mp.get(tls.(*inheritableThreadLocal[uint64]).index)) 70 assert.NotSame(t, &value, &getValue) 71 assert.Equal(t, value, getValue) 72 73 mp2 := createInheritedMap() 74 assert.NotNil(t, mp2) 75 assert.NotSame(t, mp, mp2) 76 getValue2 := entryValue[uint64](mp2.get(tls.(*inheritableThreadLocal[uint64]).index)) 77 assert.NotSame(t, &value, &getValue2) 78 assert.Equal(t, value, getValue2) 79 } 80 81 func TestCreateInheritedMap_Struct(t *testing.T) { 82 tls := NewInheritableThreadLocal[personCloneable]() 83 value := personCloneable{Id: 1, Name: "Hello"} 84 tls.Set(value) 85 srcValue := tls.Get() 86 assert.NotSame(t, &value, &srcValue) 87 assert.Equal(t, value, srcValue) 88 89 mp := createInheritedMap() 90 assert.NotNil(t, mp) 91 getValue := entryValue[personCloneable](mp.get(tls.(*inheritableThreadLocal[personCloneable]).index)) 92 assert.NotSame(t, &value, &getValue) 93 assert.Equal(t, value, getValue) 94 95 mp2 := createInheritedMap() 96 assert.NotNil(t, mp2) 97 assert.NotSame(t, mp, mp2) 98 getValue2 := entryValue[personCloneable](mp2.get(tls.(*inheritableThreadLocal[personCloneable]).index)) 99 assert.NotSame(t, &value, &getValue2) 100 assert.Equal(t, value, getValue2) 101 } 102 103 func TestCreateInheritedMap_Pointer(t *testing.T) { 104 tls := NewInheritableThreadLocal[*person]() 105 value := &person{Id: 1, Name: "Hello"} 106 tls.Set(value) 107 srcValue := tls.Get() 108 assert.Same(t, value, srcValue) 109 assert.Equal(t, *value, *srcValue) 110 111 mp := createInheritedMap() 112 assert.NotNil(t, mp) 113 getValue := entryValue[*person](mp.get(tls.(*inheritableThreadLocal[*person]).index)) 114 assert.Same(t, value, getValue) 115 assert.Equal(t, *value, *getValue) 116 117 mp2 := createInheritedMap() 118 assert.NotNil(t, mp2) 119 assert.NotSame(t, mp, mp2) 120 getValue2 := entryValue[*person](mp2.get(tls.(*inheritableThreadLocal[*person]).index)) 121 assert.Same(t, value, getValue2) 122 assert.Equal(t, *value, *getValue2) 123 } 124 125 func TestCreateInheritedMap_Cloneable(t *testing.T) { 126 tls := NewInheritableThreadLocal[*personCloneable]() 127 value := &personCloneable{Id: 1, Name: "Hello"} 128 tls.Set(value) 129 srcValue := tls.Get() 130 assert.Same(t, value, srcValue) 131 assert.Equal(t, *value, *srcValue) 132 133 mp := createInheritedMap() 134 assert.NotNil(t, mp) 135 getValue := entryValue[*personCloneable](mp.get(tls.(*inheritableThreadLocal[*personCloneable]).index)) 136 assert.NotSame(t, value, getValue) 137 assert.Equal(t, *value, *getValue) 138 139 mp2 := createInheritedMap() 140 assert.NotNil(t, mp2) 141 assert.NotSame(t, mp, mp2) 142 getValue2 := entryValue[*personCloneable](mp2.get(tls.(*inheritableThreadLocal[*personCloneable]).index)) 143 assert.NotSame(t, value, getValue2) 144 assert.Equal(t, *value, *getValue2) 145 } 146 147 func TestFill(t *testing.T) { 148 a := make([]entry, 6) 149 fill(a, 4, 5, unset) 150 for i := 0; i < 6; i++ { 151 if i == 4 { 152 assert.True(t, a[i] == unset) 153 } else { 154 assert.Nil(t, a[i]) 155 assert.True(t, a[i] != unset) 156 } 157 } 158 }