mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #8 from evan-gu/fileSystem
add testcase, fix update dir ttl bug
This commit is contained in:
commit
bd7c09e9c0
@ -159,6 +159,7 @@ func (fs *FileSystem) Update(nodePath string, value string, expireTime time.Time
|
|||||||
}
|
}
|
||||||
|
|
||||||
if expireTime != Permanent {
|
if expireTime != Permanent {
|
||||||
|
n.ExpireTime = expireTime
|
||||||
go n.Expire()
|
go n.Expire()
|
||||||
e.Expiration = &n.ExpireTime
|
e.Expiration = &n.ExpireTime
|
||||||
e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
|
e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
|
||||||
|
@ -55,7 +55,7 @@ func TestUpdateFile(t *testing.T) {
|
|||||||
_, err := fs.Create("/foo/bar", "bar", Permanent, 1, 1)
|
_, err := fs.Create("/foo/bar", "bar", Permanent, 1, 1)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cannot update %s=bar [%s]", "/foo/bar", err.Error())
|
t.Fatalf("cannot create %s=bar [%s]", "/foo/bar", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = fs.Update("/foo/bar", "barbar", Permanent, 2, 1)
|
_, err = fs.Update("/foo/bar", "barbar", Permanent, 2, 1)
|
||||||
@ -73,6 +73,80 @@ func TestUpdateFile(t *testing.T) {
|
|||||||
if e.Value != "barbar" {
|
if e.Value != "barbar" {
|
||||||
t.Fatalf("expect value of %s is barbar [%s]", "/foo/bar", e.Value)
|
t.Fatalf("expect value of %s is barbar [%s]", "/foo/bar", e.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create a directory, update its ttl, to see if it will be deleted
|
||||||
|
|
||||||
|
_, err = fs.Create("/foo/foo", "", Permanent, 3, 1)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cannot create dir [%s] [%s]", "/foo/foo", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fs.Create("/foo/foo/foo1", "bar1", Permanent, 4, 1)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("cannot create [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fs.Create("/foo/foo/foo2", "", Permanent, 5, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("cannot create [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fs.Create("/foo/foo/foo2/boo", "boo1", Permanent, 6, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("cannot create [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
expire := time.Now().Add(time.Second * 2)
|
||||||
|
_, err = fs.Update("/foo/foo", "", expire, 7, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cannot update dir [%s] [%s]", "/foo/foo", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// sleep 50ms, it should still reach the node
|
||||||
|
time.Sleep(time.Microsecond * 50)
|
||||||
|
e, err = fs.Get("/foo/foo", true, 7, 1)
|
||||||
|
|
||||||
|
if err != nil || e.Key != "/foo/foo" {
|
||||||
|
t.Fatalf("cannot get dir before expiration [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if e.KVPairs[0].Key != "/foo/foo/foo1" || e.KVPairs[0].Value != "bar1" {
|
||||||
|
t.Fatalf("cannot get sub node before expiration [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if e.KVPairs[1].Key != "/foo/foo/foo2" || e.KVPairs[1].Dir != true {
|
||||||
|
t.Fatalf("cannot get sub dir before expiration [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
/*if e.KVPairs[2].Key != "/foo/foo/foo2/boo" || e.KVPairs[2].Value != "boo1" {
|
||||||
|
t.Fatalf("cannot get sub node of sub dir before expiration [%s]", err.Error())
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// wait for expiration
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
e, err = fs.Get("/foo/foo", true, 7, 1)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("still can get dir after expiration [%s]")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fs.Get("/foo/foo/foo1", true, 7, 1)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("still can get sub node after expiration [%s]")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fs.Get("/foo/foo/foo2", true, 7, 1)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("still can get sub dir after expiration [%s]")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fs.Get("/foo/foo/foo2/boo", true, 7, 1)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("still can get sub node of sub dir after expiration [%s]", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListDirectory(t *testing.T) {
|
func TestListDirectory(t *testing.T) {
|
||||||
@ -181,23 +255,29 @@ func TestExpire(t *testing.T) {
|
|||||||
t.Fatalf("can get the node after expiration time")
|
t.Fatalf("can get the node after expiration time")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test if we can reach the node before expiration
|
||||||
|
expire = time.Now().Add(time.Second)
|
||||||
fs.Create("/foo", "bar", expire, 1, 1)
|
fs.Create("/foo", "bar", expire, 1, 1)
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 50)
|
time.Sleep(time.Millisecond * 50)
|
||||||
_, err = fs.InternalGet("/foo", 1, 1)
|
_, err = fs.InternalGet("/foo", 1, 1)
|
||||||
|
|
||||||
if err == nil {
|
if err != nil {
|
||||||
t.Fatalf("can get the node after expiration time")
|
t.Fatalf("cannot get the node before expiration", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
expire = time.Now().Add(time.Second)
|
expire = time.Now().Add(time.Second)
|
||||||
|
|
||||||
fs.Create("/foo", "bar", expire, 1, 1)
|
fs.Create("/foo", "bar", expire, 1, 1)
|
||||||
fs.Delete("/foo", false, 1, 1)
|
_, err = fs.Delete("/foo", false, 1, 1)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cannot delete the node before expiration", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTestAndSet(t *testing.T) {
|
func TestTestAndSet(t *testing.T) { // TODO prevValue == nil ?
|
||||||
fs := New()
|
fs := New()
|
||||||
fs.Create("/foo", "bar", Permanent, 1, 1)
|
fs.Create("/foo", "bar", Permanent, 1, 1)
|
||||||
|
|
||||||
@ -228,27 +308,69 @@ func TestTestAndSet(t *testing.T) {
|
|||||||
if e.PrevValue != "car" || e.Value != "bar" {
|
if e.PrevValue != "car" || e.Value != "bar" {
|
||||||
t.Fatalf("[%v/%v] [%v/%v]", e.PrevValue, "car", e.Value, "bar")
|
t.Fatalf("[%v/%v] [%v/%v]", e.PrevValue, "car", e.Value, "bar")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//e, err = fs.TestAndSet("/foo", )
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWatchRemove(t *testing.T) {
|
func TestWatch(t *testing.T) {
|
||||||
fs := New()
|
fs := New()
|
||||||
fs.Create("/foo/foo/foo", "bar", Permanent, 1, 1)
|
|
||||||
|
|
||||||
// watch at a deeper path
|
// watch at a deeper path
|
||||||
c, _ := fs.WatcherHub.watch("/foo/foo/foo", false, 0)
|
c, _ := fs.WatcherHub.watch("/foo/foo/foo", false, 0)
|
||||||
fs.Delete("/foo", true, 2, 1)
|
fs.Create("/foo/foo/foo", "bar", Permanent, 1, 1)
|
||||||
e := <-c
|
|
||||||
if e.Key != "/foo" {
|
e := nonblockingRetrive(c)
|
||||||
t.Fatal("watch for delete fails")
|
if e.Key != "/foo/foo/foo" {
|
||||||
|
t.Fatal("watch for Create node fails")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, _ = fs.WatcherHub.watch("/foo/foo/foo", false, 0)
|
||||||
|
fs.Update("/foo/foo/foo", "car", Permanent, 2, 1)
|
||||||
|
e = nonblockingRetrive(c)
|
||||||
|
if e.Key != "/foo/foo/foo" {
|
||||||
|
t.Fatal("watch for Update node fails")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, _ = fs.WatcherHub.watch("/foo/foo/foo", false, 0)
|
||||||
|
fs.TestAndSet("/foo/foo/foo", "car", 0, "bar", Permanent, 3, 1)
|
||||||
|
e = nonblockingRetrive(c)
|
||||||
|
if e.Key != "/foo/foo/foo" {
|
||||||
|
t.Fatal("watch for TestAndSet node fails")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, _ = fs.WatcherHub.watch("/foo/foo/foo", false, 0)
|
||||||
|
fs.Delete("/foo", true, 4, 1) //recursively delete
|
||||||
|
e = nonblockingRetrive(c)
|
||||||
|
if e.Key != "/foo" {
|
||||||
|
t.Fatal("watch for Delete node fails")
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.Create("/foo/foo/foo", "bar", Permanent, 3, 1)
|
|
||||||
// watch at a prefix
|
// watch at a prefix
|
||||||
c, _ = fs.WatcherHub.watch("/foo", true, 0)
|
c, _ = fs.WatcherHub.watch("/foo", true, 0)
|
||||||
fs.Delete("/foo/foo/foo", false, 4, 1)
|
fs.Create("/foo/foo/boo", "bar", Permanent, 5, 1)
|
||||||
e = <-c
|
e = nonblockingRetrive(c)
|
||||||
if e.Key != "/foo/foo/foo" {
|
if e.Key != "/foo/foo/boo" {
|
||||||
t.Fatal("watch for delete fails")
|
t.Fatal("watch for Create subdirectory fails")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, _ = fs.WatcherHub.watch("/foo", true, 0)
|
||||||
|
fs.Update("/foo/foo/boo", "foo", Permanent, 6, 1)
|
||||||
|
e = nonblockingRetrive(c)
|
||||||
|
if e.Key != "/foo/foo/boo" {
|
||||||
|
t.Fatal("watch for Update subdirectory fails")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, _ = fs.WatcherHub.watch("/foo", true, 0)
|
||||||
|
fs.TestAndSet("/foo/foo/boo", "foo", 0, "bar", Permanent, 7, 1)
|
||||||
|
e = nonblockingRetrive(c)
|
||||||
|
if e.Key != "/foo/foo/boo" {
|
||||||
|
t.Fatal("watch for TestAndSet subdirectory fails")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, _ = fs.WatcherHub.watch("/foo", true, 0)
|
||||||
|
fs.Delete("/foo/foo/boo", false, 8, 1)
|
||||||
|
e = nonblockingRetrive(c)
|
||||||
|
if e.Key != "/foo/foo/boo" {
|
||||||
|
t.Fatal("watch for Delete subdirectory fails")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -271,3 +393,12 @@ func createAndGet(fs *FileSystem, path string, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nonblockingRetrive(c <-chan *Event) *Event {
|
||||||
|
select {
|
||||||
|
case e := <-c:
|
||||||
|
return e
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWatch(t *testing.T) {
|
func TestWatcher(t *testing.T) {
|
||||||
wh := newWatchHub(100)
|
wh := newWatchHub(100)
|
||||||
c, err := wh.watch("/foo", true, 0)
|
c, err := wh.watch("/foo", true, 0)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user