File: mock.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,724 kB
  • sloc: makefile: 163
file content (81 lines) | stat: -rw-r--r-- 2,016 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
package mock

import (
	"sync"

	log "github.com/golang/glog"
	"github.com/mesos/mesos-go/api/v0/detector/zoo"
	mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
	"github.com/stretchr/testify/mock"
)

type Client struct {
	mock.Mock
}

func (m *Client) Stopped() (a <-chan struct{}) {
	args := m.Called()
	if x := args.Get(0); x != nil {
		a = x.(<-chan struct{})
	}
	return
}

func (m *Client) Stop() {
	m.Called()
}

func (m *Client) Data(path string) (a []byte, b error) {
	args := m.Called(path)
	if x := args.Get(0); x != nil {
		a = x.([]byte)
	}
	b = args.Error(1)
	return
}

func (m *Client) WatchChildren(path string) (a string, b <-chan []string, c <-chan error) {
	args := m.Called(path)
	a = args.String(0)
	if x := args.Get(1); x != nil {
		b = x.(<-chan []string)
	}
	if x := args.Get(2); x != nil {
		c = x.(<-chan error)
	}
	return
}

// newMockZkClient returns a mocked implementation of ZKInterface that implements expectations
// for Stop() and Stopped(); multiple calls to Stop() are safe.
func NewClient(testZkPath string, initialChildren ...string) (mocked *Client, snaps chan []string, errs chan error) {
	var doneOnce sync.Once
	done := make(chan struct{})

	mocked = &Client{}
	mocked.On("Stop").Return().Run(func(_ mock.Arguments) { doneOnce.Do(func() { close(done) }) })
	mocked.On("Stopped").Return((<-chan struct{})(done))

	if initialChildren != nil {
		errs = make(chan error) // this is purposefully unbuffered (some tests depend on this)
		snaps = make(chan []string, 1)
		snaps <- initialChildren[:]
		mocked.On("WatchChildren", zoo.CurrentPath).Return(
			testZkPath, (<-chan []string)(snaps), (<-chan error)(errs)).Run(
			func(_ mock.Arguments) { log.V(1).Infoln("WatchChildren invoked") })
	}
	return
}

// implements MasterChanged and AllMasters extension
type AllMastersListener struct {
	mock.Mock
}

func (a *AllMastersListener) OnMasterChanged(mi *mesos.MasterInfo) {
	a.Called(mi)
}

func (a *AllMastersListener) UpdatedMasters(mi []*mesos.MasterInfo) {
	a.Called(mi)
}