aboutsummaryrefslogtreecommitdiff
path: root/go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2014-09-20 20:51:24 -0700
committerChris Lu <chris.lu@gmail.com>2014-09-20 20:51:24 -0700
commit7920b4685e41407c3bcc64fd22579cfe0f27bda8 (patch)
treed22d4f6a8739ef990aebde857b8dfa31dcd6221f /go
parentf7094d7a99f73db855f59afafbf6e1dd276ba394 (diff)
downloadseaweedfs-7920b4685e41407c3bcc64fd22579cfe0f27bda8.tar.xz
seaweedfs-7920b4685e41407c3bcc64fd22579cfe0f27bda8.zip
Adding unit tests for volume ttl.
Diffstat (limited to 'go')
-rw-r--r--go/storage/needle.go2
-rw-r--r--go/storage/volume_ttl.go16
-rw-r--r--go/storage/volume_ttl_test.go60
3 files changed, 63 insertions, 15 deletions
diff --git a/go/storage/needle.go b/go/storage/needle.go
index 3bf627141..51079376a 100644
--- a/go/storage/needle.go
+++ b/go/storage/needle.go
@@ -118,7 +118,7 @@ func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
n.LastModified = uint64(time.Now().Unix())
}
n.SetHasLastModifiedDate()
- if n.Ttl != nil {
+ if n.Ttl != EMPTY_TTL {
n.SetHasTtl()
}
diff --git a/go/storage/volume_ttl.go b/go/storage/volume_ttl.go
index 5ff43e24e..459ee55ba 100644
--- a/go/storage/volume_ttl.go
+++ b/go/storage/volume_ttl.go
@@ -4,10 +4,6 @@ import (
"strconv"
)
-var (
- TtlRange []uint16
-)
-
const (
//stored unit types
Empty byte = iota
@@ -19,14 +15,6 @@ const (
Year
)
-func init() {
- TtlRange = []uint16{3, 10, 30,
- 60 /*1 hour*/, 60 * 3, 60 * 6, 60 * 12,
- 1440 /*1 day*/, 1440 * 3, 1440 * 7, 1440 * 15, 1440 * 31,
- 44888 /*1 month*/, 65535,
- }
-}
-
type TTL struct {
count byte
unit byte
@@ -120,7 +108,7 @@ func toStoredByte(readableUnitByte byte) byte {
return Week
case 'M':
return Month
- case 'Y':
+ case 'y':
return Year
}
return 0
@@ -141,7 +129,7 @@ func (t TTL) Minutes() uint32 {
case Month:
return uint32(t.count) * 60 * 24 * 31
case Year:
- return uint32(t.count) * 60 * 24 * 31 * 365
+ return uint32(t.count) * 60 * 24 * 365
}
return 0
}
diff --git a/go/storage/volume_ttl_test.go b/go/storage/volume_ttl_test.go
new file mode 100644
index 000000000..216469a4c
--- /dev/null
+++ b/go/storage/volume_ttl_test.go
@@ -0,0 +1,60 @@
+package storage
+
+import (
+ "testing"
+)
+
+func TestTTLReadWrite(t *testing.T) {
+ ttl, _ := ReadTTL("")
+ if ttl.Minutes() != 0 {
+ t.Errorf("empty ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("9")
+ if ttl.Minutes() != 9 {
+ t.Errorf("9 ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("8m")
+ if ttl.Minutes() != 8 {
+ t.Errorf("8m ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("5h")
+ if ttl.Minutes() != 300 {
+ t.Errorf("5h ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("5d")
+ if ttl.Minutes() != 5*24*60 {
+ t.Errorf("5d ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("5w")
+ if ttl.Minutes() != 5*7*24*60 {
+ t.Errorf("5w ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("5M")
+ if ttl.Minutes() != 5*31*24*60 {
+ t.Errorf("5M ttl:%v", ttl)
+ }
+
+ ttl, _ = ReadTTL("5y")
+ if ttl.Minutes() != 5*365*24*60 {
+ t.Errorf("5y ttl:%v", ttl)
+ }
+
+ output := make([]byte, 2)
+ ttl.ToBytes(output)
+ ttl2 := LoadTTLFromBytes(output)
+ if ttl.Minutes() != ttl2.Minutes() {
+ t.Errorf("ttl:%v ttl2:%v", ttl, ttl2)
+ }
+
+ ttl3 := LoadTTLFromUint32(ttl.ToUint32())
+ if ttl.Minutes() != ttl3.Minutes() {
+ t.Errorf("ttl:%v ttl3:%v", ttl, ttl3)
+ }
+
+}