2016-08-18 18:07:37 +02:00
|
|
|
package memstore
|
2016-08-08 19:06:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-19 18:17:55 +02:00
|
|
|
"reflect"
|
2016-08-08 19:06:16 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-08-12 21:11:53 +02:00
|
|
|
func TestFind(t *testing.T) {
|
2019-04-28 07:30:35 +02:00
|
|
|
m := NewWithCleanupInterval(0)
|
|
|
|
m.items["session_token"] = item{object: []byte("encoded_data"), expiration: time.Now().Add(time.Second).UnixNano()}
|
2016-08-08 19:06:16 +02:00
|
|
|
|
2016-08-19 18:17:55 +02:00
|
|
|
b, found, err := m.Find("session_token")
|
2016-08-08 19:06:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
|
|
|
if found != true {
|
|
|
|
t.Fatalf("got %v: expected %v", found, true)
|
|
|
|
}
|
|
|
|
if bytes.Equal(b, []byte("encoded_data")) == false {
|
|
|
|
t.Fatalf("got %v: expected %v", b, []byte("encoded_data"))
|
|
|
|
}
|
2016-08-19 18:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindMissing(t *testing.T) {
|
2019-04-28 07:30:35 +02:00
|
|
|
m := NewWithCleanupInterval(0)
|
2016-08-08 19:06:16 +02:00
|
|
|
|
2016-08-19 18:17:55 +02:00
|
|
|
_, found, err := m.Find("missing_session_token")
|
2016-08-08 19:06:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
|
|
|
if found != false {
|
|
|
|
t.Fatalf("got %v: expected %v", found, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindBadData(t *testing.T) {
|
2019-04-28 07:30:35 +02:00
|
|
|
m := NewWithCleanupInterval(0)
|
|
|
|
m.items["session_token"] = item{object: "not_a_byte_slice", expiration: time.Now().Add(time.Second).UnixNano()}
|
2016-08-08 19:06:16 +02:00
|
|
|
|
2016-08-19 18:17:55 +02:00
|
|
|
_, _, err := m.Find("session_token")
|
2016-08-19 17:54:02 +02:00
|
|
|
if err != errTypeAssertionFailed {
|
|
|
|
t.Fatalf("got %v: expected %v", err, errTypeAssertionFailed)
|
2016-08-08 19:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
func TestCommitNew(t *testing.T) {
|
|
|
|
m := NewWithCleanupInterval(0)
|
2016-08-08 19:06:16 +02:00
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
err := m.Commit("session_token", []byte("encoded_data"), time.Now().Add(time.Minute))
|
2016-08-09 19:08:36 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
2016-08-19 18:17:55 +02:00
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
v, found := m.items["session_token"]
|
2016-08-08 19:06:16 +02:00
|
|
|
if found != true {
|
|
|
|
t.Fatalf("got %v: expected %v", found, true)
|
|
|
|
}
|
2019-04-28 07:30:35 +02:00
|
|
|
b, ok := v.object.([]byte)
|
2016-08-19 18:17:55 +02:00
|
|
|
if ok == false {
|
|
|
|
t.Fatal("could not convert to []byte")
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(b, []byte("encoded_data")) == false {
|
|
|
|
t.Fatalf("got %v: expected %v", b, []byte("encoded_data"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
func TestCommitUpdated(t *testing.T) {
|
|
|
|
m := NewWithCleanupInterval(0)
|
2016-08-19 18:17:55 +02:00
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
err := m.Commit("session_token", []byte("encoded_data"), time.Now().Add(time.Minute))
|
2016-08-19 18:17:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
err = m.Commit("session_token", []byte("new_encoded_data"), time.Now().Add(time.Minute))
|
2016-08-19 18:17:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
v := m.items["session_token"].object
|
2016-08-19 18:17:55 +02:00
|
|
|
b, ok := v.([]byte)
|
|
|
|
if ok == false {
|
|
|
|
t.Fatal("could not convert to []byte")
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(b, []byte("new_encoded_data")) == false {
|
|
|
|
t.Fatalf("got %v: expected %v", b, []byte("new_encoded_data"))
|
2016-08-08 19:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:17:55 +02:00
|
|
|
func TestExpiry(t *testing.T) {
|
2019-04-28 07:30:35 +02:00
|
|
|
m := NewWithCleanupInterval(0)
|
2016-08-08 19:06:16 +02:00
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
err := m.Commit("session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond))
|
2016-08-08 19:06:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
2016-08-19 18:17:55 +02:00
|
|
|
|
|
|
|
_, found, _ := m.Find("session_token")
|
|
|
|
if found != true {
|
|
|
|
t.Fatalf("got %v: expected %v", found, true)
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
time.Sleep(101 * time.Millisecond)
|
2016-08-19 18:17:55 +02:00
|
|
|
_, found, _ = m.Find("session_token")
|
|
|
|
if found != false {
|
|
|
|
t.Fatalf("got %v: expected %v", found, false)
|
2016-08-08 19:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
2019-04-28 07:30:35 +02:00
|
|
|
m := NewWithCleanupInterval(0)
|
|
|
|
m.items["session_token"] = item{object: []byte("encoded_data"), expiration: time.Now().Add(time.Second).UnixNano()}
|
2016-08-08 19:06:16 +02:00
|
|
|
|
2016-08-19 18:17:55 +02:00
|
|
|
err := m.Delete("session_token")
|
2016-08-08 19:06:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got %v: expected %v", err, nil)
|
|
|
|
}
|
2016-08-19 18:17:55 +02:00
|
|
|
|
2019-04-28 07:30:35 +02:00
|
|
|
_, found := m.items["session_token"]
|
2016-08-19 18:17:55 +02:00
|
|
|
if found != false {
|
|
|
|
t.Fatalf("got %v: expected %v", found, false)
|
2016-08-08 19:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-21 13:35:08 +02:00
|
|
|
|
|
|
|
func TestCleanupInterval(t *testing.T) {
|
|
|
|
m := NewWithCleanupInterval(100 * time.Millisecond)
|
|
|
|
defer m.StopCleanup()
|
|
|
|
m.items["session_token"] = item{object: []byte("encoded_data"), expiration: time.Now().Add(500 * time.Millisecond).UnixNano()}
|
|
|
|
|
|
|
|
_, ok := m.items["session_token"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("got %v: expected %v", ok, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
_, ok = m.items["session_token"]
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("got %v: expected %v", ok, false)
|
|
|
|
}
|
|
|
|
}
|