-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmq_test.go
79 lines (66 loc) · 1.52 KB
/
mq_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
package gomq
import (
"testing"
"time"
"errors"
"math/rand"
)
type MockConnection struct {}
type MockChannel struct {}
type MockQueue struct {
Name string
}
func init() {
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
}
func getRandError(errs []error) error {
return errs[rand.Intn(len(errs))]
}
func NewMockConnection(URI string) (Connection, error) {
errs := []error{
errors.New("New connection failed"),
nil,
}
return MockConnection{}, getRandError(errs)
}
func (conn MockConnection) GetChannel() (Channel, error) {
errs := []error{
errors.New("Get channel failed"),
nil,
}
return MockChannel{}, getRandError(errs)
}
func (conn MockConnection) Close() error {
errs := []error{
errors.New("Close failed"),
nil,
}
return getRandError(errs)
}
func (queue MockQueue) GetName() string {
return queue.Name
}
func (channel MockChannel) DeclareQueueByName(queueName string) (Queue, error) {
errs := []error{
errors.New("Declare queue failed"),
nil,
}
return MockQueue{queueName}, getRandError(errs)
}
func (channel MockChannel) Publish(queueName string, message string) error {
errs := []error{
errors.New("Publish failed"),
nil,
}
return getRandError(errs)
}
func TestPublish(t *testing.T) {
mq := NewMQ("test-url", 2*time.Nanosecond, NewMockConnection)
mq.Publish("test-queue", "test message")
}
func TestCrazyPublish(t *testing.T) {
mq := NewMQ("test-url", 2*time.Nanosecond, NewMockConnection)
for i := 0; i < 100; i ++ {
mq.Publish("test-queue", "test message")
}
}