@@ -5,16 +5,15 @@ import (
55 "log"
66 "strconv"
77 "testing"
8- "time"
98)
109
10+ // Test package-level variables for backwards compatibility
1111var conf * StoreConfig
1212var runtime_store * RuntimeStore
1313var session_state * SessionState
1414var session_states []* SessionState
1515
1616func init () {
17- //log.Println("初始化")
1817 value := make (map [interface {}]interface {})
1918 value ["foo" ] = "bar"
2019 value ["kak" ] = "lal"
@@ -26,40 +25,67 @@ func init() {
2625 session_state = NewSessionState (nil , "session_read" , value )
2726 for i := 0 ; i < 1000000 ; i ++ {
2827 session_states = append (session_states , NewSessionState (nil , "session_read" + strconv .Itoa (i ), value ))
29- //runtime_store.SessionUpdate(NewSessionState(nil,"session_read"+strconv.FormatInt(time.Now().UnixNano(),10),value))
3028 }
3129
3230 runtime_store .SessionUpdate (session_state )
3331 runtime_store .SessionUpdate (NewSessionState (nil , "session_read_1" , value ))
3432}
3533
3634func TestRuntimeStore_SessionUpdate (t * testing.T ) {
37- //log.Println("开始 写测试")
35+ // Use a separate store for this test to avoid race conditions
36+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
37+ testValue := make (map [interface {}]interface {})
38+ testValue ["foo" ] = "bar"
39+ testValue ["kak" ] = "lal"
40+
41+ testState := NewSessionState (testStore , "session_read" , testValue )
42+ testStore .SessionUpdate (testState )
43+
3844 fmt .Println ("-------------before update session state------------" )
39- state , _ := runtime_store .SessionRead ("session_read" )
45+ state , _ := testStore .SessionRead ("session_read" )
4046 fmt .Printf ("session state session_read: %+v \n " , state )
41- session_state .values ["foo" ] = "newbar"
42- runtime_store .SessionUpdate (session_state )
43- state , _ = runtime_store .SessionRead ("session_read" )
47+
48+ testState .values ["foo" ] = "newbar"
49+ testStore .SessionUpdate (testState )
50+
51+ state , _ = testStore .SessionRead ("session_read" )
4452 fmt .Println ("-------------after update session state------------" )
4553 fmt .Printf ("session state session_read: %+v \n " , state )
46-
4754}
55+
4856func TestNewRuntimeStore_SessionUpdate_StateNotExist (t * testing.T ) {
57+ // Use a separate store for this test
58+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
59+ testValue := make (map [interface {}]interface {})
60+ testValue ["foo" ] = "bar"
61+
62+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
63+
4964 fmt .Println ("-------------before update session state------------" )
50- state , _ := runtime_store .SessionRead ("session_read_2" )
65+ state , _ := testStore .SessionRead ("session_read_2" )
5166 fmt .Printf ("session state session_read: %+v \n " , state )
67+
5268 state .values ["make" ] = "new"
53- runtime_store .SessionUpdate (state )
54- state , _ = runtime_store .SessionRead ("session_read" )
69+ testStore .SessionUpdate (state )
70+
71+ state , _ = testStore .SessionRead ("session_read" )
5572 fmt .Println ("-------------after update session state------------" )
5673 fmt .Printf ("session state session_read: %+v \n " , state )
5774}
5875
5976func TestRuntimeStore_SessionRead (t * testing.T ) {
60- //log.Println("开始读测试")
61- fmt .Printf ("runtime_store: %+v \n " , * runtime_store )
62- read , _ := runtime_store .SessionRead ("session_read" )
77+ // Use a separate store for this test
78+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
79+ testValue := make (map [interface {}]interface {})
80+ testValue ["foo" ] = "bar"
81+ testValue ["kak" ] = "lal"
82+
83+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
84+ testStore .SessionUpdate (NewSessionState (testStore , "session_read_1" , testValue ))
85+ testStore .SessionUpdate (NewSessionState (testStore , "session_read_2" , testValue ))
86+
87+ fmt .Printf ("runtime_store: %+v \n " , * testStore )
88+ read , _ := testStore .SessionRead ("session_read" )
6389 if read == nil {
6490 fmt .Println ("cannot find sessionId" )
6591 return
@@ -69,30 +95,43 @@ func TestRuntimeStore_SessionRead(t *testing.T) {
6995}
7096
7197func TestRuntimeStore_SessionExist (t * testing.T ) {
72- //log.Println("测试 session 存在")
73- fmt .Println ("is session exist: " , runtime_store .SessionExist ("session_read" ))
98+ // Use a separate store for this test
99+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
100+ testValue := make (map [interface {}]interface {})
101+ testValue ["foo" ] = "bar"
74102
103+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
104+
105+ fmt .Println ("is session exist: " , testStore .SessionExist ("session_read" ))
75106}
76107
77108func TestRuntimeStore_SessionRemove (t * testing.T ) {
109+ // Use a separate store for this test
110+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
111+ testValue := make (map [interface {}]interface {})
112+ testValue ["foo" ] = "bar"
113+ testValue ["kak" ] = "lal"
114+
115+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
116+
78117 log .Println ("session 删除测试" )
79118 fmt .Println ("------------------------" )
80119 fmt .Println ("before remove : " )
81- read , err := runtime_store .SessionRead ("session_read" )
120+ read , err := testStore .SessionRead ("session_read" )
82121 if err != nil {
83122 panic (err )
84123 }
85124 fmt .Println ("read : " )
86125 fmt .Printf ("sessionid : %s , values : %v \n " , read .SessionID (), read .values )
87126
88- err = runtime_store .SessionRemove ("session_read" )
127+ err = testStore .SessionRemove ("session_read" )
89128 if err != nil {
90129 fmt .Println (err .Error ())
91130 }
92131
93132 fmt .Println ("------------------------" )
94133 fmt .Println ("after remove : " )
95- read , err = runtime_store .SessionRead ("session_read" )
134+ read , err = testStore .SessionRead ("session_read" )
96135 if err != nil {
97136 panic (err )
98137 }
@@ -101,44 +140,78 @@ func TestRuntimeStore_SessionRemove(t *testing.T) {
101140}
102141
103142func TestRuntimeStore_SessionGC (t * testing.T ) {
104-
143+ // GC test - no assertions needed
105144}
106145
107146func TestRuntimeStore_SessionCount (t * testing.T ) {
108- fmt .Println (runtime_store .SessionCount ())
147+ // Use a separate store for this test
148+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
149+ testValue := make (map [interface {}]interface {})
150+ testValue ["foo" ] = "bar"
151+
152+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
153+ testStore .SessionUpdate (NewSessionState (testStore , "session_read_1" , testValue ))
154+ testStore .SessionUpdate (NewSessionState (testStore , "session_read_2" , testValue ))
155+
156+ fmt .Println (testStore .SessionCount ())
109157}
110158
111159func TestRuntimeStore_SessionAccess (t * testing.T ) {
112- state , _ := runtime_store .SessionRead ("session_read" )
113- fmt .Println ("------------------" )
114- fmt .Println ("before session access" )
115- fmt .Println (state .timeAccessed .String ())
116- fmt .Println ("------------------" )
117- fmt .Println ("after session access" )
118- time .Sleep (10 * time .Second )
119- runtime_store .SessionAccess ("session_read" )
120- fmt .Println (state .timeAccessed .String ())
160+ // Use a separate store for this test to avoid race conditions
161+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
162+ testValue := make (map [interface {}]interface {})
163+ testValue ["foo" ] = "bar"
164+
165+ testStore .SessionUpdate (NewSessionState (testStore , "test_access_session" , testValue ))
166+
167+ // Get initial state
168+ state , _ := testStore .SessionRead ("test_access_session" )
169+ if state == nil {
170+ t .Fatal ("Failed to read session" )
171+ }
172+
173+ // SessionAccess should update timeAccessed
174+ // Note: We don't directly access timeAccessed to avoid race conditions
175+ // Instead we verify the operation completes without error
176+ err := testStore .SessionAccess ("test_access_session" )
177+ if err != nil {
178+ t .Errorf ("SessionAccess failed: %v" , err )
179+ }
121180
181+ // Verify session still exists after access
182+ if ! testStore .SessionExist ("test_access_session" ) {
183+ t .Error ("Session should still exist after access" )
184+ }
122185}
123186
124187/**
125188性能测试 | 基准测试
126189*/
127190
128191func BenchmarkRuntimeStore_SessionRead_1 (b * testing.B ) {
192+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
193+ testValue := make (map [interface {}]interface {})
194+ testValue ["foo" ] = "bar"
195+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
196+
129197 for i := 0 ; i < b .N ; i ++ {
130- runtime_store .SessionRead ("session_read" )
198+ testStore .SessionRead ("session_read" )
131199 }
132200 b .ReportAllocs ()
133201}
202+
134203func BenchmarkRuntimeStore_SessionRead_Parallel (b * testing.B ) {
204+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
205+ testValue := make (map [interface {}]interface {})
206+ testValue ["foo" ] = "bar"
207+ testStore .SessionUpdate (NewSessionState (testStore , "session_read" , testValue ))
208+
135209 b .RunParallel (func (pb * testing.PB ) {
136210 for pb .Next () {
137- runtime_store .SessionRead ("session_read" )
211+ testStore .SessionRead ("session_read" )
138212 }
139213 })
140214 b .ReportAllocs ()
141-
142215}
143216
144217func BenchmarkRuntimeStore_SessionCount_1 (b * testing.B ) {
@@ -154,26 +227,32 @@ func BenchmarkRuntimeStore_SessionCount_Parallel(b *testing.B) {
154227 }
155228 })
156229 b .ReportAllocs ()
157-
158230}
159231
160232func BenchmarkRuntimeStore_SessionRemove_1 (b * testing.B ) {
161-
233+ // Empty benchmark
162234}
163235
164236func BenchmarkRuntimeStore_SessionUpdate_1 (b * testing.B ) {
237+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
165238 for i := 0 ; i < b .N ; i ++ {
166- runtime_store .SessionUpdate (session_states [i ])
239+ testStore .SessionUpdate (session_states [i % 1000 ])
167240 }
168241 b .ReportAllocs ()
169242}
170243
171244func BenchmarkRuntimeStore_SessionUpdate_Parallel (b * testing.B ) {
245+ testStore := NewRuntimeStore (NewDefaultRuntimeConfig ())
246+ testValue := make (map [interface {}]interface {})
247+ testValue ["foo" ] = "bar"
248+ testState := NewSessionState (testStore , "session_read" , testValue )
249+ testStore .SessionUpdate (testState )
250+
172251 b .RunParallel (func (pb * testing.PB ) {
173252 for pb .Next () {
174- runtime_store .SessionUpdate (session_state )
253+ testStore .SessionUpdate (testState )
175254 }
176255 })
177256 b .ReportAllocs ()
178- fmt .Println (len (runtime_store .sessions ))
257+ fmt .Println (len (testStore .sessions ))
179258}
0 commit comments