Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
add sqlite context cancelation test
Browse files Browse the repository at this point in the history
  • Loading branch information
amerkurev committed Jun 4, 2022
1 parent 1e60414 commit 91d70d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
10 changes: 8 additions & 2 deletions gcache_test.go
Expand Up @@ -337,6 +337,9 @@ func TestRedisCache_Context(t *testing.T) {

c := New[int, int](store.RedisStore(rdb))

var wg sync.WaitGroup
wg.Add(1)

go func() {
for k := 0; k < 100_000; k++ {
err := c.SetWithContext(ctx, k, k*k)
Expand All @@ -345,11 +348,14 @@ func TestRedisCache_Context(t *testing.T) {
break
}
}
wg.Done()
}()

cancel()
r := <-ctx.Done()
assert.Equal(t, r, struct{}{})
wg.Wait()

assert.Equal(t, <-ctx.Done(), struct{}{})
assert.True(t, errors.Is(ctx.Err(), context.Canceled))
}

func TestCacheStats(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions store/sqlite_test.go
Expand Up @@ -5,9 +5,48 @@ import (
"database/sql"
"errors"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

func TestSQLiteStore_Context(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
db, err := sql.Open("sqlite3", "test.db")
assert.Nil(t, err)
s, err := SQLiteStore(ctx, db)
assert.Nil(t, err)
assert.NotNil(t, s)

t.Cleanup(func() {
assert.Nil(t, db.Close())
})

err = s.Clear(ctx)
assert.Nil(t, err)

var wg sync.WaitGroup
wg.Add(1)

go func() {
for k := 0; k < 100_000; k++ {
key := "a"
err := s.Set(ctx, key, nil)
if err != nil {
assert.True(t, errors.Is(err, context.Canceled))
break
}
}
wg.Done()
}()

cancel()
wg.Wait()

assert.Equal(t, <-ctx.Done(), struct{}{})
assert.True(t, errors.Is(ctx.Err(), context.Canceled))
}

func TestSQLiteStore(t *testing.T) {
ctx := context.Background()
db, err := sql.Open("sqlite3", "test.db")
Expand All @@ -16,6 +55,10 @@ func TestSQLiteStore(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, s)

t.Cleanup(func() {
assert.Nil(t, db.Close())
})

err = s.Clear(ctx)
assert.Nil(t, err)

Expand Down

0 comments on commit 91d70d4

Please sign in to comment.