diff --git a/file_system/file_system.go b/file_system/file_system.go index 154f2d464..d9e3e20a9 100644 --- a/file_system/file_system.go +++ b/file_system/file_system.go @@ -159,6 +159,7 @@ func (fs *FileSystem) Update(nodePath string, value string, expireTime time.Time } if expireTime != Permanent { + n.ExpireTime = expireTime go n.Expire() e.Expiration = &n.ExpireTime e.TTL = int64(expireTime.Sub(time.Now()) / time.Second) diff --git a/file_system/file_system_test.go b/file_system/file_system_test.go index 16666f614..3956df0cd 100644 --- a/file_system/file_system_test.go +++ b/file_system/file_system_test.go @@ -55,7 +55,7 @@ func TestUpdateFile(t *testing.T) { _, err := fs.Create("/foo/bar", "bar", Permanent, 1, 1) 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) @@ -73,6 +73,84 @@ func TestUpdateFile(t *testing.T) { if e.Value != "barbar" { 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) { @@ -181,23 +259,30 @@ func TestExpire(t *testing.T) { 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) time.Sleep(time.Millisecond * 50) _, err = fs.InternalGet("/foo", 1, 1) - if err == nil { - t.Fatalf("can get the node after expiration time") + if err != nil { + t.Fatalf("cannot get the node before expiration", err.Error()) } expire = time.Now().Add(time.Second) - + 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.Create("/foo", "bar", Permanent, 1, 1) @@ -228,29 +313,73 @@ func TestTestAndSet(t *testing.T) { if 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.Create("/foo/foo/foo", "bar", Permanent, 1, 1) - // watch at a deeper path c, _ := fs.WatcherHub.watch("/foo/foo/foo", false, 0) - fs.Delete("/foo", true, 2, 1) - e := <-c - if e.Key != "/foo" { - t.Fatal("watch for delete fails") + fs.Create("/foo/foo/foo", "bar", Permanent, 1, 1) + + e := nonblockingRetrive(c) + 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") } - fs.Create("/foo/foo/foo", "bar", Permanent, 3, 1) + 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") + } + + // watch at a prefix c, _ = fs.WatcherHub.watch("/foo", true, 0) - fs.Delete("/foo/foo/foo", false, 4, 1) - e = <-c - if e.Key != "/foo/foo/foo" { - t.Fatal("watch for delete fails") + fs.Create("/foo/foo/boo", "bar", Permanent, 5, 1) + e = nonblockingRetrive(c) + if e.Key != "/foo/foo/boo" { + 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") + } + + } func createAndGet(fs *FileSystem, path string, t *testing.T) { @@ -271,3 +400,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 + } +} \ No newline at end of file diff --git a/file_system/watcher_test.go b/file_system/watcher_test.go index ea8eae787..f874163ba 100644 --- a/file_system/watcher_test.go +++ b/file_system/watcher_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestWatch(t *testing.T) { +func TestWatcher(t *testing.T) { wh := newWatchHub(100) c, err := wh.watch("/foo", true, 0)