...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/v2store/watcher_test.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api/v2store

     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  
    15  package v2store
    16  
    17  import "testing"
    18  
    19  func TestWatcher(t *testing.T) {
    20  	s := newStore()
    21  	wh := s.WatcherHub
    22  	w, err := wh.watch("/foo", true, false, 1, 1)
    23  	if err != nil {
    24  		t.Fatalf("%v", err)
    25  	}
    26  	c := w.EventChan()
    27  
    28  	select {
    29  	case <-c:
    30  		t.Fatal("should not receive from channel before send the event")
    31  	default:
    32  		// do nothing
    33  	}
    34  
    35  	e := newEvent(Create, "/foo/bar", 1, 1)
    36  
    37  	wh.notify(e)
    38  
    39  	re := <-c
    40  
    41  	if e != re {
    42  		t.Fatal("recv != send")
    43  	}
    44  
    45  	w, _ = wh.watch("/foo", false, false, 2, 1)
    46  	c = w.EventChan()
    47  
    48  	e = newEvent(Create, "/foo/bar", 2, 2)
    49  
    50  	wh.notify(e)
    51  
    52  	select {
    53  	case re = <-c:
    54  		t.Fatal("should not receive from channel if not recursive ", re)
    55  	default:
    56  		// do nothing
    57  	}
    58  
    59  	e = newEvent(Create, "/foo", 3, 3)
    60  
    61  	wh.notify(e)
    62  
    63  	re = <-c
    64  
    65  	if e != re {
    66  		t.Fatal("recv != send")
    67  	}
    68  
    69  	// ensure we are doing exact matching rather than prefix matching
    70  	w, _ = wh.watch("/fo", true, false, 1, 1)
    71  	c = w.EventChan()
    72  
    73  	select {
    74  	case re = <-c:
    75  		t.Fatal("should not receive from channel:", re)
    76  	default:
    77  		// do nothing
    78  	}
    79  
    80  	e = newEvent(Create, "/fo/bar", 3, 3)
    81  
    82  	wh.notify(e)
    83  
    84  	re = <-c
    85  
    86  	if e != re {
    87  		t.Fatal("recv != send")
    88  	}
    89  
    90  }
    91  

View as plain text