-
Notifications
You must be signed in to change notification settings - Fork 14
/
util_test.go
75 lines (67 loc) · 1.93 KB
/
util_test.go
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
package metafora
import (
"errors"
"log"
"os"
)
func init() {
SetLogger(log.New(os.Stderr, "", log.Lmicroseconds|log.Lshortfile))
}
//TODO Move out into a testutil package for other packages to use. The problem
//is that existing metafora tests would have to be moved to the metafora_test
//package which means no manipulating unexported globals like balance jitter.
type TestCoord struct {
name string
Tasks chan Task // will be returned in order, "" indicates return an error
Commands chan Command
Releases chan Task
Dones chan Task
closed chan bool
}
func NewTestCoord() *TestCoord {
return &TestCoord{
name: "testcoord",
Tasks: make(chan Task, 10),
Commands: make(chan Command, 10),
Releases: make(chan Task, 10),
Dones: make(chan Task, 10),
closed: make(chan bool),
}
}
func (*TestCoord) Init(CoordinatorContext) error { return nil }
func (*TestCoord) Claim(Task) bool { return true }
func (c *TestCoord) Close() { close(c.closed) }
func (c *TestCoord) Release(task Task) { c.Releases <- task }
func (c *TestCoord) Done(task Task) { c.Dones <- task }
func (c *TestCoord) Name() string { return c.name }
// Watch sends tasks from the Tasks channel unless an empty string is sent.
// Then an error is returned.
func (c *TestCoord) Watch(out chan<- Task) error {
var task Task
for {
select {
case task = <-c.Tasks:
Debugf("TestCoord recvd: %s", task)
if task == nil || task.ID() == "" {
return errors.New("test error")
}
case <-c.closed:
return nil
}
select {
case out <- task:
Debugf("TestCoord sent: %s", task)
case <-c.closed:
return nil
}
}
}
// Command returns commands from the Commands channel unless a nil is sent.
// Then an error is returned.
func (c *TestCoord) Command() (Command, error) {
cmd := <-c.Commands
if cmd == nil {
return cmd, errors.New("test error")
}
return cmd, nil
}