~nskaggs/+junk/xenial-test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package lifeflag_test

import (
	"github.com/juju/testing"
	"gopkg.in/juju/names.v2"

	"github.com/juju/juju/core/life"
	"github.com/juju/juju/watcher"
	"github.com/juju/juju/worker"
	"github.com/juju/juju/worker/workertest"
)

func newMockFacade(stub *testing.Stub, lifeResults ...life.Value) *mockFacade {
	return &mockFacade{
		stub:        stub,
		lifeResults: lifeResults,
	}
}

type mockFacade struct {
	stub        *testing.Stub
	lifeResults []life.Value
}

func (mock *mockFacade) Life(entity names.Tag) (life.Value, error) {
	mock.stub.AddCall("Life", entity)
	if err := mock.stub.NextErr(); err != nil {
		return "", err
	}
	return mock.nextLife(), nil
}

func (mock *mockFacade) nextLife() life.Value {
	result := mock.lifeResults[0]
	mock.lifeResults = mock.lifeResults[1:]
	return result
}

func (mock *mockFacade) Watch(entity names.Tag) (watcher.NotifyWatcher, error) {
	mock.stub.AddCall("Watch", entity)
	if err := mock.stub.NextErr(); err != nil {
		return nil, err
	}
	const count = 2
	changes := make(chan struct{}, count)
	for i := 0; i < count; i++ {
		changes <- struct{}{}
	}
	return newMockWatcher(changes), nil
}

type mockWatcher struct {
	worker.Worker
	changes chan struct{}
}

func newMockWatcher(changes chan struct{}) *mockWatcher {
	return &mockWatcher{
		Worker:  workertest.NewErrorWorker(nil),
		changes: changes,
	}
}

func (mock *mockWatcher) Changes() watcher.NotifyChannel {
	return mock.changes
}