Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/copr: migrate test-infra to testify #27716

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 58 additions & 52 deletions store/copr/coprocessor_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@
package copr

import (
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/tidb/kv"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/config"
)

type testCoprocessorCacheSuite struct {
}

var _ = Suite(&testCoprocessorCacheSuite{})

func (s *testCoprocessorSuite) TestBuildCacheKey(c *C) {
func TestBuildCacheKey(t *testing.T) {
t.Parallel()
req := coprocessor.Request{
Tp: 0xAB,
StartTs: 0xAABBCC,
Expand All @@ -46,7 +43,7 @@ func (s *testCoprocessorSuite) TestBuildCacheKey(c *C) {
}

key, err := coprCacheBuildKey(&req)
c.Assert(err, IsNil)
require.NoError(t, err)
expectKey := ""
expectKey += "\xab" // 1 byte Tp
expectKey += "\x08\x00\x00\x00" // 4 bytes Data len
Expand All @@ -59,7 +56,7 @@ func (s *testCoprocessorSuite) TestBuildCacheKey(c *C) {
expectKey += "\x01\x01\x02" // StartKey
expectKey += "\x03\x00" // 2 bytes EndKey len
expectKey += "\x01\x01\x03" // EndKey
c.Assert(key, DeepEquals, []byte(expectKey))
require.EqualValues(t, []byte(expectKey), key)

req = coprocessor.Request{
Tp: 0xABCC, // Tp too big
Expand All @@ -69,95 +66,101 @@ func (s *testCoprocessorSuite) TestBuildCacheKey(c *C) {
}

_, err = coprCacheBuildKey(&req)
c.Assert(err, NotNil)
require.Error(t, err)
}

func (s *testCoprocessorSuite) TestDisable(c *C) {
func TestDisable(t *testing.T) {
t.Parallel()
cache, err := newCoprCache(&config.CoprocessorCache{CapacityMB: 0})
c.Assert(err, IsNil)
c.Assert(cache, IsNil)
require.NoError(t, err)
require.Nil(t, cache)

v := cache.Set([]byte("foo"), &coprCacheValue{})
c.Assert(v, Equals, false)
require.False(t, v)

v2 := cache.Get([]byte("foo"))
c.Assert(v2, IsNil)
require.Nil(t, v2)

v = cache.CheckResponseAdmission(1024, time.Second*5)
c.Assert(v, Equals, false)
require.False(t, v)

cache, err = newCoprCache(&config.CoprocessorCache{CapacityMB: 0.001})
c.Assert(err, NotNil)
c.Assert(cache, IsNil)
require.Error(t, err)
require.Nil(t, cache)

cache, err = newCoprCache(&config.CoprocessorCache{CapacityMB: 0.001, AdmissionMaxResultMB: 1})
c.Assert(err, IsNil)
c.Assert(cache, NotNil)
require.NoError(t, err)
require.NotNil(t, cache)
defer cache.cache.Close()
}

func (s *testCoprocessorSuite) TestAdmission(c *C) {
func TestAdmission(t *testing.T) {
t.Parallel()
cache, err := newCoprCache(&config.CoprocessorCache{AdmissionMinProcessMs: 5, AdmissionMaxResultMB: 1, CapacityMB: 1})
c.Assert(err, IsNil)
c.Assert(cache, NotNil)
require.NoError(t, err)
require.NotNil(t, cache)
defer cache.cache.Close()

v := cache.CheckRequestAdmission(0)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckRequestAdmission(1000)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckResponseAdmission(0, 0)
c.Assert(v, Equals, false)
require.False(t, v)

v = cache.CheckResponseAdmission(0, 4*time.Millisecond)
c.Assert(v, Equals, false)
require.False(t, v)

v = cache.CheckResponseAdmission(0, 5*time.Millisecond)
c.Assert(v, Equals, false)
require.False(t, v)

v = cache.CheckResponseAdmission(1, 0)
c.Assert(v, Equals, false)
require.False(t, v)

v = cache.CheckResponseAdmission(1, 4*time.Millisecond)
c.Assert(v, Equals, false)
require.False(t, v)

v = cache.CheckResponseAdmission(1, 5*time.Millisecond)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckResponseAdmission(1024, 5*time.Millisecond)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckResponseAdmission(1024*1024, 5*time.Millisecond)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckResponseAdmission(1024*1024+1, 5*time.Millisecond)
c.Assert(v, Equals, false)
require.False(t, v)

v = cache.CheckResponseAdmission(1024*1024+1, 4*time.Millisecond)
c.Assert(v, Equals, false)
require.False(t, v)

cache, err = newCoprCache(&config.CoprocessorCache{AdmissionMaxRanges: 5, AdmissionMinProcessMs: 5, AdmissionMaxResultMB: 1, CapacityMB: 1})
c.Assert(err, IsNil)
c.Assert(cache, NotNil)
require.NoError(t, err)
require.NotNil(t, cache)
defer cache.cache.Close()

v = cache.CheckRequestAdmission(0)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckRequestAdmission(5)
c.Assert(v, Equals, true)
require.True(t, v)

v = cache.CheckRequestAdmission(6)
c.Assert(v, Equals, false)
require.False(t, v)
}

func (s *testCoprocessorSuite) TestCacheValueLen(c *C) {
func TestCacheValueLen(t *testing.T) {
t.Parallel()
v := coprCacheValue{
TimeStamp: 0x123,
RegionID: 0x1,
RegionDataVersion: 0x3,
}
// 72 = (8 byte pointer + 8 byte for length + 8 byte for cap) * 2 + 8 byte * 3
c.Assert(v.Len(), Equals, 72)
require.Equal(t, 72, v.Len())

v = coprCacheValue{
Key: []byte("foobar"),
Expand All @@ -166,35 +169,38 @@ func (s *testCoprocessorSuite) TestCacheValueLen(c *C) {
RegionID: 0x1,
RegionDataVersion: 0x3,
}
c.Assert(v.Len(), Equals, 72+6+8)
require.Equal(t, 72+len(v.Key)+len(v.Data), v.Len())
}

func (s *testCoprocessorSuite) TestGetSet(c *C) {
func TestGetSet(t *testing.T) {
t.Parallel()
cache, err := newCoprCache(&config.CoprocessorCache{AdmissionMinProcessMs: 5, AdmissionMaxResultMB: 1, CapacityMB: 1})
c.Assert(err, IsNil)
c.Assert(cache, NotNil)
require.NoError(t, err)
require.NotNil(t, cache)
defer cache.cache.Close()

v := cache.Get([]byte("foo"))
c.Assert(v, IsNil)
require.Nil(t, v)

v2 := cache.Set([]byte("foo"), &coprCacheValue{
Data: []byte("bar"),
TimeStamp: 0x123,
RegionID: 0x1,
RegionDataVersion: 0x3,
})
c.Assert(v2, Equals, true)
require.True(t, v2)

// See https://github.com/dgraph-io/ristretto/blob/83508260cb49a2c3261c2774c991870fd18b5a1b/cache_test.go#L13
// Changed from 10ms to 50ms to resist from unstable CI environment.
time.Sleep(time.Millisecond * 50)

v = cache.Get([]byte("foo"))
c.Assert(v, NotNil)
c.Assert(v.Data, DeepEquals, []byte("bar"))
require.NotNil(t, v)
require.EqualValues(t, []byte("bar"), v.Data)
}

func (s *testCoprocessorSuite) TestIssue24118(c *C) {
func TestIssue24118(t *testing.T) {
t.Parallel()
_, err := newCoprCache(&config.CoprocessorCache{AdmissionMinProcessMs: 5, AdmissionMaxResultMB: 1, CapacityMB: -1})
c.Assert(err.Error(), Equals, "Capacity must be > 0 to enable the cache")
require.EqualError(t, err, "Capacity must be > 0 to enable the cache")
}
Loading