Browse Source

fix watcher test and keyword test

Xiang Li 12 years ago
parent
commit
dc2eae8595
3 changed files with 61 additions and 34 deletions
  1. 5 5
      store/keyword_test.go
  2. 0 29
      store/watcher_test.bak
  3. 56 0
      store/watcher_test.go

+ 5 - 5
store/keyword_test.go

@@ -5,30 +5,30 @@ import (
 )
 
 func TestKeywords(t *testing.T) {
-	keyword := CheckKeyword("machines")
+	keyword := CheckKeyword("_etcd")
 	if !keyword {
 		t.Fatal("machines should be keyword")
 	}
 
-	keyword = CheckKeyword("/machines")
+	keyword = CheckKeyword("/_etcd")
 
 	if !keyword {
 		t.Fatal("/machines should be keyword")
 	}
 
-	keyword = CheckKeyword("/machines/")
+	keyword = CheckKeyword("/_etcd/")
 
 	if !keyword {
 		t.Fatal("/machines/ contains keyword prefix")
 	}
 
-	keyword = CheckKeyword("/machines/node1")
+	keyword = CheckKeyword("/_etcd/node1")
 
 	if !keyword {
 		t.Fatal("/machines/* contains keyword prefix")
 	}
 
-	keyword = CheckKeyword("/nokeyword/machines/node1")
+	keyword = CheckKeyword("/nokeyword/_etcd/node1")
 
 	if keyword {
 		t.Fatal("this does not contain keyword prefix")

+ 0 - 29
store/watcher_test.bak

@@ -1,29 +0,0 @@
-package store
-
-import (
-	"fmt"
-	"testing"
-	"time"
-)
-
-func TestWatch(t *testing.T) {
-	// watcher := createWatcher()
-	c := make(chan Response)
-	d := make(chan Response)
-	w.add("/", c)
-	go say(c)
-	w.add("/prefix/", d)
-	go say(d)
-	s.Set("/prefix/foo", "bar", time.Unix(0, 0))
-}
-
-func say(c chan Response) {
-	result := <-c
-
-	if result.Action != -1 {
-		fmt.Println("yes")
-	} else {
-		fmt.Println("no")
-	}
-
-}

+ 56 - 0
store/watcher_test.go

@@ -0,0 +1,56 @@
+package store
+
+import (
+	"fmt"
+	"testing"
+	"time"
+)
+
+func TestWatch(t *testing.T) {
+
+	s := CreateStore(100)
+
+	watchers := make([]*Watcher, 10)
+
+	for i, _ := range watchers {
+
+		// create a new watcher
+		watchers[i] = NewWatcher()
+		// add to the watchers list
+		s.AddWatcher("foo", watchers[i], 0)
+
+	}
+
+	s.Set("/foo/foo", "bar", time.Unix(0, 0), 1)
+
+	for _, watcher := range watchers {
+
+		// wait for the notification for any changing
+		res := <-watcher.C
+
+		if res == nil {
+			t.Fatal("watcher is cleared")
+		}
+	}
+
+	for i, _ := range watchers {
+
+		// create a new watcher
+		watchers[i] = NewWatcher()
+		// add to the watchers list
+		s.AddWatcher("foo/foo/foo", watchers[i], 0)
+
+	}
+
+	s.watcher.stopWatchers()
+
+	for _, watcher := range watchers {
+
+		// wait for the notification for any changing
+		res := <-watcher.C
+
+		if res != nil {
+			t.Fatal("watcher is cleared")
+		}
+	}
+}