Skip to content

Commit

Permalink
Add Retention Test
Browse files Browse the repository at this point in the history
  • Loading branch information
tung.tq committed Dec 18, 2023
1 parent 4f5fb3d commit 1a201d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Simple & Efficient Cache Invalidator
# Simple & Efficient Cache Invalidator

[![Coverage Status](https://coveralls.io/repos/github/QuangTung97/cacheinv/badge.svg?branch=master)](https://coveralls.io/github/QuangTung97/cacheinv?branch=master)
46 changes: 42 additions & 4 deletions cacheinv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/QuangTung97/eventx"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/redis/go-redis/v9"
Expand Down Expand Up @@ -61,7 +62,7 @@ func initClients() map[int64]*redis.Client {
return globalClients
}

func newJobTest(_ *testing.T) *jobTest {
func newJobTest(_ *testing.T, options ...cacheinv.Option) *jobTest {
db := initDB()

db.MustExec(`TRUNCATE invalidate_events`)
Expand All @@ -83,7 +84,7 @@ func newJobTest(_ *testing.T) *jobTest {
db: db,
repo: repo,
clients: redisClients,
inv: cacheinv.NewInvalidatorJob(repo, client),
inv: cacheinv.NewInvalidatorJob(repo, client, options...),
}
}

Expand Down Expand Up @@ -118,13 +119,12 @@ func TestInvalidatorJob(t *testing.T) {
j.run()

time.Sleep(500 * time.Millisecond)
j.waitCompleted()

lastSeq, err := j.repo.GetLastSequence(context.Background(), "redis:11")
assert.Equal(t, nil, err)
assert.Equal(t, true, lastSeq.Valid)
assert.Equal(t, int64(0), lastSeq.Int64)

j.waitCompleted()
})

t.Run("do delete on redis", func(t *testing.T) {
Expand Down Expand Up @@ -156,6 +156,7 @@ func TestInvalidatorJob(t *testing.T) {
j.inv.Notify()

time.Sleep(500 * time.Millisecond)
j.waitCompleted()

lastSeq, err := j.repo.GetLastSequence(context.Background(), "redis:11")
assert.Equal(t, nil, err)
Expand All @@ -173,7 +174,44 @@ func TestInvalidatorJob(t *testing.T) {
val, err = client2.Get(context.Background(), "key03").Result()
assert.Equal(t, redis.Nil, err)
assert.Equal(t, "", val)
})

t.Run("do retention", func(t *testing.T) {
j := newJobTest(t,
cacheinv.WithRunnerOptions(eventx.WithCoreStoredEventsSize(512)),
cacheinv.WithRetentionOptions(
eventx.WithMaxTotalEvents(64),
eventx.WithDeleteBatchSize(4),
),
cacheinv.WithRetryConsumerOptions(eventx.WithRetryConsumerFetchLimit(32)),
)

j.run()

for i := 0; i < 128; i++ {
j.insertEvents(
cacheinv.InvalidateEvent{
Data: "key01,key02",
},
)
}

j.inv.Notify()

time.Sleep(2000 * time.Millisecond)

j.waitCompleted()

lastSeq, err := j.repo.GetLastSequence(context.Background(), "redis:11")
assert.Equal(t, nil, err)
assert.Equal(t, int64(128), lastSeq.Int64)

minSeq, err := j.repo.GetMinSequence(context.Background())
assert.Equal(t, nil, err)
assert.Greater(t, minSeq.Int64, int64(128-64-5))

lastSeq, err = j.repo.GetLastSequence(context.Background(), "redis:12")
assert.Equal(t, nil, err)
assert.Equal(t, int64(128), lastSeq.Int64)
})
}

0 comments on commit 1a201d3

Please sign in to comment.