-
Notifications
You must be signed in to change notification settings - Fork 86
/
amqp_canceller_test.go
80 lines (64 loc) · 1.64 KB
/
amqp_canceller_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
package worker
import (
"testing"
"time"
gocontext "context"
"github.com/pborman/uuid"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/travis-ci/worker/context"
)
func newTestAMQPCanceller(t *testing.T, cancellationBroadcaster *CancellationBroadcaster) *AMQPCanceller {
amqpConn, _ := setupAMQPConn(t)
uuid := uuid.NewRandom()
ctx := context.FromUUID(gocontext.TODO(), uuid.String())
return NewAMQPCanceller(ctx, amqpConn, cancellationBroadcaster)
}
func TestNewAMQPCanceller(t *testing.T) {
if newTestAMQPCanceller(t, NewCancellationBroadcaster()) == nil {
t.Fail()
}
}
func TestAMQPCanceller_Run(t *testing.T) {
cancellationBroadcaster := NewCancellationBroadcaster()
canceller := newTestAMQPCanceller(t, cancellationBroadcaster)
errChan := make(chan interface{})
go func() {
defer func() {
errChan <- recover()
}()
canceller.Run()
}()
select {
case <-time.After(3 * time.Second):
case err := <-errChan:
if err != nil {
t.Error(err)
}
}
}
func TestAMQPCanceller_processCommand(t *testing.T) {
cancellationBroadcaster := NewCancellationBroadcaster()
canceller := newTestAMQPCanceller(t, cancellationBroadcaster)
err := canceller.processCommand(amqp.Delivery{Body: []byte("{")})
if err == nil {
t.Fatalf("no JSON parsing error returned")
}
errChan := make(chan error)
go func() {
delivery := amqp.Delivery{Body: []byte(`{
"type": "cancel_job",
"job_id": 123,
"source": "tests"
}`),
}
errChan <- canceller.processCommand(delivery)
}()
select {
case <-time.After(3 * time.Second):
t.Fatalf("hit potential deadlock condition")
case err := <-errChan:
if err != nil {
t.Error(err)
}
}
}