Skip to content

Commit

Permalink
test(cache): adding test for list many
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Sep 30, 2023
1 parent d37a2d6 commit aaed64a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 13 deletions.
95 changes: 82 additions & 13 deletions pkg/cache/list_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func (s *ListSuite) TestList() {
assert.Equal(s.T(), []Repository{first, second}, got)
})

s.Run("no memory no redis, load many", func() {
instance := cache.New(nil, func(id int) string { return strconv.Itoa(id) }, fetchRepository, nil).
WithMissMany(fetchRepositories)

first := getRepository(s.T())
first.ID = 10

second := getRepository(s.T())
second.ID = 20

got, err := instance.List(context.Background(), 10, 20)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first, second}, got)
})

s.Run("memory no redis", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -102,7 +118,7 @@ func (s *ListSuite) TestList() {
assert.Equal(s.T(), []Repository{first, second, third}, got)

// Wait for async save
time.Sleep(time.Millisecond * 50)
time.Sleep(time.Second)

got, err = instance.List(context.Background(), 10, 20, 30)

Expand All @@ -128,7 +144,7 @@ func (s *ListSuite) TestList() {
assert.Equal(s.T(), []Repository{first, second, third}, got)

// Wait for async save
time.Sleep(time.Millisecond * 50)
time.Sleep(time.Second)

got, err = instance.List(context.Background(), 10, 20, 30)

Expand All @@ -144,43 +160,96 @@ func (s *ListSuite) TestList() {
WithClientSideCaching(ctx, "memory_redis")

first := getRepository(s.T())
first.ID = 10
first.ID = 1000

second := getRepository(s.T())
second.ID = 20
second.ID = 2000

third := getRepository(s.T())
third.ID = 30
third.ID = 3000

four := getRepository(s.T())
four.ID = 40
four.ID = 4000

got, err := instance.List(context.Background(), 10)
got, err := instance.List(context.Background(), 1000)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first}, got)

// Wait for async save
time.Sleep(time.Millisecond * 50)
time.Sleep(time.Second)

err = s.integration.Client().Store(ctx, "20", "invalid_payload", 0)
err = s.integration.Client().Store(ctx, "2000", "invalid_payload", 0)
assert.NoError(s.T(), err)

got, err = instance.List(context.Background(), 10, 20, 30)
got, err = instance.List(context.Background(), 1000, 2000, 3000)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first, second, third}, got)

// Wait for async save
time.Sleep(time.Second)

payload, err := cache.JSONSerializer[Repository]{}.Encode(four)
assert.NoError(s.T(), err)

err = s.integration.Client().Store(ctx, "4000", payload, 0)
assert.NoError(s.T(), err)

got, err = instance.List(context.Background(), 1000, 4000, 2000, 3000)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first, four, second, third}, got)
})

s.Run("memory, redis, many and extend", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

instance := cache.New(s.integration.Client(), func(id int) string { return strconv.Itoa(id) }, fetchOnce(), nil).
WithClientSideCaching(ctx, "memory_redis_may_extend").
WithExtendOnHit().
WithTTL(time.Hour).
WithMissMany(fetchRepositoriesOnce())

first := getRepository(s.T())
first.ID = 100

second := getRepository(s.T())
second.ID = 200

third := getRepository(s.T())
third.ID = 300

four := getRepository(s.T())
four.ID = 400

got, err := instance.List(context.Background(), 100)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first}, got)

// Wait for async save
time.Sleep(time.Second)

err = s.integration.Client().Store(ctx, "200", "invalid_payload", 0)
assert.NoError(s.T(), err)

got, err = instance.List(context.Background(), 100, 200, 300)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first, second, third}, got)

// Wait for async save
time.Sleep(time.Millisecond * 50)
time.Sleep(time.Second)

payload, err := cache.JSONSerializer[Repository]{}.Encode(four)
assert.NoError(s.T(), err)

err = s.integration.Client().Store(ctx, "40", payload, 0)
err = s.integration.Client().Store(ctx, "400", payload, 0)
assert.NoError(s.T(), err)

got, err = instance.List(context.Background(), 10, 40, 20, 30)
got, err = instance.List(context.Background(), 100, 400, 200, 300)

assert.Nil(s.T(), err)
assert.Equal(s.T(), []Repository{first, four, second, third}, got)
Expand Down
34 changes: 34 additions & 0 deletions pkg/cache/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ func fetchRepository(_ context.Context, id int) (Repository, error) {
return output, err
}

func fetchRepositoriesOnce() func(ctx context.Context, ids []int) ([]Repository, error) {
fetcher := fetchOnce()

return func(ctx context.Context, ids []int) ([]Repository, error) {
output := make([]Repository, len(ids))

for index, id := range ids {
item, err := fetcher(ctx, id)
if err != nil {
return output, err
}

output[index] = item
}

return output, nil
}
}

func fetchRepositories(ctx context.Context, ids []int) ([]Repository, error) {
output := make([]Repository, len(ids))

for index, id := range ids {
item, err := fetchRepository(ctx, id)
if err != nil {
return output, err
}

output[index] = item
}

return output, nil
}

func noFetch(_ context.Context, _ int) (Repository, error) {
return Repository{}, errors.New("not implemented")
}
Expand Down

0 comments on commit aaed64a

Please sign in to comment.