1
// Copyright 2018 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
20
raftstoremock "github.com/cubefs/cubefs/metanode/mocktest/raftstore"
21
"github.com/golang/mock/gomock"
24
func TestInodeOnce(t *testing.T) {
31
ino2, _ := InodeOnceUnmarshal(val)
33
t.Fatal("inode once unmarshal failed")
37
func TestAllocateUniqId(t *testing.T) {
38
mp := metaPartition{config: &MetaPartitionConfig{UniqId: 0}}
39
s, e := mp.allocateUniqID(1)
41
t.Errorf("allocateUniqID failed: %v, %v", s, e)
44
s, e = mp.allocateUniqID(1000)
45
if s != 2 || e != 1001 {
46
t.Errorf("allocateUniqID failed: %v, %v", s, e)
50
func TestDoEvit(t *testing.T) {
51
mockCtrl := gomock.NewController(t)
52
defer mockCtrl.Finish()
53
mp := mockPartitionRaft(mockCtrl)
54
checker := mp.uniqChecker
56
if checker.inQue.len() != 0 || len(checker.op) != 0 {
57
t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
60
for i := 1; i <= 1; i++ {
61
if !checker.legalIn(uint64(i)) {
66
if checker.inQue.len() != 1 || len(checker.op) != 1 {
67
t.Errorf("failed, inQue %v, op [%v]", checker.inQue, checker.op)
69
mp.uniqChecker.keepTime = 0
71
if checker.inQue.len() != 0 || len(checker.op) != 0 {
72
t.Errorf("failed, inQue %v, op [%v]", checker.inQue, checker.op)
75
for i := 1; i <= 1000; i++ {
76
if !checker.legalIn(uint64(i)) {
80
if checker.inQue.len() != 1000 || len(checker.op) != 1000 {
81
t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
85
mp.uniqChecker.keepOps = 100
86
for i := 1001; i <= 1100; i++ {
87
if !checker.legalIn(uint64(i)) {
92
if checker.inQue.len() != 100 || len(checker.op) != 100 {
93
t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
97
func TestDoEvit1(t *testing.T) {
98
mockCtrl := gomock.NewController(t)
99
defer mockCtrl.Finish()
100
mp := mockPartitionRaft(mockCtrl)
101
checker := mp.uniqChecker
102
mp.uniqChecker.keepTime = 0
104
for i := 1; i <= 10240; i++ {
105
if !checker.legalIn(uint64(i)) {
109
mp.uniqCheckerEvict()
111
if checker.inQue.len() != 0 || len(checker.op) != 0 {
112
t.Errorf("failed, inQue %v, op [%v]", checker.inQue, checker.op)
116
func TestDoEvit2(t *testing.T) {
117
mockCtrl := gomock.NewController(t)
118
defer mockCtrl.Finish()
119
mp := mockPartitionRaft(mockCtrl)
120
checker := mp.uniqChecker
121
mp.uniqChecker.keepTime = 0
123
for i := 1; i <= 9000; i++ {
124
if !checker.legalIn(uint64(i)) {
128
for i := 9001; i <= 13000; i++ {
129
if !checker.legalIn(uint64(i)) {
133
mp.uniqCheckerEvict()
134
if checker.inQue.len() != len(checker.op) {
135
t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
139
func mockPartitionRaft(ctrl *gomock.Controller) *metaPartition {
140
conf := &MetaPartitionConfig{
143
partition := NewMetaPartition(conf, nil).(*metaPartition)
144
partition.uniqChecker.keepTime = 1
145
partition.uniqChecker.keepOps = 0
146
raft := raftstoremock.NewMockPartition(ctrl)
148
raft.EXPECT().Submit(gomock.Any()).DoAndReturn(func(cmd []byte) (resp interface{}, err error) {
150
return partition.Apply(cmd, idx)
153
raft.EXPECT().LeaderTerm().DoAndReturn(func() (uint64, uint64) {
157
partition.raftPartition = raft