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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lifeflag_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/core/life"
    11  	"github.com/juju/juju/watcher"
    12  	"github.com/juju/juju/worker"
    13  	"github.com/juju/juju/worker/workertest"
    14  )
    15  
    16  func newMockFacade(stub *testing.Stub, lifeResults ...life.Value) *mockFacade {
    17  	return &mockFacade{
    18  		stub:        stub,
    19  		lifeResults: lifeResults,
    20  	}
    21  }
    22  
    23  type mockFacade struct {
    24  	stub        *testing.Stub
    25  	lifeResults []life.Value
    26  }
    27  
    28  func (mock *mockFacade) Life(entity names.Tag) (life.Value, error) {
    29  	mock.stub.AddCall("Life", entity)
    30  	if err := mock.stub.NextErr(); err != nil {
    31  		return "", err
    32  	}
    33  	return mock.nextLife(), nil
    34  }
    35  
    36  func (mock *mockFacade) nextLife() life.Value {
    37  	result := mock.lifeResults[0]
    38  	mock.lifeResults = mock.lifeResults[1:]
    39  	return result
    40  }
    41  
    42  func (mock *mockFacade) Watch(entity names.Tag) (watcher.NotifyWatcher, error) {
    43  	mock.stub.AddCall("Watch", entity)
    44  	if err := mock.stub.NextErr(); err != nil {
    45  		return nil, err
    46  	}
    47  	const count = 2
    48  	changes := make(chan struct{}, count)
    49  	for i := 0; i < count; i++ {
    50  		changes <- struct{}{}
    51  	}
    52  	return newMockWatcher(changes), nil
    53  }
    54  
    55  type mockWatcher struct {
    56  	worker.Worker
    57  	changes chan struct{}
    58  }
    59  
    60  func newMockWatcher(changes chan struct{}) *mockWatcher {
    61  	return &mockWatcher{
    62  		Worker:  workertest.NewErrorWorker(nil),
    63  		changes: changes,
    64  	}
    65  }
    66  
    67  func (mock *mockWatcher) Changes() watcher.NotifyChannel {
    68  	return mock.changes
    69  }