Browse Source

test add watcher prefix test

Xiang Li 12 years ago
parent
commit
78e382cb6b
2 changed files with 27 additions and 1 deletions
  1. 7 1
      store/event_history.go
  2. 20 0
      store/watcher_test.go

+ 7 - 1
store/event_history.go

@@ -66,7 +66,13 @@ func (eh *EventHistory) scan(key string, recursive bool, index uint64) (*Event,
 		ok := (e.Key == key)
 
 		if recursive {
-			ok = ok || strings.HasPrefix(e.Key, path.Join(key, "/"))
+			// add tailing slash
+			key := path.Clean(key)
+			if key[len(key)-1] != '/' {
+				key = key + "/"
+			}
+
+			ok = ok || strings.HasPrefix(e.Key, key)
 		}
 
 		if ok && index <= e.Index() { // make sure we bypass the smaller one

+ 20 - 0
store/watcher_test.go

@@ -68,4 +68,24 @@ func TestWatcher(t *testing.T) {
 		t.Fatal("recv != send")
 	}
 
+	// ensure we are doing exact matching rather than prefix matching
+	c, _ = wh.watch("/fo", true, 1)
+
+	select {
+	case re = <-c:
+		t.Fatal("should not receive from channel:", re)
+	default:
+		// do nothing
+	}
+
+	e = newEvent(Create, "/fo/bar", 3)
+
+	wh.notify(e)
+
+	re = <-c
+
+	if e != re {
+		t.Fatal("recv != send")
+	}
+
 }