-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemory_test.go
145 lines (115 loc) · 3.26 KB
/
memory_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package session
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/flamego/flamego"
)
func TestMemoryStore(t *testing.T) {
f := flamego.NewWithLogger(&bytes.Buffer{})
f.Use(Sessioner())
f.Get("/set", func(s Session) {
s.Set("username", "flamego")
})
f.Get("/get", func(s Session) {
sid := s.ID()
assert.Len(t, sid, 16)
username, ok := s.Get("username").(string)
assert.True(t, ok)
assert.Equal(t, "flamego", username)
s.Delete("username")
_, ok = s.Get("username").(string)
assert.False(t, ok)
s.Set("random", "value")
s.Flush()
_, ok = s.Get("random").(string)
assert.False(t, ok)
})
f.Get("/destroy", func(c flamego.Context, session Session, store Store) error {
return store.Destroy(c.Request().Context(), session.ID())
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/set", nil)
require.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
cookie := resp.Header().Get("Set-Cookie")
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/get", nil)
require.Nil(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/destroy", nil)
require.Nil(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
}
func TestMemoryStore_GC(t *testing.T) {
ctx := context.Background()
now := time.Now()
store := newMemoryStore(
MemoryConfig{
nowFunc: func() time.Time { return now },
Lifetime: time.Second,
},
nil,
)
sess1, err := store.Read(ctx, "1")
require.Nil(t, err)
now = now.Add(-2 * time.Second)
sess2, err := store.Read(ctx, "2")
require.Nil(t, err)
sess2.Set("name", "flamego")
err = store.Save(ctx, sess2)
require.Nil(t, err)
// Read on an expired session should wipe data but preserve the record
now = now.Add(2 * time.Second)
tmp, err := store.Read(ctx, "2")
require.Nil(t, err)
assert.Nil(t, tmp.Get("name"))
now = now.Add(-2 * time.Second)
_, err = store.Read(ctx, "3")
require.Nil(t, err)
now = now.Add(2 * time.Second)
err = store.GC(ctx) // sess3 should be recycled
require.Nil(t, err)
wantHeap := []*memorySession{sess2.(*memorySession), sess1.(*memorySession)}
assert.Equal(t, wantHeap, store.heap)
wantIndex := map[string]*memorySession{
"1": sess1.(*memorySession),
"2": sess2.(*memorySession),
}
assert.Equal(t, wantIndex, store.index)
}
func TestMemoryStore_Touch(t *testing.T) {
ctx := context.Background()
now := time.Now()
store := newMemoryStore(
MemoryConfig{
nowFunc: func() time.Time { return now },
Lifetime: time.Second,
},
nil,
)
sess, err := store.Read(ctx, "1")
require.Nil(t, err)
now = now.Add(2 * time.Second)
// Touch should keep the session alive
err = store.Touch(ctx, sess.ID())
require.Nil(t, err)
err = store.GC(ctx)
require.Nil(t, err)
wantHeap := []*memorySession{sess.(*memorySession)}
assert.Equal(t, wantHeap, store.heap)
}