| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // Copyright 2015 CoreOS, Inc.
- //
- // 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.
- package storage
- import "testing"
- // TestWatcherWatchID tests that each watcher provides unique watchID,
- // and the watched event attaches the correct watchID.
- func TestWatcherWatchID(t *testing.T) {
- s := WatchableKV(newWatchableStore(tmpPath))
- defer cleanup(s, tmpPath)
- w := s.NewWatchStream()
- defer w.Close()
- idm := make(map[int64]struct{})
- for i := 0; i < 10; i++ {
- id, cancel := w.Watch([]byte("foo"), false, 0)
- if _, ok := idm[id]; ok {
- t.Errorf("#%d: id %d exists", i, id)
- }
- idm[id] = struct{}{}
- s.Put([]byte("foo"), []byte("bar"))
- evs := <-w.Chan()
- for j, ev := range evs {
- if ev.WatchID != id {
- t.Errorf("#%d.%d: watch id in event = %d, want %d", i, j, ev.WatchID, id)
- }
- }
- cancel()
- }
- s.Put([]byte("foo2"), []byte("bar"))
- // unsynced watchers
- for i := 10; i < 20; i++ {
- id, cancel := w.Watch([]byte("foo2"), false, 1)
- if _, ok := idm[id]; ok {
- t.Errorf("#%d: id %d exists", i, id)
- }
- idm[id] = struct{}{}
- evs := <-w.Chan()
- for j, ev := range evs {
- if ev.WatchID != id {
- t.Errorf("#%d.%d: watch id in event = %d, want %d", i, j, ev.WatchID, id)
- }
- }
- cancel()
- }
- }
- // TestWatchStreamCancel ensures cancel calls the cancel func of the watcher
- // with given id inside watchStream.
- func TestWatchStreamCancelWatcherByID(t *testing.T) {
- s := WatchableKV(newWatchableStore(tmpPath))
- defer cleanup(s, tmpPath)
- w := s.NewWatchStream()
- defer w.Close()
- id, _ := w.Watch([]byte("foo"), false, 0)
- tests := []struct {
- cancelID int64
- werr error
- }{
- // no error should be returned when cancel the created watcher.
- {id, nil},
- // not exist error should be returned when cancel again.
- {id, ErrWatcherNotExist},
- // not exist error should be returned when cancel a bad id.
- {id + 1, ErrWatcherNotExist},
- }
- for i, tt := range tests {
- gerr := w.Cancel(tt.cancelID)
- if gerr != tt.werr {
- t.Errorf("#%d: err = %v, want %v", i, gerr, tt.werr)
- }
- }
- if l := len(w.(*watchStream).cancels); l != 0 {
- t.Errorf("cancels = %d, want 0", l)
- }
- }
|