-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
74 lines (68 loc) · 1.61 KB
/
queue_test.go
File metadata and controls
74 lines (68 loc) · 1.61 KB
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
// Copyright (c) 2018 Paul Weiske
//This Code is licensed under the MIT License, see LICENSE file for details.
package gojobqueue
import (
"errors"
"testing"
"time"
)
func TestQueue_AddJob(t *testing.T) {
result := "foo"
res := &result
func1 := func() error {
time.Sleep(2 * time.Second)
*res += "bar"
return nil
}
func2 := func() error {
*res += "baz"
return nil
}
func3 := func() error {
*res += "blub"
return nil
}
queue := make(Queue, 4)
err1 := queue.AddJob(func1, nil)
err2 := queue.AddJob(func2, nil)
queue.StartWorking()
err3 := queue.AddJob(func2, nil)
func3()
time.Sleep(4 * time.Second)
if err1 != nil || err2 != nil || err3 != nil {
t.Errorf("Failed adding jobs to queue with the following errors: %+v \n %+v \n %+v \n", err1, err2, err3)
}
if result != "fooblubbarbazbaz" {
t.Errorf("Failed executing jobs in correct order, expected result to be fooblubbarbaz, got %s", result)
}
}
func TestQueue_Close(t *testing.T) {
queue := make(Queue, 4)
queue.StartWorking()
queue.Close()
err := queue.AddJob(func() error {return nil}, nil)
if err == nil {
t.Errorf("Adding jobs to closed Queue was still possible.")
}
}
func TestQueue_StartWorking(t *testing.T) {
result := "foo"
res := &result
queue := make(Queue, 4)
queue.StartWorking()
queue.AddJob(func() error {
time.Sleep(1 * time.Second)
*res += "bar"
return nil
}, nil)
queue.AddJob(func() error {
return errors.New("blub")
}, func(e error) {
*res += e.Error()
*res += "baz"
})
time.Sleep(2 * time.Second)
if result != "foobarblubbaz" {
t.Errorf("Expected result to be 'foobarblubbaz', got %s", result)
}
}