-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
90 lines (80 loc) · 1.89 KB
/
main_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"reflect"
"testing"
"time"
)
type BrokenClock struct {
now time.Time
}
func (c *BrokenClock) Now() time.Time {
return c.now
}
func TestApp_Enqueue(t *testing.T) {
clock := &BrokenClock{now: time.Now()}
type args struct {
cmd *Command
}
tests := []struct {
name string
args args
want *EnqueuedCommand
wantErr bool
}{
{
name: "pushes command to queue",
args: args{&Command{Command: "echo", Args: []string{"hello"}, Env: []string{"FOO=BAR"}, WorkingDirectory: "/cwd"}},
want: &EnqueuedCommand{
Command: Command{Command: "echo", Args: []string{"hello"}, Env: []string{"FOO=BAR"}, WorkingDirectory: "/cwd"},
EnqueuedAt: clock.now,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var cmdQueue = make(chan *Command)
a := &App{
cmdQueue: cmdQueue,
clock: clock,
}
got, err := a.Enqueue(tt.args.cmd)
if (err != nil) != tt.wantErr {
t.Errorf("Enqueue() error = %v, wantErr %v", err, tt.wantErr)
return
}
select {
case cmd := <-cmdQueue:
if !reflect.DeepEqual(cmd, tt.args.cmd) {
t.Errorf("Enqueue() got = %v, want %v", cmd, tt.args.cmd)
}
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Enqueue() got = %v, want %v", got, tt.want)
}
})
}
}
type fakeExecutor struct {
executed bool
}
func (e *fakeExecutor) Execute(cmd *Command, killCh <-chan bool) {
e.executed = true
}
func TestApp_processQueue(t *testing.T) {
t.Run("queue executes a command then idles", func(t *testing.T) {
app := &App{
quitCh: make(chan bool),
idleTimeout: 0,
cmdQueue: make(chan *Command),
executor: &fakeExecutor{},
}
killCh := make(chan bool)
go app.processQueue(killCh)
app.cmdQueue <- &Command{}
<-app.quitCh
if !app.executor.(*fakeExecutor).executed {
t.Errorf("processQueue() did not execute command")
}
})
}