2016-05-13 06:51:48 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2013-10-07 20:44:51 +04:00
|
|
|
|
2013-09-29 03:26:19 +04:00
|
|
|
package store
|
2013-09-03 22:30:42 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2013-10-20 23:39:29 +04:00
|
|
|
|
|
|
|
etcdErr "github.com/coreos/etcd/error"
|
2016-03-23 03:10:28 +03:00
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2013-09-03 22:30:42 +04:00
|
|
|
)
|
|
|
|
|
2015-02-05 03:33:40 +03:00
|
|
|
func TestNewStoreWithNamespaces(t *testing.T) {
|
|
|
|
s := newStore("/0", "/1")
|
|
|
|
|
|
|
|
_, err := s.Get("/0", false, false)
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
_, err = s.Get("/1", false, false)
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can retrieve an existing value.
|
|
|
|
func TestStoreGetValue(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err := s.Get("/foo", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "get", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 08:32:48 +04:00
|
|
|
// Ensure that any TTL <= minExpireTime becomes Permanent
|
|
|
|
func TestMinExpireTime(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
|
s.clock = fc
|
|
|
|
// FakeClock starts at 0, so minExpireTime should be far in the future.. but just in case
|
|
|
|
assert.True(t, minExpireTime.After(fc.Now()), "minExpireTime should be ahead of FakeClock!")
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(3 * time.Second)})
|
2014-10-17 08:32:48 +04:00
|
|
|
fc.Advance(5 * time.Second)
|
|
|
|
// Ensure it hasn't expired
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
|
|
|
var eidx uint64 = 1
|
|
|
|
e, err := s.Get("/foo", true, false)
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
|
|
|
assert.Equal(t, e.Action, "get", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0))
|
2014-10-17 08:32:48 +04:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// Ensure that the store can recursively retrieve a directory listing.
|
2013-10-20 23:39:29 +04:00
|
|
|
// Note that hidden files should not be returned.
|
|
|
|
func TestStoreGetDirectory(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2014-10-17 02:19:00 +04:00
|
|
|
s.clock = fc
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/bar", false, "X", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/baz", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/baz/bat", false, "Y", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/baz/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/baz/ttl", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(time.Second * 3)})
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 7
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err := s.Get("/foo", true, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "get", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.Equal(t, len(e.Node.Nodes), 2, "")
|
2014-08-16 06:23:31 +04:00
|
|
|
var bazNodes NodeExterns
|
|
|
|
for _, node := range e.Node.Nodes {
|
|
|
|
switch node.Key {
|
|
|
|
case "/foo/bar":
|
|
|
|
assert.Equal(t, *node.Value, "X", "")
|
|
|
|
assert.Equal(t, node.Dir, false, "")
|
|
|
|
case "/foo/baz":
|
|
|
|
assert.Equal(t, node.Dir, true, "")
|
|
|
|
assert.Equal(t, len(node.Nodes), 2, "")
|
|
|
|
bazNodes = node.Nodes
|
|
|
|
default:
|
|
|
|
t.Errorf("key = %s, not matched", node.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, node := range bazNodes {
|
|
|
|
switch node.Key {
|
|
|
|
case "/foo/baz/bat":
|
|
|
|
assert.Equal(t, *node.Value, "Y", "")
|
|
|
|
assert.Equal(t, node.Dir, false, "")
|
|
|
|
case "/foo/baz/ttl":
|
|
|
|
assert.Equal(t, *node.Value, "Y", "")
|
|
|
|
assert.Equal(t, node.Dir, false, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, node.TTL, int64(3), "")
|
2014-08-16 06:23:31 +04:00
|
|
|
default:
|
|
|
|
t.Errorf("key = %s, not matched", node.Key)
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 07:57:19 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can retrieve a directory in sorted order.
|
|
|
|
func TestStoreGetSorted(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/x", false, "0", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/z", false, "0", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/y", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/y/a", false, "0", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/y/b", false, "0", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 6
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err := s.Get("/foo", true, true)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2016-11-29 16:58:18 +03:00
|
|
|
|
2014-08-16 06:23:31 +04:00
|
|
|
var yNodes NodeExterns
|
2016-11-29 16:58:18 +03:00
|
|
|
sortedStrings := []string{"/foo/x", "/foo/y", "/foo/z"}
|
|
|
|
for i := range e.Node.Nodes {
|
|
|
|
node := e.Node.Nodes[i]
|
|
|
|
if node.Key != sortedStrings[i] {
|
|
|
|
t.Errorf("expect key = %s, got key = %s", sortedStrings[i], node.Key)
|
|
|
|
}
|
|
|
|
if node.Key == "/foo/y" {
|
2014-08-16 06:23:31 +04:00
|
|
|
yNodes = node.Nodes
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 16:58:18 +03:00
|
|
|
|
|
|
|
sortedStrings = []string{"/foo/y/a", "/foo/y/b"}
|
|
|
|
for i := range yNodes {
|
|
|
|
node := yNodes[i]
|
|
|
|
if node.Key != sortedStrings[i] {
|
|
|
|
t.Errorf("expect key = %s, got key = %s", sortedStrings[i], node.Key)
|
2014-08-16 06:23:31 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-04 07:10:33 +04:00
|
|
|
|
2013-12-06 05:46:52 +04:00
|
|
|
func TestSet(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
|
|
|
|
// Set /foo=""
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err := s.Set("/foo", false, "", TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Action, "set", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "", "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, e.Node.Nodes, "")
|
|
|
|
assert.Nil(t, e.Node.Expiration, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(1), "")
|
|
|
|
|
|
|
|
// Set /foo="bar"
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err = s.Set("/foo", false, "bar", TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Action, "set", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, e.Node.Nodes, "")
|
|
|
|
assert.Nil(t, e.Node.Expiration, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
|
2014-01-01 15:50:07 +04:00
|
|
|
// check prevNode
|
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "", "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
|
2014-01-30 05:52:25 +04:00
|
|
|
// Set /foo="baz" (for testing prevNode)
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 3
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err = s.Set("/foo", false, "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-01-30 05:52:25 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-01-30 05:52:25 +04:00
|
|
|
assert.Equal(t, e.Action, "set", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-01-30 05:52:25 +04:00
|
|
|
assert.Nil(t, e.Node.Nodes, "")
|
|
|
|
assert.Nil(t, e.Node.Expiration, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2014-01-30 05:52:25 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "")
|
|
|
|
// check prevNode
|
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2014-01-30 05:52:25 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(2), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
|
|
|
|
// Set /dir as a directory
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 4
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err = s.Set("/dir", true, "", TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Action, "set", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/dir", "")
|
|
|
|
assert.True(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Nil(t, e.Node.Value)
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, e.Node.Nodes, "")
|
|
|
|
assert.Nil(t, e.Node.Expiration, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2014-01-30 05:52:25 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(4), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can create a new key if it doesn't already exist.
|
|
|
|
func TestStoreCreateValue(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 05:46:52 +04:00
|
|
|
// Create /foo=bar
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err := s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Nil(t, e.Node.Nodes, "")
|
|
|
|
assert.Nil(t, e.Node.Expiration, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(1), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
|
|
|
|
// Create /empty=""
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err = s.Create("/empty", false, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/empty", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "", "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, e.Node.Nodes, "")
|
|
|
|
assert.Nil(t, e.Node.Expiration, "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
|
|
|
|
|
2013-09-04 07:10:33 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can create a new directory if it doesn't already exist.
|
|
|
|
func TestStoreCreateDirectory(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err := s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.True(t, e.Node.Dir, "")
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store fails to create a key if it already exists.
|
|
|
|
func TestStoreCreateFailsIfExists(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2013-12-06 05:46:52 +04:00
|
|
|
// create /foo as dir
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
|
|
|
|
// create /foo as dir again
|
2016-01-07 23:20:44 +03:00
|
|
|
e, _err := s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNodeExist, "")
|
2013-12-06 03:16:01 +04:00
|
|
|
assert.Equal(t, err.Message, "Key already exists", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, err.Cause, "/foo", "")
|
2013-11-10 05:55:54 +04:00
|
|
|
assert.Equal(t, err.Index, uint64(1), "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, e, 0, "")
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can update a key if it already exists.
|
|
|
|
func TestStoreUpdateValue(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2013-12-06 05:46:52 +04:00
|
|
|
// create /foo=bar
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
// update /foo="bzr"
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "update", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
|
2014-01-01 15:50:07 +04:00
|
|
|
// check prevNode
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.PrevNode.TTL, int64(0), "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
|
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
|
|
|
|
// update /foo=""
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 3
|
2016-01-07 23:20:44 +03:00
|
|
|
e, err = s.Update("/foo", "", TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Action, "update", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.False(t, e.Node.Dir, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "", "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.Node.TTL, int64(0), "")
|
2013-12-06 05:46:52 +04:00
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "")
|
2014-01-01 15:50:07 +04:00
|
|
|
// check prevNode
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "baz", "")
|
2016-11-09 00:00:12 +03:00
|
|
|
assert.Equal(t, e.PrevNode.TTL, int64(0), "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(2), "")
|
|
|
|
|
2013-12-06 05:46:52 +04:00
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "", "")
|
2013-09-05 23:38:22 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store cannot update a directory.
|
|
|
|
func TestStoreUpdateFailsIfDirectory(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
e, _err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
2014-01-23 23:22:11 +04:00
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
|
|
|
|
assert.Equal(t, err.Message, "Not a file", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, err.Cause, "/foo", "")
|
|
|
|
assert.Nil(t, e, "")
|
2013-09-07 09:05:11 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can update the TTL on a value.
|
|
|
|
func TestStoreUpdateValueTTL(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2014-10-17 02:19:00 +04:00
|
|
|
s.clock = fc
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
_, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
2013-11-10 05:55:54 +04:00
|
|
|
e, _ := s.Get("/foo", false, false)
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err = s.Get("/foo", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
|
|
|
|
}
|
2013-09-09 05:14:31 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can update the TTL on a directory.
|
|
|
|
func TestStoreUpdateDirTTL(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2014-10-17 02:19:00 +04:00
|
|
|
s.clock = fc
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 3
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
e, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
2014-04-18 02:05:14 +04:00
|
|
|
assert.Equal(t, e.Node.Dir, true, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-04-18 02:05:14 +04:00
|
|
|
e, _ = s.Get("/foo/bar", false, false)
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-28 20:42:54 +04:00
|
|
|
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err = s.Get("/foo/bar", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
|
|
|
|
}
|
2013-09-09 05:14:31 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can delete a value.
|
|
|
|
func TestStoreDeleteValue(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 02:48:32 +04:00
|
|
|
e, err := s.Delete("/foo", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "delete", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
// check prevNode
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can delete a directory if recursive is specified.
|
|
|
|
func TestStoreDeleteDiretory(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 05:46:52 +04:00
|
|
|
// create directory /foo
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
// delete /foo with dir = true and recursive = false
|
2013-12-09 20:33:55 +04:00
|
|
|
// this should succeed, since the directory is empty
|
2013-12-06 05:46:52 +04:00
|
|
|
e, err := s.Delete("/foo", true, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "delete", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
// check prevNode
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
|
|
|
assert.Equal(t, e.PrevNode.Dir, true, "")
|
2013-12-06 05:46:52 +04:00
|
|
|
|
|
|
|
// create directory /foo and directory /foo/bar
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/bar", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
// delete /foo with dir = true and recursive = false
|
|
|
|
// this should fail, since the directory is not empty
|
|
|
|
_, err = s.Delete("/foo", true, false)
|
|
|
|
assert.NotNil(t, err, "")
|
|
|
|
|
|
|
|
// delete /foo with dir=false and recursive = true
|
2013-12-09 20:33:55 +04:00
|
|
|
// this should succeed, since recursive implies dir=true
|
2013-12-06 05:46:52 +04:00
|
|
|
// and recursively delete should be able to delete all
|
|
|
|
// items under the given directory
|
|
|
|
e, err = s.Delete("/foo", false, true)
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
assert.Equal(t, e.Action, "delete", "")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store cannot delete a directory if both of recursive
|
|
|
|
// and dir are not specified.
|
|
|
|
func TestStoreDeleteDiretoryFailsIfNonRecursiveAndDir(t *testing.T) {
|
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-06 05:46:52 +04:00
|
|
|
e, _err := s.Delete("/foo", false, false)
|
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
|
|
|
|
assert.Equal(t, err.Message, "Not a file", "")
|
|
|
|
assert.Nil(t, e, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-09 07:13:28 +04:00
|
|
|
|
2013-12-02 03:16:32 +04:00
|
|
|
func TestRootRdOnly(t *testing.T) {
|
2015-02-05 03:47:08 +03:00
|
|
|
s := newStore("/0")
|
2013-12-02 03:16:32 +04:00
|
|
|
|
2015-02-05 03:47:08 +03:00
|
|
|
for _, tt := range []string{"/", "/0"} {
|
2016-01-07 23:20:44 +03:00
|
|
|
_, err := s.Set(tt, true, "", TTLOptionSet{ExpireTime: Permanent})
|
2015-02-05 03:47:08 +03:00
|
|
|
assert.NotNil(t, err, "")
|
2013-12-02 03:16:32 +04:00
|
|
|
|
2015-02-05 03:47:08 +03:00
|
|
|
_, err = s.Delete(tt, true, true)
|
|
|
|
assert.NotNil(t, err, "")
|
2013-12-02 03:16:32 +04:00
|
|
|
|
2016-01-07 23:20:44 +03:00
|
|
|
_, err = s.Create(tt, true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2015-02-05 03:47:08 +03:00
|
|
|
assert.NotNil(t, err, "")
|
2013-12-02 03:16:32 +04:00
|
|
|
|
2016-01-07 23:20:44 +03:00
|
|
|
_, err = s.Update(tt, "", TTLOptionSet{ExpireTime: Permanent})
|
2015-02-05 03:47:08 +03:00
|
|
|
assert.NotNil(t, err, "")
|
2013-12-02 03:16:32 +04:00
|
|
|
|
2016-01-07 23:20:44 +03:00
|
|
|
_, err = s.CompareAndSwap(tt, "", 0, "", TTLOptionSet{ExpireTime: Permanent})
|
2015-02-05 03:47:08 +03:00
|
|
|
assert.NotNil(t, err, "")
|
|
|
|
}
|
2013-12-19 18:19:49 +04:00
|
|
|
}
|
|
|
|
|
2013-12-02 00:28:14 +04:00
|
|
|
func TestStoreCompareAndDeletePrevValue(t *testing.T) {
|
2013-12-19 18:19:49 +04:00
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-20 01:10:22 +04:00
|
|
|
e, err := s.CompareAndDelete("/foo", "bar", 0)
|
2013-12-19 18:19:49 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-19 18:19:49 +04:00
|
|
|
assert.Equal(t, e.Action, "compareAndDelete", "")
|
2013-12-20 01:10:22 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2014-01-01 15:50:07 +04:00
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// check prevNode
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
|
|
|
|
assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
|
2013-12-02 00:28:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) {
|
2013-12-19 18:19:49 +04:00
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-20 01:10:22 +04:00
|
|
|
e, _err := s.CompareAndDelete("/foo", "baz", 0)
|
2013-12-19 18:19:49 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
|
2013-12-20 01:10:22 +04:00
|
|
|
assert.Equal(t, err.Message, "Compare failed", "")
|
2013-12-19 18:19:49 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-12-02 00:28:14 +04:00
|
|
|
}
|
2013-12-02 03:16:32 +04:00
|
|
|
|
2013-12-02 00:28:14 +04:00
|
|
|
func TestStoreCompareAndDeletePrevIndex(t *testing.T) {
|
2013-12-19 18:19:49 +04:00
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-20 01:10:22 +04:00
|
|
|
e, err := s.CompareAndDelete("/foo", "", 1)
|
2013-12-19 18:19:49 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-12-19 18:19:49 +04:00
|
|
|
assert.Equal(t, e.Action, "compareAndDelete", "")
|
2016-02-01 08:42:39 +03:00
|
|
|
// check prevNode
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
|
|
|
|
assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
|
2013-12-02 00:28:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) {
|
2013-12-19 18:19:49 +04:00
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-20 01:10:22 +04:00
|
|
|
e, _err := s.CompareAndDelete("/foo", "", 100)
|
|
|
|
assert.NotNil(t, _err, "")
|
2013-12-19 18:19:49 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
|
2013-12-20 01:10:22 +04:00
|
|
|
assert.Equal(t, err.Message, "Compare failed", "")
|
2013-12-19 18:19:49 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-12-02 00:38:09 +04:00
|
|
|
}
|
|
|
|
|
2013-12-20 01:10:22 +04:00
|
|
|
// Ensure that the store cannot delete a directory.
|
|
|
|
func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) {
|
2013-12-19 18:19:49 +04:00
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-12-20 01:10:22 +04:00
|
|
|
_, _err := s.CompareAndDelete("/foo", "", 0)
|
|
|
|
assert.NotNil(t, _err, "")
|
2013-12-19 18:19:49 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
|
2013-12-02 03:16:32 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can conditionally update a key if it has a previous value.
|
|
|
|
func TestStoreCompareAndSwapPrevValue(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "compareAndSwap", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2016-02-01 08:42:39 +03:00
|
|
|
// check prevNode
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
|
|
|
|
assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
|
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2013-09-09 07:13:28 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store cannot conditionally update a key if it has the wrong previous value.
|
|
|
|
func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
|
2013-12-06 03:16:01 +04:00
|
|
|
assert.Equal(t, err.Message, "Compare failed", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, e, "")
|
2013-11-10 05:55:54 +04:00
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can conditionally update a key if it has a previous index.
|
|
|
|
func TestStoreCompareAndSwapPrevIndex(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
e, err := s.CompareAndSwap("/foo", "", 1, "baz", TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "compareAndSwap", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
// check prevNode
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.NotNil(t, e.PrevNode, "")
|
|
|
|
assert.Equal(t, e.PrevNode.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
2014-01-01 15:50:07 +04:00
|
|
|
assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
|
|
|
|
assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
|
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-09 07:13:28 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store cannot conditionally update a key if it has the wrong previous index.
|
|
|
|
func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
e, _err := s.CompareAndSwap("/foo", "", 100, "baz", TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
err := _err.(*etcdErr.Error)
|
|
|
|
assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
|
2013-12-06 03:16:01 +04:00
|
|
|
assert.Equal(t, err.Message, "Compare failed", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, e, "")
|
2013-11-10 05:55:54 +04:00
|
|
|
e, _ = s.Get("/foo", false, false)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for key creation.
|
|
|
|
func TestStoreWatchCreate(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 0
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", false, false, 0)
|
2014-09-09 03:56:10 +04:00
|
|
|
c := w.EventChan()
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-26 22:34:11 +04:00
|
|
|
eidx = 1
|
2013-10-20 23:39:29 +04:00
|
|
|
e := nbselect(c)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
e = nbselect(c)
|
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for recursive key creation.
|
|
|
|
func TestStoreWatchRecursiveCreate(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 0
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo/bar", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for key updates.
|
|
|
|
func TestStoreWatchUpdate(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", false, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "update", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for recursive key updates.
|
|
|
|
func TestStoreWatchRecursiveUpdate(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Update("/foo/bar", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "update", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo/bar", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-10-01 08:35:44 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for key deletions.
|
|
|
|
func TestStoreWatchDelete(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", false, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 2
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Delete("/foo", false, false)
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "delete", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-10-01 08:35:44 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for recursive key deletions.
|
|
|
|
func TestStoreWatchRecursiveDelete(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 2
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Delete("/foo/bar", false, false)
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "delete", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo/bar", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-09 07:13:28 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for CAS updates.
|
|
|
|
func TestStoreWatchCompareAndSwap(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", false, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.CompareAndSwap("/foo", "bar", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "compareAndSwap", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for recursive CAS updates.
|
|
|
|
func TestStoreWatchRecursiveCompareAndSwap(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 1
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.CompareAndSwap("/foo/bar", "baz", 0, "bat", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "compareAndSwap", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo/bar", "")
|
2013-10-20 23:39:29 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can watch for key expiration.
|
|
|
|
func TestStoreWatchExpire(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2014-10-17 02:19:00 +04:00
|
|
|
s.clock = fc
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2014-09-26 22:34:11 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
|
|
|
s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
2013-11-07 09:19:37 +04:00
|
|
|
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/", true, false, 0)
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
2014-09-09 03:56:10 +04:00
|
|
|
c := w.EventChan()
|
2013-10-20 23:39:29 +04:00
|
|
|
e := nbselect(c)
|
|
|
|
assert.Nil(t, e, "")
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2014-09-26 22:34:11 +04:00
|
|
|
eidx = 3
|
2013-10-20 23:39:29 +04:00
|
|
|
e = nbselect(c)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, e.Action, "expire", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ = s.Watch("/", true, false, 4)
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 4
|
2014-09-26 22:34:11 +04:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
e = nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-11-07 09:19:37 +04:00
|
|
|
assert.Equal(t, e.Action, "expire", "")
|
2013-11-28 08:04:52 +04:00
|
|
|
assert.Equal(t, e.Node.Key, "/foofoo", "")
|
2013-09-09 07:13:28 +04:00
|
|
|
}
|
|
|
|
|
2016-01-07 23:20:44 +03:00
|
|
|
// Ensure that the store can watch for key expiration when refreshing.
|
|
|
|
func TestStoreWatchExpireRefresh(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
fc := newFakeClock()
|
|
|
|
s.clock = fc
|
|
|
|
|
|
|
|
var eidx uint64 = 2
|
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1200 * time.Millisecond), Refresh: true})
|
|
|
|
|
|
|
|
// Make sure we set watch updates when Refresh is true for newly created keys
|
|
|
|
w, _ := s.Watch("/", true, false, 0)
|
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
c := w.EventChan()
|
|
|
|
e := nbselect(c)
|
|
|
|
assert.Nil(t, e, "")
|
|
|
|
fc.Advance(600 * time.Millisecond)
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
|
|
|
eidx = 3
|
|
|
|
e = nbselect(c)
|
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
|
|
|
assert.Equal(t, e.Action, "expire", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
|
|
|
|
s.Update("/foofoo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
w, _ = s.Watch("/", true, false, 4)
|
|
|
|
fc.Advance(700 * time.Millisecond)
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2016-12-05 15:04:18 +03:00
|
|
|
eidx = 5 // We should skip 4 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true
|
2016-01-07 23:20:44 +03:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx-1, "")
|
|
|
|
e = nbselect(w.EventChan())
|
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
|
|
|
assert.Equal(t, e.Action, "expire", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foofoo", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store can watch for key expiration when refreshing with an empty value.
|
|
|
|
func TestStoreWatchExpireEmptyRefresh(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
fc := newFakeClock()
|
|
|
|
s.clock = fc
|
|
|
|
|
|
|
|
var eidx uint64 = 1
|
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
// Should be no-op
|
|
|
|
fc.Advance(200 * time.Millisecond)
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
|
|
|
|
|
|
|
s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
w, _ := s.Watch("/", true, false, 2)
|
|
|
|
fc.Advance(700 * time.Millisecond)
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2016-12-05 15:04:18 +03:00
|
|
|
eidx = 3 // We should skip 2 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true
|
2016-01-07 23:20:44 +03:00
|
|
|
assert.Equal(t, w.StartIndex(), eidx-1, "")
|
|
|
|
e := nbselect(w.EventChan())
|
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
|
|
|
assert.Equal(t, e.Action, "expire", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
|
|
|
}
|
|
|
|
|
2016-12-05 15:04:18 +03:00
|
|
|
// Update TTL of a key (set TTLOptionSet.Refresh to false) and send notification
|
|
|
|
func TestStoreWatchNoRefresh(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
fc := newFakeClock()
|
|
|
|
s.clock = fc
|
|
|
|
|
|
|
|
var eidx uint64 = 1
|
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
// Should be no-op
|
|
|
|
fc.Advance(200 * time.Millisecond)
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
|
|
|
|
|
|
|
// Update key's TTL with setting `TTLOptionSet.Refresh` to false will cause an update event
|
|
|
|
s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: false})
|
|
|
|
w, _ := s.Watch("/", true, false, 2)
|
|
|
|
fc.Advance(700 * time.Millisecond)
|
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
|
|
|
eidx = 2
|
|
|
|
assert.Equal(t, w.StartIndex(), eidx, "")
|
|
|
|
e := nbselect(w.EventChan())
|
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
|
|
|
assert.Equal(t, e.Action, "update", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
|
|
|
assert.Equal(t, *e.PrevNode.Value, "bar", "")
|
|
|
|
}
|
|
|
|
|
2016-01-07 23:20:44 +03:00
|
|
|
// Ensure that the store can update the TTL on a value with refresh.
|
|
|
|
func TestStoreRefresh(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
fc := newFakeClock()
|
|
|
|
s.clock = fc
|
|
|
|
|
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
|
|
|
s.Create("/bar", true, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
|
|
|
_, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
|
|
|
|
_, err = s.Set("/foo", false, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
|
|
|
|
_, err = s.Update("/bar", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
|
|
|
|
_, err = s.CompareAndSwap("/foo", "bar", 0, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
|
|
|
|
assert.Nil(t, err, "")
|
|
|
|
}
|
|
|
|
|
2014-01-09 11:28:33 +04:00
|
|
|
// Ensure that the store can watch in streaming mode.
|
|
|
|
func TestStoreWatchStream(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2014-01-09 11:28:33 +04:00
|
|
|
w, _ := s.Watch("/foo", false, true, 0)
|
|
|
|
// first modification
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-01-09 11:28:33 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-09 11:28:33 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
// second modification
|
2014-09-25 02:04:32 +04:00
|
|
|
eidx = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-01-09 11:28:33 +04:00
|
|
|
assert.Equal(t, e.Action, "update", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foo", "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-09 11:28:33 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can recover from a previously saved state.
|
|
|
|
func TestStoreRecover(t *testing.T) {
|
|
|
|
s := newStore()
|
2015-02-06 09:26:52 +03:00
|
|
|
var eidx uint64 = 4
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Update("/foo/x", "barbar", TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2013-10-20 23:39:29 +04:00
|
|
|
b, err := s.Save()
|
2013-09-09 07:13:28 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
s2 := newStore()
|
|
|
|
s2.Recovery(b)
|
2013-09-09 07:13:28 +04:00
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err := s.Get("/foo/x", false, false)
|
2015-02-06 09:26:52 +03:00
|
|
|
assert.Equal(t, e.Node.CreatedIndex, uint64(2), "")
|
|
|
|
assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2015-02-06 09:26:52 +03:00
|
|
|
assert.Equal(t, *e.Node.Value, "barbar", "")
|
2013-10-01 08:35:44 +04:00
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err = s.Get("/foo/y", false, false)
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "baz", "")
|
2013-09-09 05:14:31 +04:00
|
|
|
}
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that the store can recover from a previously saved state that includes an expiring key.
|
|
|
|
func TestStoreRecoverWithExpiration(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
s.clock = newFakeClock()
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 4
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
|
|
|
s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: fc.Now().Add(5 * time.Millisecond)})
|
2013-10-20 23:39:29 +04:00
|
|
|
b, err := s.Save()
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
s2 := newStore()
|
2014-10-17 02:19:00 +04:00
|
|
|
s2.clock = fc
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
s2.Recovery(b)
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2013-11-06 09:47:25 +04:00
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err := s.Get("/foo/x", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Nil(t, err, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-02-04 05:12:36 +04:00
|
|
|
assert.Equal(t, *e.Node.Value, "bar", "")
|
2013-09-14 01:10:40 +04:00
|
|
|
|
2013-11-10 05:55:54 +04:00
|
|
|
e, err = s.Get("/foo/y", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.NotNil(t, err, "")
|
|
|
|
assert.Nil(t, e, "")
|
2013-09-14 01:10:40 +04:00
|
|
|
}
|
|
|
|
|
2014-01-22 18:29:53 +04:00
|
|
|
// Ensure that the store can watch for hidden keys as long as it's an exact path match.
|
|
|
|
func TestStoreWatchCreateWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2014-01-22 18:29:53 +04:00
|
|
|
w, _ := s.Watch("/_foo", false, false, 0)
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/_foo", "")
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store doesn't see hidden key creates without an exact path match in recursive mode.
|
|
|
|
func TestStoreWatchRecursiveCreateWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/_bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
w, _ = s.Watch("/foo", true, false, 0)
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/_baz", true, "", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/_baz/quux", false, "quux", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store doesn't see hidden key updates.
|
|
|
|
func TestStoreWatchUpdateWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-22 18:29:53 +04:00
|
|
|
w, _ := s.Watch("/_foo", false, false, 0)
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Update("/_foo", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Equal(t, e.Action, "update", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/_foo", "")
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store doesn't see hidden key updates without an exact path match in recursive mode.
|
|
|
|
func TestStoreWatchRecursiveUpdateWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/_bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-22 18:29:53 +04:00
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Update("/foo/_bar", "baz", TTLOptionSet{ExpireTime: Permanent})
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store can watch for key deletions.
|
|
|
|
func TestStoreWatchDeleteWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 2
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-22 18:29:53 +04:00
|
|
|
w, _ := s.Watch("/_foo", false, false, 0)
|
|
|
|
s.Delete("/_foo", false, false)
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Equal(t, e.Action, "delete", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/_foo", "")
|
2014-09-09 03:56:10 +04:00
|
|
|
e = nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store doesn't see hidden key deletes without an exact path match in recursive mode.
|
|
|
|
func TestStoreWatchRecursiveDeleteWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/foo/_bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-01-22 18:29:53 +04:00
|
|
|
w, _ := s.Watch("/foo", true, false, 0)
|
|
|
|
s.Delete("/foo/_bar", false, false)
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-01-22 18:29:53 +04:00
|
|
|
assert.Nil(t, e, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the store doesn't see expirations of hidden keys.
|
|
|
|
func TestStoreWatchExpireWithHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2014-10-17 02:19:00 +04:00
|
|
|
s.clock = fc
|
2014-01-22 18:29:53 +04:00
|
|
|
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
|
|
|
|
s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1000 * time.Millisecond)})
|
2014-01-22 18:29:53 +04:00
|
|
|
|
|
|
|
w, _ := s.Watch("/", true, false, 0)
|
2014-09-09 03:56:10 +04:00
|
|
|
c := w.EventChan()
|
2014-01-22 18:29:53 +04:00
|
|
|
e := nbselect(c)
|
|
|
|
assert.Nil(t, e, "")
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2014-01-22 18:29:53 +04:00
|
|
|
e = nbselect(c)
|
|
|
|
assert.Nil(t, e, "")
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2014-01-22 18:29:53 +04:00
|
|
|
e = nbselect(c)
|
|
|
|
assert.Equal(t, e.Action, "expire", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/foofoo", "")
|
|
|
|
}
|
|
|
|
|
2014-02-06 07:34:41 +04:00
|
|
|
// Ensure that the store does see hidden key creates if watching deeper than a hidden key in recursive mode.
|
|
|
|
func TestStoreWatchRecursiveCreateDeeperThanHiddenKey(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-09-25 02:04:32 +04:00
|
|
|
var eidx uint64 = 1
|
2014-02-06 07:34:41 +04:00
|
|
|
w, _ := s.Watch("/_foo/bar", true, false, 0)
|
2016-01-07 23:20:44 +03:00
|
|
|
s.Create("/_foo/bar/baz", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
|
2014-02-06 07:34:41 +04:00
|
|
|
|
2014-09-09 03:56:10 +04:00
|
|
|
e := nbselect(w.EventChan())
|
2014-02-06 07:34:41 +04:00
|
|
|
assert.NotNil(t, e, "")
|
2014-09-25 02:04:32 +04:00
|
|
|
assert.Equal(t, e.EtcdIndex, eidx, "")
|
2014-02-06 07:34:41 +04:00
|
|
|
assert.Equal(t, e.Action, "create", "")
|
|
|
|
assert.Equal(t, e.Node.Key, "/_foo/bar/baz", "")
|
|
|
|
}
|
|
|
|
|
2014-02-15 04:12:53 +04:00
|
|
|
// Ensure that slow consumers are handled properly.
|
|
|
|
//
|
2016-12-05 15:04:18 +03:00
|
|
|
// Since Watcher.EventChan() has a buffer of size 100 we can only queue 100
|
2014-02-15 04:12:53 +04:00
|
|
|
// event per watcher. If the consumer cannot consume the event on time and
|
|
|
|
// another event arrives, the channel is closed and event is discarded.
|
|
|
|
// This test ensures that after closing the channel, the store can continue
|
|
|
|
// to operate correctly.
|
|
|
|
func TestStoreWatchSlowConsumer(t *testing.T) {
|
|
|
|
s := newStore()
|
2016-12-05 15:04:18 +03:00
|
|
|
s.Watch("/foo", true, true, 0) // stream must be true
|
|
|
|
// Fill watch channel with 100 events
|
|
|
|
for i := 1; i <= 100; i++ {
|
|
|
|
s.Set("/foo", false, string(i), TTLOptionSet{ExpireTime: Permanent}) // ok
|
|
|
|
}
|
|
|
|
assert.Equal(t, s.WatcherHub.count, int64(1), "")
|
|
|
|
s.Set("/foo", false, "101", TTLOptionSet{ExpireTime: Permanent}) // ok
|
|
|
|
// remove watcher
|
|
|
|
assert.Equal(t, s.WatcherHub.count, int64(0), "")
|
|
|
|
s.Set("/foo", false, "102", TTLOptionSet{ExpireTime: Permanent}) // must not panic
|
2014-02-15 04:12:53 +04:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Performs a non-blocking select on an event channel.
|
|
|
|
func nbselect(c <-chan *Event) *Event {
|
2013-09-14 01:10:40 +04:00
|
|
|
select {
|
|
|
|
case e := <-c:
|
|
|
|
return e
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 08:32:48 +04:00
|
|
|
|
|
|
|
// newFakeClock creates a new FakeClock that has been advanced to at least minExpireTime
|
|
|
|
func newFakeClock() clockwork.FakeClock {
|
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
|
for minExpireTime.After(fc.Now()) {
|
|
|
|
fc.Advance((0x1 << 62) * time.Nanosecond)
|
|
|
|
}
|
|
|
|
return fc
|
|
|
|
}
|