-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_test.go
56 lines (45 loc) · 1.27 KB
/
cleanup_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
package crdbstore
import (
"net/http"
"os"
"testing"
"time"
)
func TestCleanup(t *testing.T) {
dsn := os.Getenv("CRDB_TEST_CONN")
if dsn == "" {
t.Skip("This test requires a real database.")
}
ss, err := NewCrDBStore(dsn, []byte(secret))
if err != nil {
t.Fatal("Failed to get store", err)
}
defer ss.Close()
// Start the cleanup goroutine.
defer ss.StopCleanup(ss.Cleanup(time.Millisecond * 500))
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("Failed to create request", err)
}
session, err := ss.Get(req, "newsess")
if err != nil {
t.Fatal("Failed to create session", err)
}
// Expire the session.
session.Options.MaxAge = 1
m := make(http.Header)
if err = ss.Save(req, headerOnlyResponseWriter(m), session); err != nil {
t.Fatal("failed to save session:", err.Error())
}
// Give the ticker a moment to run.
time.Sleep(time.Millisecond * 1500)
// SELECT expired sessions. We should get a count of zero back.
var count int
err = ss.DbPool.QueryRow("SELECT count(*) FROM http_sessions WHERE expires_on < now()").Scan(&count)
if err != nil {
t.Fatalf("failed to select expired sessions from DB: %v", err)
}
if count > 0 {
t.Fatalf("ticker did not delete expired sessions: want 0 got %v", count)
}
}