forked from true1064/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart_test.go
170 lines (161 loc) · 4.78 KB
/
multipart_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
// Copyright 2018 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package metanode
import (
"math/rand"
"reflect"
"testing"
"time"
"github.com/cubefs/cubefs/util"
)
func TestMUPart_Bytes(t *testing.T) {
var (
id uint16 = 1
uploadTime = time.Now().Local()
md5 = util.RandomString(16, util.UpperLetter|util.Numeric)
size uint64 = 65536
inode uint64 = 12345
err error
)
part1 := &Part{
ID: id,
UploadTime: uploadTime,
MD5: md5,
Size: size,
Inode: inode,
}
var partBytes []byte
if partBytes, err = part1.Bytes(); err != nil {
t.Fatalf("get bytes of part fail cause: %v", err)
}
part2 := PartFromBytes(partBytes)
if !reflect.DeepEqual(part1, part2) {
t.Fatalf("result mismatch:\n\tpart1:%v\n\tpart2:%v", part1, part2)
}
t.Logf("encoded length: %v", len(partBytes))
}
func TestMUParts_Bytes(t *testing.T) {
var err error
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
var parts1 = PartsFromBytes(nil)
for i := 0; i < 100; i++ {
part := &Part{
ID: uint16(i),
UploadTime: time.Now().Local(),
MD5: util.RandomString(16, util.UpperLetter|util.Numeric),
Size: random.Uint64(),
Inode: random.Uint64(),
}
parts1.Insert(part, false)
}
var partsBytes []byte
partsBytes, err = parts1.Bytes()
if partsBytes, err = parts1.Bytes(); err != nil {
t.Fatalf("get bytes of part fail cause: %v", err)
}
parts2 := PartsFromBytes(partsBytes)
if !reflect.DeepEqual(parts1, parts2) {
t.Fatalf("result mismatch:\n\tpart1:%v\n\tpart2:%v", parts1, parts2)
}
t.Logf("encoded length: %v", len(partsBytes))
}
func TestMUParts_Modify(t *testing.T) {
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
var parts = PartsFromBytes(nil)
for i := 0; i < 100; i++ {
part := &Part{
ID: uint16(i),
UploadTime: time.Now().Local(),
MD5: util.RandomString(16, util.UpperLetter|util.Numeric),
Size: random.Uint64(),
Inode: random.Uint64(),
}
parts.Insert(part, false)
}
if parts.Len() != 100 {
t.Fatalf("parts length mismatch: except 100 actual %v", parts.Len())
}
// validate before modify
if _, found := parts.Search(0); !found {
t.Fatalf("part id[0] not found before modify")
}
if _, found := parts.Search(50); !found {
t.Fatalf("part id[50] not found before modify")
}
if _, found := parts.Search(99); !found {
t.Fatalf("part id[99] not found before modify")
}
// modify
parts.Remove(0)
parts.Remove(50)
parts.Remove(99)
if parts.Len() != 97 {
t.Fatalf("parts length mismatch: expect 97 actual %v", parts.Len())
}
// validate after modify
if _, found := parts.Search(0); found {
t.Fatalf("part id[0] not found before modify")
}
if _, found := parts.Search(50); found {
t.Fatalf("part id[50] not found before modify")
}
if _, found := parts.Search(99); found {
t.Fatalf("part id[99] not found before modify")
}
}
func TestMUSession_Bytes(t *testing.T) {
var err error
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
var session1 = MultipartFromBytes(nil)
me := NewMultipartExtend()
me["oss::tag"] = "name=123&age456"
me["oss::disposition"] = "attachment=file.txt"
session1.extend = me
for i := 0; i < 100; i++ {
id := uint16(i)
md5 := util.RandomString(16, util.UpperLetter|util.Numeric)
size := random.Uint64()
inode := random.Uint64()
session1.InsertPart(&Part{
ID: id,
MD5: md5,
Size: size,
Inode: inode,
UploadTime: time.Now().Local(),
}, false)
}
var sessionBytes []byte
sessionBytes, err = session1.Bytes()
if err != nil {
t.Fatalf("encode session to bytes fail caue: %v", err)
}
session2 := MultipartFromBytes(sessionBytes)
if !reflect.DeepEqual(session1, session2) {
t.Fatalf("result mismatch:\n\tsession1:%v\n\tsession2:%v", session1, session2)
}
t.Logf("encoded session length: %v", len(sessionBytes))
}
func TestMultipartExtend_Bytes(t *testing.T) {
me := NewMultipartExtend()
me["oss::tag"] = "name=123&age456"
me["oss::disposition"] = "attachment=file.txt"
bytes, err := me.Bytes()
if err != nil {
t.Errorf("Encode multipart extend fail cause : %v", err)
}
me2 := MultipartExtendFromBytes(bytes)
if !reflect.DeepEqual(me, me2) {
t.Fatalf("result mismatch:\n\tme1:%v\n\tme2:%v", me, me2)
}
}