From cea29fe158c953cf0ac2c815bc74398351dcceb1 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 29 Dec 2014 11:15:02 -0800 Subject: [PATCH] etcdserver: move getExpr to timeutil --- etcdserver/server.go | 11 ++------ etcdserver/server_test.go | 27 ------------------- pkg/timeutil/timeutil.go | 29 ++++++++++++++++++++ pkg/timeutil/timeutil_test.go | 50 +++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 36 deletions(-) create mode 100644 pkg/timeutil/timeutil.go create mode 100644 pkg/timeutil/timeutil_test.go diff --git a/etcdserver/server.go b/etcdserver/server.go index 0b18c0ba3..4fcb3f394 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -38,6 +38,7 @@ import ( "github.com/coreos/etcd/etcdserver/stats" "github.com/coreos/etcd/pkg/fileutil" "github.com/coreos/etcd/pkg/pbutil" + "github.com/coreos/etcd/pkg/timeutil" "github.com/coreos/etcd/pkg/types" "github.com/coreos/etcd/pkg/wait" "github.com/coreos/etcd/raft" @@ -664,14 +665,6 @@ func (s *EtcdServer) publish(retryInterval 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 -} - func (s *EtcdServer) send(ms []raftpb.Message) { for _, m := range ms { if !s.Cluster.IsIDRemoved(types.ID(m.To)) { @@ -717,7 +710,7 @@ func (s *EtcdServer) applyRequest(r pb.Request) Response { f := func(ev *store.Event, err error) Response { return Response{Event: ev, err: err} } - expr := getExpirationTime(&r) + expr := timeutil.UnixNanoToTime(r.Expiration) switch r.Method { case "POST": return f(s.store.Create(r.Path, r.Dir, r.Val, true, expr)) diff --git a/etcdserver/server_test.go b/etcdserver/server_test.go index 1b432716b..242d0728e 100644 --- a/etcdserver/server_test.go +++ b/etcdserver/server_test.go @@ -45,33 +45,6 @@ func init() { log.SetOutput(ioutil.Discard) } -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) { diff --git a/pkg/timeutil/timeutil.go b/pkg/timeutil/timeutil.go new file mode 100644 index 000000000..1a15afb86 --- /dev/null +++ b/pkg/timeutil/timeutil.go @@ -0,0 +1,29 @@ +/* + Copyright 2014 CoreOS, Inc. + + 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 timeutil + +import "time" + +// UnixNanoToTime returns the local time corresponding to the given Unix time in nanoseconds. +// If the given Unix time is zero, an uninitialized zero time is returned. +func UnixNanoToTime(ns int64) time.Time { + var t time.Time + if ns != 0 { + t = time.Unix(0, ns) + } + return t +} diff --git a/pkg/timeutil/timeutil_test.go b/pkg/timeutil/timeutil_test.go new file mode 100644 index 000000000..4f47cd35d --- /dev/null +++ b/pkg/timeutil/timeutil_test.go @@ -0,0 +1,50 @@ +/* + Copyright 2014 CoreOS, Inc. + + 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 timeutil + +import ( + "reflect" + "testing" + "time" +) + +func TestUnixNanoToTime(t *testing.T) { + tests := []struct { + ns int64 + want time.Time + }{ + { + 0, + time.Time{}, + }, + { + 60000, + time.Unix(0, 60000), + }, + { + -60000, + time.Unix(0, -60000), + }, + } + + for i, tt := range tests { + got := UnixNanoToTime(tt.ns) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("#%d: time = %v, want %v", i, got, tt.want) + } + } +}