Merge pull request #2011 from xiang90/timeutil

etcdserver: move getExpr to timeutil
release-2.0
Xiang Li 2014-12-29 12:03:25 -08:00
commit 7c8b9c0203
4 changed files with 81 additions and 36 deletions

View File

@ -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))

View File

@ -44,33 +44,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) {

29
pkg/timeutil/timeutil.go Normal file
View File

@ -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
}

View File

@ -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)
}
}
}