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"
|
|
|
|
)
|
|
|
|
|
2013-09-09 00:36:24 +04:00
|
|
|
func TestWatcher(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2013-09-29 03:26:19 +04:00
|
|
|
wh := s.WatcherHub
|
2014-09-18 03:20:34 +04:00
|
|
|
w, err := wh.watch("/foo", true, false, 1, 1)
|
2013-09-03 22:30:42 +04:00
|
|
|
if err != nil {
|
2013-10-21 23:37:22 +04:00
|
|
|
t.Fatalf("%v", err)
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
2014-09-09 03:56:10 +04:00
|
|
|
c := w.EventChan()
|
2013-09-03 22:30:42 +04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
t.Fatal("should not receive from channel before send the event")
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2013-11-29 06:34:38 +04:00
|
|
|
e := newEvent(Create, "/foo/bar", 1, 1)
|
2013-09-03 22:30:42 +04:00
|
|
|
|
|
|
|
wh.notify(e)
|
|
|
|
|
|
|
|
re := <-c
|
|
|
|
|
|
|
|
if e != re {
|
|
|
|
t.Fatal("recv != send")
|
|
|
|
}
|
2013-09-07 06:05:11 +04:00
|
|
|
|
2014-09-18 03:20:34 +04:00
|
|
|
w, _ = wh.watch("/foo", false, false, 2, 1)
|
2014-09-09 03:56:10 +04:00
|
|
|
c = w.EventChan()
|
2013-09-07 06:05:11 +04:00
|
|
|
|
2013-11-29 06:34:38 +04:00
|
|
|
e = newEvent(Create, "/foo/bar", 2, 2)
|
2013-09-07 06:05:11 +04:00
|
|
|
|
|
|
|
wh.notify(e)
|
|
|
|
|
|
|
|
select {
|
2013-10-01 06:10:40 +04:00
|
|
|
case re = <-c:
|
|
|
|
t.Fatal("should not receive from channel if not recursive ", re)
|
2013-09-07 06:05:11 +04:00
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2013-11-29 06:34:38 +04:00
|
|
|
e = newEvent(Create, "/foo", 3, 3)
|
2013-09-07 06:05:11 +04:00
|
|
|
|
|
|
|
wh.notify(e)
|
|
|
|
|
|
|
|
re = <-c
|
|
|
|
|
|
|
|
if e != re {
|
|
|
|
t.Fatal("recv != send")
|
|
|
|
}
|
|
|
|
|
2013-12-02 02:35:22 +04:00
|
|
|
// ensure we are doing exact matching rather than prefix matching
|
2014-09-18 03:20:34 +04:00
|
|
|
w, _ = wh.watch("/fo", true, false, 1, 1)
|
2014-09-09 03:56:10 +04:00
|
|
|
c = w.EventChan()
|
2013-12-02 02:35:22 +04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case re = <-c:
|
|
|
|
t.Fatal("should not receive from channel:", re)
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2013-12-03 07:36:38 +04:00
|
|
|
e = newEvent(Create, "/fo/bar", 3, 3)
|
2013-12-02 02:35:22 +04:00
|
|
|
|
|
|
|
wh.notify(e)
|
|
|
|
|
|
|
|
re = <-c
|
|
|
|
|
|
|
|
if e != re {
|
|
|
|
t.Fatal("recv != send")
|
|
|
|
}
|
|
|
|
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|