2022-03-31 13:21:18 +00:00
|
|
|
package shopfront_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
"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
|
|
|
|
2023-10-03 17:25:41 +00:00
|
|
|
"gitlab.com/manytask/itmo-go/public/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{
|
|
|
|
Addr: StartRedis(t),
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkShopfront(b *testing.B) {
|
|
|
|
const nItems = 1024
|
|
|
|
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
Addr: StartRedis(b),
|
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
}
|