This repository has been archived by the owner on May 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
publisher_test.go
149 lines (110 loc) · 2.9 KB
/
publisher_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
package dispatcher
import (
"encoding/json"
"errors"
"github.com/gofort/dispatcher/log"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
const publisherTestExchange = "dispatcher_test"
const publisherTestQueue = "test_queue"
const publisherTestRoutingKey = "test_rk_1"
func createPublisherTestEnv() (*amqp.Connection, *publisher, error) {
p := &publisher{
log: log.InitLogger(true),
}
con, err := amqp.Dial(os.Getenv("DISPATCHER_AMQP_CON"))
if err != nil {
return nil, nil, err
}
ch, err := con.Channel()
if err != nil {
return nil, nil, err
}
defer ch.Close()
if err := declareExchange(ch, publisherTestExchange); err != nil {
return nil, nil, err
}
if err := declareQueue(ch, publisherTestQueue); err != nil {
return nil, nil, err
}
if err := queueBind(ch, publisherTestExchange, publisherTestQueue, publisherTestRoutingKey); err != nil {
return nil, nil, err
}
return con, p, nil
}
func destroyPublisherTestEnv(con *amqp.Connection, p *publisher) {
p.ch.ExchangeDelete(publisherTestExchange, false, false)
p.ch.QueueDelete(publisherTestExchange, false, false, false)
p.deactivate()
con.Close()
}
func newPublisherTestTask() *Task {
return &Task{
Name: "test_task_1",
Args: []TaskArgument{{"string", "test string"}, {"int", 1}},
}
}
func TestPublisher_Publish(t *testing.T) {
as := assert.New(t)
con, p, err := createPublisherTestEnv()
if err != nil {
t.Error(err)
return
}
defer destroyPublisherTestEnv(con, p)
p.defaultExchange = publisherTestExchange
p.defaultRoutingKey = publisherTestRoutingKey
if err = p.init(con); err != nil {
t.Error(err)
return
}
task := newPublisherTestTask()
if err = p.Publish(task); err != nil {
t.Error(err)
return
}
q, err := p.ch.QueueInspect(publisherTestQueue)
if err != nil {
t.Error(err)
return
}
as.Equal(1, q.Messages, "Number of messages in queue is not equal to 1")
deliveries, err := p.ch.Consume(publisherTestQueue, "test_consumer_1", true, false, false, false, nil)
if err != nil {
t.Error(err)
return
}
msg := <-deliveries
var receivedTask Task
if err = json.Unmarshal(msg.Body, &receivedTask); err != nil {
t.Error(err)
return
}
if task.UUID == receivedTask.UUID && task.Name == receivedTask.Name && task.Args[0].Type == receivedTask.Args[0].Type && task.Args[0].Value == receivedTask.Args[0].Value {
return
}
t.Error("Sended task and received task are not equal")
}
func TestPublisher_Publish2(t *testing.T) {
con, p, err := createPublisherTestEnv()
if err != nil {
t.Error(err)
return
}
defer destroyPublisherTestEnv(con, p)
p.defaultExchange = publisherTestExchange
p.defaultRoutingKey = publisherTestRoutingKey
if err = p.init(con); err != nil {
t.Error(err)
return
}
task := newPublisherTestTask()
task.Name = ""
if err = p.Publish(task); err == nil {
t.Error(errors.New("Task had no name - error expected"))
return
}
}