This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_test.go
69 lines (56 loc) · 1.81 KB
/
task_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTask_Success(t *testing.T) {
task := &Task{Capacity: 2, Timeout: time.Second}
task.AddJob(Job{ID: "test", Name: "echo", Args: []string{"hello,", "world"}})
task.AddJob(Job{ID: "test", Name: "echo", Args: []string{"hello,", "world"}})
r := task.Run()
assert.Equal(t, cap(r), 2)
assert.NoError(t, (<-r).Error)
assert.NoError(t, (<-r).Error)
}
func TestTask_Fails(t *testing.T) {
{
task := &Task{}
task.AddJob(Job{ID: "test", Name: "echo", Args: []string{"hello,", "world"}})
r := task.Run()
assert.Equal(t, cap(r), 1)
assert.Error(t, (<-r).Error)
}
{
task := &Task{Capacity: 1, Timeout: time.Second}
task.AddJob(Job{ID: "test", Name: "curl", Args: []string{"unknown"}})
r := task.Run()
assert.Equal(t, cap(r), 1)
assert.Error(t, (<-r).Error)
}
}
func TestJob(t *testing.T) {
j := Job{ID: "test", Name: "echo", Args: []string{"hello,", "world"}}
assert.Equal(t, "echo#test", fmt.Sprintf("%v", j))
assert.Equal(t, "echo#test [hello, world]", fmt.Sprintf("%+v", j))
assert.Equal(t, "echo#test", j.String())
assert.Equal(t, "echo#test `echo hello, world`", fmt.Sprintf("%q", j))
assert.NoError(t, j.Run(ioutil.Discard, ioutil.Discard))
assert.Equal(t, "echo#test", j.String())
}
func TestResult(t *testing.T) {
buf := bytes.NewBuffer(nil)
r := Result{Job: Job{ID: "test", Name: "echo", Args: []string{"hello,", "world"}}, Stderr: buf, Stdout: buf}
assert.NoError(t, r.Fetch())
}
func TestResults(t *testing.T) {
r := Results{}
r.Append(Result{Job: Job{ID: "3"}})
r.Append(Result{Job: Job{ID: "1"}})
r.Append(Result{Job: Job{ID: "2"}})
assert.Equal(t, 3, r.Len())
assert.Equal(t, Results{Result{Job: Job{ID: "1"}}, Result{Job: Job{ID: "2"}}, Result{Job: Job{ID: "3"}}}, r.Sort())
}