2022-03-31 13:21:18 +00:00
|
|
|
package shopfront_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-05 17:36:34 +00:00
|
|
|
"sync"
|
2022-03-31 13:21:18 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
2024-06-05 17:36:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-31 13:21:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-04-08 09:08:35 +00:00
|
|
|
"go.uber.org/goleak"
|
2022-04-17 15:28:33 +00:00
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
"gitlab.com/slon/shad-go/redisfixture"
|
|
|
|
"gitlab.com/slon/shad-go/shopfront"
|
2022-03-31 13:21:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShopfront(t *testing.T) {
|
2023-04-08 09:08:35 +00:00
|
|
|
goleak.VerifyNone(t)
|
|
|
|
|
2022-03-31 13:21:18 +00:00
|
|
|
rdb := redis.NewClient(&redis.Options{
|
2024-06-05 17:36:34 +00:00
|
|
|
Addr: redisfixture.StartRedis(t),
|
2022-03-31 13:21:18 +00:00
|
|
|
})
|
2022-04-13 20:11:00 +00:00
|
|
|
defer func() { _ = rdb.Close() }()
|
2022-03-31 13:21:18 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
c := shopfront.New(rdb)
|
|
|
|
|
|
|
|
items, err := c.GetItems(ctx, []shopfront.ItemID{1, 2, 3, 4}, 42)
|
|
|
|
require.NoError(t, err)
|
2022-04-12 19:25:19 +00:00
|
|
|
require.Equal(t, []shopfront.Item{
|
2022-03-31 13:21:18 +00:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
2022-04-12 19:25:19 +00:00
|
|
|
}, items)
|
2022-03-31 13:21:18 +00:00
|
|
|
|
|
|
|
require.NoError(t, c.RecordView(ctx, 3, 42))
|
|
|
|
require.NoError(t, c.RecordView(ctx, 2, 42))
|
|
|
|
|
|
|
|
require.NoError(t, c.RecordView(ctx, 2, 4242))
|
|
|
|
|
|
|
|
items, err = c.GetItems(ctx, []shopfront.ItemID{1, 2, 3, 4}, 42)
|
|
|
|
require.NoError(t, err)
|
2022-04-12 19:25:19 +00:00
|
|
|
require.Equal(t, []shopfront.Item{
|
2022-03-31 13:21:18 +00:00
|
|
|
{},
|
|
|
|
{ViewCount: 2, Viewed: true},
|
|
|
|
{ViewCount: 1, Viewed: true},
|
|
|
|
{},
|
2022-04-12 19:25:19 +00:00
|
|
|
}, items)
|
2022-03-31 13:21:18 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
func TestShopFrontConcurrent(t *testing.T) {
|
|
|
|
goleak.VerifyNone(t)
|
|
|
|
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
Addr: redisfixture.StartRedis(t),
|
|
|
|
})
|
|
|
|
defer func() { _ = rdb.Close() }()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
c := shopfront.New(rdb)
|
|
|
|
|
|
|
|
N := 10000
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NoError(t, c.RecordView(ctx, 1, 1))
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
items, err := c.GetItems(ctx, []shopfront.ItemID{1}, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []shopfront.Item{
|
|
|
|
{ViewCount: N, Viewed: true},
|
|
|
|
}, items)
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:21:18 +00:00
|
|
|
func BenchmarkShopfront(b *testing.B) {
|
|
|
|
const nItems = 1024
|
|
|
|
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
2024-06-05 17:36:34 +00:00
|
|
|
Addr: redisfixture.StartRedis(b),
|
2022-03-31 13:21:18 +00:00
|
|
|
})
|
2022-04-13 20:11:00 +00:00
|
|
|
defer func() { _ = rdb.Close() }()
|
2022-03-31 13:21:18 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
c := shopfront.New(rdb)
|
|
|
|
|
|
|
|
var ids []shopfront.ItemID
|
|
|
|
for i := 0; i < nItems; i++ {
|
|
|
|
ids = append(ids, shopfront.ItemID(i))
|
|
|
|
require.NoError(b, c.RecordView(ctx, shopfront.ItemID(i), 42))
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := c.GetItems(ctx, ids, 42)
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
|
|
|
}
|