github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/store/watcher_test.go (about)

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