forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_test.go
217 lines (182 loc) · 4.34 KB
/
transaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package proto
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGetMaskFromString(t *testing.T) {
cs := []struct {
op TxOpMask
str string
}{
{TxPause, "pause"},
{TxOpMaskAll, "all"},
{TxOpMaskOff, "off"},
{TxOpMaskCreate, "create"},
{TxOpMaskRename, "rename"},
}
for _, c := range cs {
str := GetMaskString(c.op)
require.Equal(t, str, c.str)
}
}
func TestMaskContains(t *testing.T) {
cs := []struct {
total TxOpMask
sub TxOpMask
result bool
}{
{TxOpMaskOff, TxOpMaskOff, true},
{TxOpMaskOff, TxOpMaskRemove, false},
{TxOpMaskRemove | TxOpMaskRename, TxOpMaskRename, true},
{TxOpMaskAll, TxPause, false},
{TxOpMaskAll, TxOpMaskRename, true},
{TxPause, TxPause, true},
}
for _, c := range cs {
got := MaskContains(c.total, c.sub)
require.Equal(t, c.result, got)
}
}
func TestTxInodeInfoMarshal(t *testing.T) {
ifo := &TxInodeInfo{
Ino: 101,
MpMembers: "m1,m2,m3",
MpID: 11,
CreateTime: 10110,
Timeout: 112,
// TxID: "tx123",
}
bs, err := ifo.Marshal()
require.NoError(t, err)
nIfo := &TxInodeInfo{}
err = nIfo.Unmarshal(bs)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(ifo, nIfo))
nIfo.TxID = "txId1"
require.False(t, reflect.DeepEqual(ifo, nIfo))
}
func TestTxDentryInfoMarshal(t *testing.T) {
ifo := &TxDentryInfo{
ParentId: 101,
Name: "dir1",
MpMembers: "m1,m2,m3",
MpID: 11,
CreateTime: 10110,
Timeout: 112,
// TxID: "tx123",
}
bs, err := ifo.Marshal()
require.NoError(t, err)
nIfo := &TxDentryInfo{}
err = nIfo.Unmarshal(bs)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(ifo, nIfo))
nIfo.TxID = "txId1"
require.False(t, reflect.DeepEqual(ifo, nIfo))
}
func TestTransactionInfo_Marshal(t *testing.T) {
tx := NewTransactionInfo(1, TxTypeRename)
tx.RMFinish = true
tx.DoneTime = 1012
tx.CreateTime = 101
tx.State = TxStateRollbackDone
require.False(t, tx.IsInitialized())
inoIfo := &TxInodeInfo{
Ino: 10,
TxID: "tx_11",
}
tx.TxInodeInfos[inoIfo.GetKey()] = inoIfo
dIfo := &TxDentryInfo{
ParentId: 10,
Name: "na1",
TxID: "tx_11",
}
tx.TxDentryInfos[dIfo.GetKey()] = dIfo
bs, err := tx.Marshal()
require.NoError(t, err)
ntx := NewTransactionInfo(2, TxTypeLink)
err = ntx.Unmarshal(bs)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(tx, ntx))
tx.TxID = "txId_1"
require.False(t, reflect.DeepEqual(tx, ntx))
cpTx := tx.Copy().(*TransactionInfo)
require.True(t, reflect.DeepEqual(tx, cpTx))
}
func TestTransactionInfo_SetCreateInodeId(t *testing.T) {
tx := NewTransactionInfo(1, TxTypeRename)
inoIfo := &TxInodeInfo{
TxID: "tx_11",
}
tx.TxInodeInfos[inoIfo.GetKey()] = inoIfo
ino := uint64(11)
tx.SetCreateInodeId(ino)
nifo := tx.TxInodeInfos[ino]
require.True(t, reflect.DeepEqual(nifo, inoIfo))
require.True(t, len(tx.TxInodeInfos) == 1)
}
func TestTransactionInfo_Less(t *testing.T) {
tx := &TransactionInfo{
TxID: "10_11",
}
tx2 := &TransactionInfo{
TxID: "11_10",
}
require.True(t, tx.Less(tx2))
}
func TestTransactionInfo_GroupByMp(t *testing.T) {
mp1, mp2 := uint64(1), uint64(2)
addr1, addr2 := "m1,m1", "m2,m2"
ino1 := &TxInodeInfo{
Ino: 1,
MpID: mp1,
MpMembers: addr1,
}
ino2 := &TxInodeInfo{
Ino: 2,
MpID: mp2,
MpMembers: addr2,
}
d1 := &TxDentryInfo{
ParentId: 1,
MpID: mp1,
MpMembers: addr1,
}
d2 := &TxDentryInfo{
ParentId: 2,
MpID: mp2,
MpMembers: addr2,
}
tx := NewTransactionInfo(0, TxTypeRename)
tx.TxInodeInfos[ino1.GetKey()] = ino1
tx.TxInodeInfos[ino2.GetKey()] = ino2
tx.TxDentryInfos[d1.GetKey()] = d1
tx.TxDentryInfos[d2.GetKey()] = d2
mps := tx.GroupByMp()
require.True(t, len(mps) == 2)
for id, mp := range mps {
if id == mp1 {
require.Equal(t, addr1, mp.Members)
} else {
require.Equal(t, addr2, mp.Members)
}
require.Equal(t, len(mp.TxDentryInfos), 1)
require.Equal(t, len(mp.TxInodeInfos), 1)
}
}
func TestTxFunc(t *testing.T) {
tx := NewTransactionInfo(1, TxTypeRename)
require.False(t, tx.IsDone())
tx.State = TxStateRollbackDone
require.True(t, tx.IsDone())
tx.State = TxStateCommitDone
require.True(t, tx.IsDone())
// can finish
tx.RMFinish = true
tx.DoneTime = time.Now().Unix() - 130
require.True(t, tx.CanDelete())
tx.DoneTime = time.Now().Unix() - 61
require.True(t, tx.IsExpired())
}