sqlite3: add support for context management

This commit is contained in:
Muhammed Efe Cetin
2025-02-10 23:31:20 +03:00
parent ce31f766b9
commit 75f5216baf
2 changed files with 108 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package sqlite3
import (
"context"
"database/sql"
"testing"
"time"
@@ -23,6 +24,19 @@ func Test_SQLite3_Set(t *testing.T) {
require.NoError(t, err)
}
func Test_SQLite3_SetWithContext(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := testStore.SetWithContext(ctx, key, val, 0)
require.ErrorIs(t, err, context.Canceled)
}
func Test_SQLite3_Set_Override(t *testing.T) {
var (
key = "john"
@@ -50,6 +64,23 @@ func Test_SQLite3_Get(t *testing.T) {
require.Equal(t, val, result)
}
func Test_SQLite3_GetWithContext(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
cancel()
result, err := testStore.GetWithContext(ctx, key)
require.ErrorIs(t, err, context.Canceled)
require.Zero(t, len(result))
}
func Test_SQLite3_Set_Expiration(t *testing.T) {
var (
key = "john"
@@ -94,6 +125,26 @@ func Test_SQLite3_Delete(t *testing.T) {
require.Zero(t, len(result))
}
func Test_SQLite3_DeleteWithContext(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = testStore.DeleteWithContext(ctx, key)
require.ErrorIs(t, err, context.Canceled)
result, err := testStore.Get(key)
require.NoError(t, err)
require.Equal(t, val, result)
}
func Test_SQLite3_Reset(t *testing.T) {
val := []byte("doe")
@@ -115,6 +166,30 @@ func Test_SQLite3_Reset(t *testing.T) {
require.Zero(t, len(result))
}
func Test_SQLite3_ResetWithContext(t *testing.T) {
val := []byte("doe")
err := testStore.Set("john1", val, 0)
require.NoError(t, err)
err = testStore.Set("john2", val, 0)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = testStore.ResetWithContext(ctx)
require.ErrorIs(t, err, context.Canceled)
result, err := testStore.Get("john1")
require.NoError(t, err)
require.Equal(t, val, result)
result, err = testStore.Get("john2")
require.NoError(t, err)
require.Equal(t, val, result)
}
func Test_SQLite3_GC(t *testing.T) {
testVal := []byte("doe")