-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession_test.go
More file actions
59 lines (49 loc) · 1.27 KB
/
session_test.go
File metadata and controls
59 lines (49 loc) · 1.27 KB
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
package session
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
type Session struct {
name string
}
func FakeUserLogin(ss *SessionStore[Session]) {
req, _ := http.NewRequest("GET", "/whatever", nil)
rr := httptest.NewRecorder()
ss.PutSession(rr, req, &Session{name: "testing"})
}
func TestSessionStore(t *testing.T) {
var ss SessionStore[Session]
ss.InitStore("test", time.Duration(time.Minute*5))
var wg sync.WaitGroup
numGoroutines := 20
wg.Add(numGoroutines)
for i := 0; i <= numGoroutines; i++ {
go func() {
defer wg.Done()
for created := 0; created <= 500000; created++ {
FakeUserLogin(&ss)
}
}()
}
wg.Wait()
fmt.Println("Done, simulated 10 million user sessions")
}
func BenchmarkSessionCreation(b *testing.B) {
var ss SessionStore[Session]
ss.InitStore("test", time.Duration(time.Minute))
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/whatever", nil)
if err != nil {
b.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
ss.PutSession(rr, req, &Session{name: "testing"})
}
}