mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 08:37:10 +08:00
🎁 fix tests
This commit is contained in:
@@ -39,14 +39,13 @@ func Test_Memcache_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_Memcache_Get_Expired(t *testing.T) {
|
||||
|
@@ -39,14 +39,13 @@ func Test_Memory_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_Memory_Get_Expired(t *testing.T) {
|
||||
@@ -61,7 +60,6 @@ func Test_Memory_Get_Expired(t *testing.T) {
|
||||
|
||||
func Test_Memory_Get_NotExist(t *testing.T) {
|
||||
|
||||
|
||||
result, err := testStore.Get("notexist")
|
||||
utils.AssertEqual(t, ErrNotExist, err)
|
||||
utils.AssertEqual(t, true, len(result) == 0)
|
||||
|
@@ -41,14 +41,13 @@ func Test_MongoDB_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_MongoDB_Get_Expired(t *testing.T) {
|
||||
|
@@ -76,7 +76,7 @@ func New(config ...Config) *Storage {
|
||||
gcInterval: cfg.GCInterval,
|
||||
db: db,
|
||||
sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE id=?;`, cfg.Table),
|
||||
sqlInsert: fmt.Sprintf("INSERT INTO %s (id, data, exp) VALUES (?,?,?)", cfg.Table),
|
||||
sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (id, data, exp) VALUES (?,?,?)", cfg.Table),
|
||||
sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE id=?", cfg.Table),
|
||||
sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table),
|
||||
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= ?", cfg.Table),
|
||||
|
@@ -45,14 +45,13 @@ func Test_MYSQL_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_MYSQL_Get_Expired(t *testing.T) {
|
||||
|
@@ -99,7 +99,7 @@ func New(config ...Config) *Storage {
|
||||
db: db,
|
||||
gcInterval: cfg.GCInterval,
|
||||
sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE key=$1;`, cfg.Table),
|
||||
sqlInsert: fmt.Sprintf("INSERT INTO %s (key, data, exp) VALUES ($1, $2, $3)", cfg.Table),
|
||||
sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (key, data, exp) VALUES ($1, $2, $3)", cfg.Table),
|
||||
sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE key=$1", cfg.Table),
|
||||
sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table),
|
||||
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= $1", cfg.Table),
|
||||
|
@@ -45,14 +45,13 @@ func Test_Postgres_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.(key, val, exp)
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_Postgres_Get_Expired(t *testing.T) {
|
||||
|
@@ -41,14 +41,13 @@ func Test_Redis_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_Redis_Get_Expired(t *testing.T) {
|
||||
|
BIN
sqlite3/fiber.sqlite3
Normal file
BIN
sqlite3/fiber.sqlite3
Normal file
Binary file not shown.
@@ -81,7 +81,7 @@ func New(config ...Config) *Storage {
|
||||
db: db,
|
||||
gcInterval: cfg.GCInterval,
|
||||
sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE key=?;`, cfg.Table),
|
||||
sqlInsert: fmt.Sprintf("INSERT INTO %s (key, data, exp) VALUES (?,?,?)", cfg.Table),
|
||||
sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (key, data, exp) VALUES (?,?,?)", cfg.Table),
|
||||
sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE key=?", cfg.Table),
|
||||
sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table),
|
||||
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= ?", cfg.Table),
|
||||
@@ -96,7 +96,6 @@ func New(config ...Config) *Storage {
|
||||
// Get value by key
|
||||
func (s *Storage) Get(key string) ([]byte, error) {
|
||||
row := s.db.QueryRow(s.sqlSelect, key)
|
||||
|
||||
// Add db response to data
|
||||
var (
|
||||
data = []byte{}
|
||||
@@ -108,10 +107,9 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If the expiration time has already passed, then return nil
|
||||
if exp != 0 && exp <= time.Now().Unix() {
|
||||
return nil, nil
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
|
||||
return data, nil
|
||||
|
@@ -42,14 +42,13 @@ func Test_SQLite3_Set_Expiration(t *testing.T) {
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 500 * time.Millisecond
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_SQLite3_Get_Expired(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user