1// Copyright 2015 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package store
16
17import (
18	"testing"
19)
20
21func TestWatcher(t *testing.T) {
22	s := newStore()
23	wh := s.WatcherHub
24	w, err := wh.watch("/foo", true, false, 1, 1)
25	if err != nil {
26		t.Fatalf("%v", err)
27	}
28	c := w.EventChan()
29
30	select {
31	case <-c:
32		t.Fatal("should not receive from channel before send the event")
33	default:
34		// do nothing
35	}
36
37	e := newEvent(Create, "/foo/bar", 1, 1)
38
39	wh.notify(e)
40
41	re := <-c
42
43	if e != re {
44		t.Fatal("recv != send")
45	}
46
47	w, _ = wh.watch("/foo", false, false, 2, 1)
48	c = w.EventChan()
49
50	e = newEvent(Create, "/foo/bar", 2, 2)
51
52	wh.notify(e)
53
54	select {
55	case re = <-c:
56		t.Fatal("should not receive from channel if not recursive ", re)
57	default:
58		// do nothing
59	}
60
61	e = newEvent(Create, "/foo", 3, 3)
62
63	wh.notify(e)
64
65	re = <-c
66
67	if e != re {
68		t.Fatal("recv != send")
69	}
70
71	// ensure we are doing exact matching rather than prefix matching
72	w, _ = wh.watch("/fo", true, false, 1, 1)
73	c = w.EventChan()
74
75	select {
76	case re = <-c:
77		t.Fatal("should not receive from channel:", re)
78	default:
79		// do nothing
80	}
81
82	e = newEvent(Create, "/fo/bar", 3, 3)
83
84	wh.notify(e)
85
86	re = <-c
87
88	if e != re {
89		t.Fatal("recv != send")
90	}
91
92}
93