github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/caasoperator/remotestate/mock_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package remotestate_test
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/charm.v6"
    11  
    12  	"github.com/juju/juju/core/watcher"
    13  )
    14  
    15  func newMockWatcher() *mockWatcher {
    16  	return &mockWatcher{
    17  		stopped: make(chan struct{}),
    18  	}
    19  }
    20  
    21  type mockWatcher struct {
    22  	mu      sync.Mutex
    23  	stopped chan struct{}
    24  }
    25  
    26  func (w *mockWatcher) Kill() {
    27  	w.mu.Lock()
    28  	defer w.mu.Unlock()
    29  	if !w.Stopped() {
    30  		close(w.stopped)
    31  	}
    32  }
    33  
    34  func (w *mockWatcher) Wait() error {
    35  	<-w.stopped
    36  	return nil
    37  }
    38  
    39  func (w *mockWatcher) Stopped() bool {
    40  	select {
    41  	case <-w.stopped:
    42  		return true
    43  	default:
    44  		return false
    45  	}
    46  }
    47  
    48  func newMockNotifyWatcher() *mockNotifyWatcher {
    49  	return &mockNotifyWatcher{
    50  		mockWatcher: newMockWatcher(),
    51  		changes:     make(chan struct{}, 1),
    52  	}
    53  }
    54  
    55  type mockNotifyWatcher struct {
    56  	*mockWatcher
    57  	changes chan struct{}
    58  }
    59  
    60  func (w *mockNotifyWatcher) Changes() watcher.NotifyChannel {
    61  	return w.changes
    62  }
    63  
    64  type mockApplicationWatcher struct {
    65  	watcher *mockNotifyWatcher
    66  }
    67  
    68  func (s *mockApplicationWatcher) Watch(application string) (watcher.NotifyWatcher, error) {
    69  	if application != "gitlab" {
    70  		return nil, errors.NotFoundf(application)
    71  	}
    72  	return s.watcher, nil
    73  }
    74  
    75  type mockCharmGetter struct {
    76  	curl    *charm.URL
    77  	force   bool
    78  	sha256  string
    79  	version int
    80  }
    81  
    82  func (m *mockCharmGetter) Charm(application string) (_ *charm.URL, force bool, sha256 string, vers int, _ error) {
    83  	return m.curl, m.force, m.sha256, m.version, nil
    84  }