浏览代码

Merge pull request #1131 from bcwaldon/1129

etcdserver: init time.Time only if Expiration > 0
Brian Waldon 11 年之前
父节点
当前提交
ceab948831
共有 2 个文件被更改,包括 36 次插入1 次删除
  1. 9 1
      etcdserver/server.go
  2. 27 0
      etcdserver/server_test.go

+ 9 - 1
etcdserver/server.go

@@ -241,12 +241,20 @@ func (s *EtcdServer) sync(timeout time.Duration) {
 	}()
 }
 
+func getExpirationTime(r *pb.Request) time.Time {
+	var t time.Time
+	if r.Expiration != 0 {
+		t = time.Unix(0, r.Expiration)
+	}
+	return t
+}
+
 // apply interprets r as a call to store.X and returns an Response interpreted from store.Event
 func (s *EtcdServer) apply(r pb.Request) Response {
 	f := func(ev *store.Event, err error) Response {
 		return Response{Event: ev, err: err}
 	}
-	expr := time.Unix(0, r.Expiration)
+	expr := getExpirationTime(&r)
 	switch r.Method {
 	case "POST":
 		return f(s.Store.Create(r.Path, r.Dir, r.Val, true, expr))

+ 27 - 0
etcdserver/server_test.go

@@ -16,6 +16,33 @@ import (
 	"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
 )
 
+func TestGetExpirationTime(t *testing.T) {
+	tests := []struct {
+		r    pb.Request
+		want time.Time
+	}{
+		{
+			pb.Request{Expiration: 0},
+			time.Time{},
+		},
+		{
+			pb.Request{Expiration: 60000},
+			time.Unix(0, 60000),
+		},
+		{
+			pb.Request{Expiration: -60000},
+			time.Unix(0, -60000),
+		},
+	}
+
+	for i, tt := range tests {
+		got := getExpirationTime(&tt.r)
+		if !reflect.DeepEqual(tt.want, got) {
+			t.Errorf("#%d: incorrect expiration time: want=%v got=%v", i, tt.want, got)
+		}
+	}
+}
+
 // TestDoLocalAction tests requests which do not need to go through raft to be applied,
 // and are served through local data.
 func TestDoLocalAction(t *testing.T) {