cubefs

Форк
0
/
partition_op_uniq_test.go 
160 строк · 4.0 Кб
1
// Copyright 2018 The CubeFS Authors.
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
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.
14

15
package metanode
16

17
import (
18
	"testing"
19

20
	raftstoremock "github.com/cubefs/cubefs/metanode/mocktest/raftstore"
21
	"github.com/golang/mock/gomock"
22
)
23

24
func TestInodeOnce(t *testing.T) {
25
	ino := &InodeOnce{
26
		UniqID: 123,
27
		Inode:  456,
28
	}
29

30
	val := ino.Marshal()
31
	ino2, _ := InodeOnceUnmarshal(val)
32
	if *ino2 != *ino {
33
		t.Fatal("inode once unmarshal failed")
34
	}
35
}
36

37
func TestAllocateUniqId(t *testing.T) {
38
	mp := metaPartition{config: &MetaPartitionConfig{UniqId: 0}}
39
	s, e := mp.allocateUniqID(1)
40
	if s != 1 || e != 1 {
41
		t.Errorf("allocateUniqID failed: %v, %v", s, e)
42
	}
43

44
	s, e = mp.allocateUniqID(1000)
45
	if s != 2 || e != 1001 {
46
		t.Errorf("allocateUniqID failed: %v, %v", s, e)
47
	}
48
}
49

50
func TestDoEvit(t *testing.T) {
51
	mockCtrl := gomock.NewController(t)
52
	defer mockCtrl.Finish()
53
	mp := mockPartitionRaft(mockCtrl)
54
	checker := mp.uniqChecker
55
	mp.uniqCheckerEvict()
56
	if checker.inQue.len() != 0 || len(checker.op) != 0 {
57
		t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
58
	}
59

60
	for i := 1; i <= 1; i++ {
61
		if !checker.legalIn(uint64(i)) {
62
			t.Errorf("failed")
63
		}
64
	}
65
	mp.uniqCheckerEvict()
66
	if checker.inQue.len() != 1 || len(checker.op) != 1 {
67
		t.Errorf("failed, inQue %v, op [%v]", checker.inQue, checker.op)
68
	}
69
	mp.uniqChecker.keepTime = 0
70
	mp.uniqCheckerEvict()
71
	if checker.inQue.len() != 0 || len(checker.op) != 0 {
72
		t.Errorf("failed, inQue %v, op [%v]", checker.inQue, checker.op)
73
	}
74

75
	for i := 1; i <= 1000; i++ {
76
		if !checker.legalIn(uint64(i)) {
77
			t.Errorf("failed")
78
		}
79
	}
80
	if checker.inQue.len() != 1000 || len(checker.op) != 1000 {
81
		t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
82
	}
83

84
	mp.uniqCheckerEvict()
85
	mp.uniqChecker.keepOps = 100
86
	for i := 1001; i <= 1100; i++ {
87
		if !checker.legalIn(uint64(i)) {
88
			t.Errorf("failed")
89
		}
90
	}
91
	mp.uniqCheckerEvict()
92
	if checker.inQue.len() != 100 || len(checker.op) != 100 {
93
		t.Errorf("failed, inQue %v, op [%v]", checker.inQue.len(), len(checker.op))
94
	}
95
}
96

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
103

104
	for i := 1; i <= 10240; i++ {
105
		if !checker.legalIn(uint64(i)) {
106
			t.Errorf("failed")
107
		}
108
	}
109
	mp.uniqCheckerEvict()
110

111
	if checker.inQue.len() != 0 || len(checker.op) != 0 {
112
		t.Errorf("failed, inQue %v, op [%v]", checker.inQue, checker.op)
113
	}
114
}
115

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
122

123
	for i := 1; i <= 9000; i++ {
124
		if !checker.legalIn(uint64(i)) {
125
			t.Errorf("failed")
126
		}
127
	}
128
	for i := 9001; i <= 13000; i++ {
129
		if !checker.legalIn(uint64(i)) {
130
			t.Errorf("failed")
131
		}
132
	}
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))
136
	}
137
}
138

139
func mockPartitionRaft(ctrl *gomock.Controller) *metaPartition {
140
	conf := &MetaPartitionConfig{
141
		VerSeq: 0,
142
	}
143
	partition := NewMetaPartition(conf, nil).(*metaPartition)
144
	partition.uniqChecker.keepTime = 1
145
	partition.uniqChecker.keepOps = 0
146
	raft := raftstoremock.NewMockPartition(ctrl)
147
	idx := uint64(0)
148
	raft.EXPECT().Submit(gomock.Any()).DoAndReturn(func(cmd []byte) (resp interface{}, err error) {
149
		idx++
150
		return partition.Apply(cmd, idx)
151
	}).AnyTimes()
152

153
	raft.EXPECT().LeaderTerm().DoAndReturn(func() (uint64, uint64) {
154
		return 1, 1
155
	}).AnyTimes()
156

157
	partition.raftPartition = raft
158

159
	return partition
160
}
161

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.