github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/workertest/fake_watcher.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package workertest
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/juju/juju/worker"
    10  )
    11  
    12  type NotAWatcher struct {
    13  	changes chan struct{}
    14  	worker.Worker
    15  }
    16  
    17  // NewFakeWatcher returns a fake watcher
    18  func NewFakeWatcher(len, preload int) NotAWatcher {
    19  	if len < preload {
    20  		panic("len must be larger than preload")
    21  	}
    22  	ch := make(chan struct{}, len)
    23  	for i := 0; i < preload; i++ {
    24  		ch <- struct{}{}
    25  	}
    26  	return NotAWatcher{
    27  		changes: ch,
    28  		Worker:  NewErrorWorker(nil),
    29  	}
    30  }
    31  
    32  func (w NotAWatcher) Changes() <-chan struct{} {
    33  	return w.changes
    34  }
    35  
    36  func (w NotAWatcher) Stop() error {
    37  	return nil
    38  }
    39  
    40  func (w NotAWatcher) Err() error {
    41  	return errors.New("An error")
    42  }
    43  
    44  func (w *NotAWatcher) Ping() {
    45  	w.changes <- struct{}{}
    46  }
    47  
    48  func (w *NotAWatcher) Close() {
    49  	close(w.changes)
    50  }