2020-04-30 15:07:27 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pingcap-incubator/tinykv/proto/pkg/metapb"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRaftstoreErrToPbError(t *testing.T) {
|
|
|
|
regionId := uint64(1)
|
|
|
|
notLeader := &ErrNotLeader{RegionId: regionId, Leader: nil}
|
|
|
|
pbErr := RaftstoreErrToPbError(notLeader)
|
|
|
|
require.NotNil(t, pbErr.NotLeader)
|
2020-04-01 15:27:12 +08:00
|
|
|
assert.Equal(t, regionId, pbErr.NotLeader.RegionId)
|
2020-04-30 15:07:27 +08:00
|
|
|
|
|
|
|
regionNotFound := &ErrRegionNotFound{RegionId: regionId}
|
|
|
|
pbErr = RaftstoreErrToPbError(regionNotFound)
|
|
|
|
require.NotNil(t, pbErr.RegionNotFound)
|
2020-04-01 15:27:12 +08:00
|
|
|
assert.Equal(t, regionId, pbErr.RegionNotFound.RegionId)
|
2020-04-30 15:07:27 +08:00
|
|
|
|
|
|
|
region := &metapb.Region{Id: regionId, StartKey: []byte{0}, EndKey: []byte{1}}
|
|
|
|
|
|
|
|
keyNotInRegion := &ErrKeyNotInRegion{Key: []byte{2}, Region: region}
|
|
|
|
pbErr = RaftstoreErrToPbError(keyNotInRegion)
|
|
|
|
require.NotNil(t, pbErr.KeyNotInRegion)
|
2020-04-01 15:27:12 +08:00
|
|
|
assert.Equal(t, []byte{0}, pbErr.KeyNotInRegion.StartKey)
|
|
|
|
assert.Equal(t, []byte{1}, pbErr.KeyNotInRegion.EndKey)
|
|
|
|
assert.Equal(t, []byte{2}, pbErr.KeyNotInRegion.Key)
|
2020-04-30 15:07:27 +08:00
|
|
|
|
|
|
|
epochNotMatch := &ErrEpochNotMatch{Regions: []*metapb.Region{region}}
|
|
|
|
pbErr = RaftstoreErrToPbError(epochNotMatch)
|
|
|
|
require.NotNil(t, pbErr.EpochNotMatch)
|
2020-04-01 15:27:12 +08:00
|
|
|
assert.Equal(t, []*metapb.Region{region}, pbErr.EpochNotMatch.CurrentRegions)
|
2020-04-30 15:07:27 +08:00
|
|
|
|
|
|
|
staleCommand := &ErrStaleCommand{}
|
|
|
|
pbErr = RaftstoreErrToPbError(staleCommand)
|
|
|
|
require.NotNil(t, pbErr.StaleCommand)
|
|
|
|
|
|
|
|
requestStoreId, actualStoreId := uint64(1), uint64(2)
|
|
|
|
storeNotMatch := &ErrStoreNotMatch{RequestStoreId: requestStoreId, ActualStoreId: actualStoreId}
|
|
|
|
pbErr = RaftstoreErrToPbError(storeNotMatch)
|
|
|
|
require.NotNil(t, pbErr.StoreNotMatch)
|
2020-04-01 15:27:12 +08:00
|
|
|
assert.Equal(t, requestStoreId, pbErr.StoreNotMatch.RequestStoreId)
|
|
|
|
assert.Equal(t, actualStoreId, pbErr.StoreNotMatch.ActualStoreId)
|
2020-04-30 15:07:27 +08:00
|
|
|
}
|