forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
liveTestHelpers_test.go
235 lines (183 loc) · 7.47 KB
/
liveTestHelpers_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azservicebus
import (
"context"
"crypto/tls"
"errors"
"fmt"
"os"
"testing"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus/admin"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus/internal/test"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus/internal/utils"
"github.com/stretchr/testify/require"
)
func enableDebugClientOptions(t *testing.T, baseClientOptions *ClientOptions) (*ClientOptions, func()) {
// Setting this variable will cause the SB client to dump out (in TESTS ONLY)
// the pre-master-key for your AMQP connection. This allows you decrypt a packet
// capture from wireshark.
//
// If you want to do this just set SSLKEYLOGFILE_TEST env var to a path on disk and
// Go will write out the key.
keyLogFile := os.Getenv("SSLKEYLOGFILE_TEST")
var clientOptions ClientOptions
if baseClientOptions != nil {
clientOptions = *baseClientOptions
}
if keyLogFile != "" {
writer, err := os.Create(keyLogFile)
if err != nil {
require.Fail(t, fmt.Sprintf("SSLKEYLOGFILE_TEST was set but we failed to create a keylog file at %s: %s", keyLogFile, err))
}
clientOptions.TLSConfig = &tls.Config{
KeyLogWriter: writer,
}
return &clientOptions, func() { _ = writer.Close() }
}
return &clientOptions, func() {}
}
type liveTestOptions struct {
QueueProperties *admin.QueueProperties
ClientOptions *ClientOptions
}
func setupLiveTest(t *testing.T, options *liveTestOptions) (*Client, func(), string) {
if options == nil {
options = &liveTestOptions{}
}
cs := test.GetConnectionString(t)
clientOptions, flushKeyFn := enableDebugClientOptions(t, options.ClientOptions)
serviceBusClient, err := NewClientFromConnectionString(cs, clientOptions)
require.NoError(t, err)
queueName, cleanupQueue := createQueue(t, cs, options.QueueProperties)
testCleanup := func() {
require.NoError(t, serviceBusClient.Close(context.Background()))
flushKeyFn()
cleanupQueue()
// just a simple sanity check that closing twice doesn't cause errors.
// it's basically zero cost since all the links and connection are gone from the
// first Close().
require.NoError(t, serviceBusClient.Close(context.Background()))
}
return serviceBusClient, testCleanup, queueName
}
type liveTestOptionsWithSubscription struct {
SubscriptionProperties *admin.SubscriptionProperties
TopicProperties *admin.TopicProperties
ClientOptions *ClientOptions
}
func setupLiveTestWithSubscription(t *testing.T, options *liveTestOptionsWithSubscription) (client *Client, cleanup func(), topic string, subscription string) {
if options == nil {
options = &liveTestOptionsWithSubscription{}
}
cs := test.GetConnectionString(t)
clientOptions, flushKeyFn := enableDebugClientOptions(t, options.ClientOptions)
serviceBusClient, err := NewClientFromConnectionString(cs, clientOptions)
require.NoError(t, err)
topic, cleanupTopic := createSubscription(t, cs, options.TopicProperties, options.SubscriptionProperties)
testCleanup := func() {
require.NoError(t, serviceBusClient.Close(context.Background()))
flushKeyFn()
cleanupTopic()
// just a simple sanity check that closing twice doesn't cause errors.
// it's basically zero cost since all the links and connection are gone from the
// first Close().
require.NoError(t, serviceBusClient.Close(context.Background()))
}
return serviceBusClient, testCleanup, topic, "sub"
}
// createQueue creates a queue, automatically setting it to delete on idle in 5 minutes.
func createQueue(t *testing.T, connectionString string, queueProperties *admin.QueueProperties) (string, func()) {
nanoSeconds := time.Now().UnixNano()
queueName := fmt.Sprintf("queue-%X", nanoSeconds)
adminClient, err := admin.NewClientFromConnectionString(connectionString, nil)
require.NoError(t, err)
if queueProperties == nil {
queueProperties = &admin.QueueProperties{}
}
if queueProperties.AutoDeleteOnIdle == nil {
autoDeleteOnIdle := "PT5M"
queueProperties.AutoDeleteOnIdle = &autoDeleteOnIdle
}
_, err = adminClient.CreateQueue(context.Background(), queueName, &admin.CreateQueueOptions{
Properties: queueProperties,
})
require.NoError(t, err)
return queueName, func() {
deleteQueue(t, adminClient, queueName)
}
}
// createSubscription creates a topic, automatically setting it to delete on idle in 5 minutes.
// It also creates a subscription named 'sub'.
func createSubscription(t *testing.T, connectionString string, topicProperties *admin.TopicProperties, subscriptionProperties *admin.SubscriptionProperties) (string, func()) {
nanoSeconds := time.Now().UnixNano()
topicName := fmt.Sprintf("topic-%X", nanoSeconds)
adminClient, err := admin.NewClientFromConnectionString(connectionString, nil)
require.NoError(t, err)
if topicProperties == nil {
topicProperties = &admin.TopicProperties{}
}
autoDeleteOnIdle := "PT5M"
topicProperties.AutoDeleteOnIdle = &autoDeleteOnIdle
_, err = adminClient.CreateTopic(context.Background(), topicName, &admin.CreateTopicOptions{
Properties: topicProperties,
})
require.NoError(t, err)
_, err = adminClient.CreateSubscription(context.Background(), topicName, "sub", &admin.CreateSubscriptionOptions{Properties: subscriptionProperties})
require.NoError(t, err)
return topicName, func() {
_, err := adminClient.DeleteTopic(context.Background(), topicName, nil)
require.NoError(t, err)
}
}
func deleteQueue(t *testing.T, ac *admin.Client, queueName string) {
_, err := ac.DeleteQueue(context.Background(), queueName, nil)
require.NoError(t, err)
}
func deleteSubscription(t *testing.T, ac *admin.Client, topicName string, subscriptionName string) {
_, err := ac.DeleteSubscription(context.Background(), topicName, subscriptionName, nil)
require.NoError(t, err)
}
// peekSingleMessageForTest wraps a standard Receiver.Peek() call so it returns at least one message
// and fails tests otherwise.
func peekSingleMessageForTest(t *testing.T, receiver *Receiver, options *PeekMessagesOptions) *ReceivedMessage {
var msg *ReceivedMessage
// Peek, unlike Receive, doesn't block until at least one message has arrived, so we have to poll
// to get a similar effect.
err := utils.Retry(context.Background(), EventReceiver, "peekSingleForTest", func(ctx context.Context, args *utils.RetryFnArgs) error {
peekedMessages, err := receiver.PeekMessages(context.Background(), 1, options)
require.NoError(t, err)
if len(peekedMessages) == 1 {
msg = peekedMessages[0]
return nil
} else {
return errors.New("No peekable messages available")
}
}, func(err error) bool {
return false
}, RetryOptions{})
require.NoError(t, err)
return msg
}
func requireScheduledMessageDisappears(ctx context.Context, t *testing.T, receiver *Receiver, sequenceNumber int64) {
// this function will keep checking a particular sequence number until it's gone (ie, it was the last
// sequence number so it's obvious) _or_ we end up retrieving the next message instead since
// it auto-skips gaps.
for {
msgs, err := receiver.PeekMessages(ctx, 1, &PeekMessagesOptions{
FromSequenceNumber: &sequenceNumber,
})
require.NoError(t, err)
if len(msgs) == 0 {
// no message exists at the sequence number, and there was nowhere to jump to
return
}
if *msgs[0].SequenceNumber != sequenceNumber {
// the message is gone, we've been pushed to the next message after the "gap"
return
}
require.Equal(t, MessageStateScheduled, msgs[0].State)
time.Sleep(100 * time.Millisecond)
}
}