Browse Source
Merge pull request #934 from stlpmo-jn/parse_fileid_from_string
Merge pull request #934 from stlpmo-jn/parse_fileid_from_string
add function ParseFileIdFromStringpull/942/head
Chris Lu
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 150 additions and 4 deletions
-
46weed/storage/needle/file_id.go
-
55weed/storage/needle/file_id_test.go
-
8weed/storage/needle/volume_id.go
-
45weed/storage/needle/volume_id_test.go
@ -0,0 +1,55 @@ |
|||||
|
package needle |
||||
|
|
||||
|
import ( |
||||
|
"github.com/chrislusf/seaweedfs/weed/storage/types" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
func TestParseFileIdFromString(t *testing.T) { |
||||
|
fidStr1 := "100,12345678" |
||||
|
_, err := ParseFileIdFromString(fidStr1) |
||||
|
if err == nil { |
||||
|
t.Errorf("%s : KeyHash is too short", fidStr1) |
||||
|
} |
||||
|
|
||||
|
fidStr1 = "100, 12345678" |
||||
|
_, err = ParseFileIdFromString(fidStr1) |
||||
|
if err == nil { |
||||
|
t.Errorf("%s : needlId invalid syntax", fidStr1) |
||||
|
} |
||||
|
|
||||
|
fidStr1 = "100,123456789" |
||||
|
_, err = ParseFileIdFromString(fidStr1) |
||||
|
if err != nil { |
||||
|
t.Errorf("%s : should be OK", fidStr1) |
||||
|
} |
||||
|
|
||||
|
var fileId *FileId |
||||
|
fidStr1 = "100,123456789012345678901234" |
||||
|
fileId, err = ParseFileIdFromString(fidStr1) |
||||
|
if err != nil { |
||||
|
t.Errorf("%s : should be OK", fidStr1) |
||||
|
} |
||||
|
if !(fileId.VolumeId == VolumeId(100) && |
||||
|
fileId.Key == types.NeedleId(0x1234567890123456) && |
||||
|
fileId.Cookie == types.Cookie(types.Uint32ToCookie(uint32(0x78901234)))) { |
||||
|
t.Errorf("src : %s, dest : %v", fidStr1, fileId) |
||||
|
} |
||||
|
|
||||
|
fidStr1 = "100,abcd0000abcd" |
||||
|
fileId, err = ParseFileIdFromString(fidStr1) |
||||
|
if err != nil { |
||||
|
t.Errorf("%s : should be OK", fidStr1) |
||||
|
} |
||||
|
if !(fileId.VolumeId == VolumeId(100) && |
||||
|
fileId.Key == types.NeedleId(0xabcd) && |
||||
|
fileId.Cookie == types.Cookie(types.Uint32ToCookie(uint32(0xabcd)))) { |
||||
|
t.Errorf("src : %s, dest : %v", fidStr1, fileId) |
||||
|
} |
||||
|
|
||||
|
fidStr1 = "100,1234567890123456789012345" |
||||
|
_, err = ParseFileIdFromString(fidStr1) |
||||
|
if err == nil { |
||||
|
t.Errorf("%s : needleId is too long", fidStr1) |
||||
|
} |
||||
|
} |
@ -0,0 +1,45 @@ |
|||||
|
package needle |
||||
|
|
||||
|
import "testing" |
||||
|
|
||||
|
func TestNewVolumeId(t *testing.T) { |
||||
|
if _,err := NewVolumeId("1"); err != nil { |
||||
|
t.Error(err) |
||||
|
} |
||||
|
|
||||
|
if _, err := NewVolumeId("a");err != nil { |
||||
|
t.Logf("a is not legal volume id, %v", err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func TestVolumeId_String(t *testing.T) { |
||||
|
if str := VolumeId(10).String(); str != "10" { |
||||
|
t.Errorf("to string failed") |
||||
|
} |
||||
|
|
||||
|
vid := VolumeId(11) |
||||
|
if str := vid.String(); str != "11" { |
||||
|
t.Errorf("to string failed") |
||||
|
} |
||||
|
|
||||
|
pvid := &vid |
||||
|
if str := pvid.String(); str != "11" { |
||||
|
t.Errorf("to string failed") |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func TestVolumeId_Next(t *testing.T) { |
||||
|
if vid := VolumeId(10).Next(); vid != VolumeId(11) { |
||||
|
t.Errorf("get next volume id failed") |
||||
|
} |
||||
|
|
||||
|
vid := VolumeId(11) |
||||
|
if new := vid.Next(); new != 12 { |
||||
|
t.Errorf("get next volume id failed") |
||||
|
} |
||||
|
|
||||
|
pvid := &vid |
||||
|
if new := pvid.Next(); new != 12 { |
||||
|
t.Errorf("get next volume id failed") |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue