-
Notifications
You must be signed in to change notification settings - Fork 24
/
queue_test.go
138 lines (117 loc) · 3.6 KB
/
queue_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package conveyor
import (
"testing"
"golang.org/x/net/context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/remind101/conveyor/builder"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestBuildQueue(t *testing.T) {
q := &buildQueue{
queue: make(chan BuildContext, 1),
}
background := context.Background()
options := builder.BuildOptions{}
err := q.Push(background, options)
assert.NoError(t, err)
ch := make(chan BuildContext)
go q.Subscribe(ch)
req := <-ch
assert.Equal(t, req.BuildOptions, options)
assert.Equal(t, req.Ctx, background)
}
func TestSQSBuildQueue_Push(t *testing.T) {
c := new(mockSQSClient)
q := &SQSBuildQueue{
sqs: c,
}
c.On("SendMessage", &sqs.SendMessageInput{
MessageBody: aws.String(`{"ID":"01234567-89ab-cdef-0123-456789abcdef","Repository":"remind101/acme-inc","Sha":"abcd","Branch":"master","NoCache":false}`),
QueueUrl: aws.String(""),
}).Return(&sqs.SendMessageOutput{}, nil)
background := context.Background()
options := builder.BuildOptions{
ID: fakeUUID,
Repository: "remind101/acme-inc",
Branch: "master",
Sha: "abcd",
}
err := q.Push(background, options)
assert.NoError(t, err)
}
func TestSQSBuildQueue_Subscribe(t *testing.T) {
c := new(mockSQSClient)
q := &SQSBuildQueue{
sqs: c,
}
c.On("ReceiveMessage", &sqs.ReceiveMessageInput{
QueueUrl: aws.String(""),
}).Return(&sqs.ReceiveMessageOutput{
Messages: []*sqs.Message{
{
ReceiptHandle: aws.String("a"),
Body: aws.String(`{"Repository":"remind101/acme-inc-1","Sha":"abcd","Branch":"master","NoCache":false}`),
},
{
ReceiptHandle: aws.String("b"),
Body: aws.String(`{"Repository":"remind101/acme-inc-2","Sha":"abcd","Branch":"master","NoCache":false}`),
},
},
}, nil)
c.On("DeleteMessageBatch", &sqs.DeleteMessageBatchInput{
Entries: []*sqs.DeleteMessageBatchRequestEntry{
{Id: aws.String("0"), ReceiptHandle: aws.String("a")},
{Id: aws.String("1"), ReceiptHandle: aws.String("b")},
},
QueueUrl: aws.String(""),
}).Return(&sqs.DeleteMessageBatchOutput{}, nil)
ch := make(chan BuildContext, 1)
q.Subscribe(ch)
assert.Equal(t, builder.BuildOptions{
Repository: "remind101/acme-inc-1",
Branch: "master",
Sha: "abcd",
}, (<-ch).BuildOptions)
assert.Equal(t, builder.BuildOptions{
Repository: "remind101/acme-inc-2",
Branch: "master",
Sha: "abcd",
}, (<-ch).BuildOptions)
}
func TestSQSBuildQueue_Subscribe_Panic(t *testing.T) {
called := make(chan error)
c := new(mockSQSClient)
q := &SQSBuildQueue{
sqs: c,
ErrHandler: func(err error) {
called <- err
},
}
c.On("ReceiveMessage", &sqs.ReceiveMessageInput{
QueueUrl: aws.String(""),
}).Run(func(args mock.Arguments) {
panic("boom")
})
ch := make(chan BuildContext, 1)
q.Subscribe(ch)
err := <-called
assert.EqualError(t, err, "panic: boom")
}
// mockSQSClient is an implementation of the sqsClient interface for testing.
type mockSQSClient struct {
mock.Mock
}
func (c *mockSQSClient) SendMessage(input *sqs.SendMessageInput) (*sqs.SendMessageOutput, error) {
args := c.Called(input)
return args.Get(0).(*sqs.SendMessageOutput), args.Error(1)
}
func (c *mockSQSClient) ReceiveMessage(input *sqs.ReceiveMessageInput) (*sqs.ReceiveMessageOutput, error) {
args := c.Called(input)
return args.Get(0).(*sqs.ReceiveMessageOutput), args.Error(1)
}
func (c *mockSQSClient) DeleteMessageBatch(input *sqs.DeleteMessageBatchInput) (*sqs.DeleteMessageBatchOutput, error) {
args := c.Called(input)
return args.Get(0).(*sqs.DeleteMessageBatchOutput), args.Error(1)
}