cubefs

Форк
0
/
uniq_checker_test.go 
141 строка · 3.1 Кб
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

21
func TestLegal(t *testing.T) {
22
	checker := newUniqChecker()
23
	for i := 1; i <= 10; i++ {
24
		if !checker.legalIn(uint64(i)) {
25
			t.Errorf("failed")
26
		}
27
	}
28
	if checker.legalIn(1) {
29
		t.Errorf("failed, %v", checker.op)
30
	}
31
}
32

33
func TestOpQueue(t *testing.T) {
34
	q := newUniqOpQueue()
35
	for i := uint64(0); i < 10000; i++ {
36
		q.append(&uniqOp{
37
			uniqid: i,
38
		})
39
	}
40

41
	cnt := uint64(0)
42
	q.scan(func(op *uniqOp) bool {
43
		if op.uniqid != cnt {
44
			t.Fatalf("op queue scan failed")
45
		}
46
		cnt++
47
		return true
48
	})
49
	if cnt != 10000 {
50
		t.Fatalf("scan failed %v", cnt)
51
	}
52

53
	op := q.index(4567)
54
	if op.uniqid != 4567 {
55
		t.Fatalf("q.index 4567 failed")
56
	}
57

58
	q.truncate(4567)
59

60
	op = q.index(0)
61
	if op.uniqid != 4568 {
62
		t.Fatalf("q.index 4568 failed")
63
	}
64

65
	if q.len() != 10000-4567-1 {
66
		t.Fatalf("op queue trancate failed")
67
	}
68

69
	q.scan(func(op *uniqOp) bool {
70
		if op.uniqid != 4568 {
71
			t.Fatalf("op queue trancate scan failed")
72
		}
73
		return false
74
	})
75

76
	clone := q.clone()
77
	for i := uint64(10000); i < 20000; i++ {
78
		q.append(&uniqOp{uniqid: i})
79
	}
80

81
	if q.len()-clone.len() != 10000 || q.index(1234).uniqid != clone.index(1234).uniqid {
82
		t.Fatalf("op queue clone failed")
83
	}
84

85
	q.reset()
86
	if q.len() != 0 || len(q.cur.s) != 0 || len(q.ss) != 1 {
87
		t.Fatalf("op queue trancate failed")
88
	}
89
}
90

91
func TestClone(t *testing.T) {
92
	checker := newUniqChecker()
93
	for i := 1; i <= 10000; i++ {
94
		checker.legalIn(uint64(i))
95
	}
96

97
	checker1 := checker.clone()
98
	if len(checker1.op) != 0 || checker.inQue.len() != checker1.inQue.len() {
99
		t.Errorf("failed")
100
	}
101

102
	i := 0
103
	checker.inQue.scan(func(op *uniqOp) bool {
104
		if op.uniqid != checker1.inQue.index(i).uniqid || op.atime != checker1.inQue.index(i).atime {
105
			t.Errorf("failed")
106
			return false
107
		}
108
		i++
109
		return true
110
	})
111
}
112

113
func TestMarshal(t *testing.T) {
114
	checker := newUniqChecker()
115
	for i := 1; i <= 10000; i++ {
116
		checker.legalIn(uint64(i))
117
	}
118

119
	bts, _, _ := checker.Marshal()
120
	checker1 := newUniqChecker()
121
	checker1.UnMarshal(bts)
122

123
	if len(checker.op) != len(checker1.op) || checker.inQue.len() != checker1.inQue.len() {
124
		t.Errorf("failed")
125
	}
126

127
	i := 0
128
	checker.inQue.scan(func(v *uniqOp) bool {
129
		if v.uniqid != checker1.inQue.index(i).uniqid || v.atime != checker1.inQue.index(i).atime {
130
			t.Errorf("failed, id(%v, %v), atime(%v, %v)", v.uniqid, checker1.inQue.index(i).uniqid, v.atime, checker1.inQue.index(i).atime)
131
			return false
132
		}
133

134
		if _, ok := checker1.op[v.uniqid]; !ok {
135
			t.Errorf("failed, %v, %v", checker.op[v.uniqid], checker1.op[v.uniqid])
136
			return false
137
		}
138
		i++
139
		return true
140
	})
141
}
142

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

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

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

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