Debian Codegraph @

amqptest/server/vhost_test.go

raw
1 package server
2
3 import (
4 "testing"
5
6 "github.com/NeowayLabs/wabbit"
7 )
8
9 func TestVHostWithDefaults(t *testing.T) {
10 vh := NewVHost("/")
11
12 if vh.name != "/" {
13 t.Errorf("Invalid broker name: %s", vh.name)
14 }
15
16 if len(vh.exchanges) < 5 || vh.exchanges[""] == nil ||
17 vh.exchanges["amq.direct"] == nil || vh.exchanges["amq.topic"] == nil {
18 t.Errorf("VHost created without the required exchanges specified by amqp 0.9.1")
19 }
20 }
21
22 func TestQueueDeclare(t *testing.T) {
23 vh := NewVHost("/")
24
25 q, err := vh.QueueDeclare("test-queue", nil)
26
27 if err != nil {
28 t.Error(err)
29 }
30
31 if q.Name() != "test-queue" {
32 t.Errorf("Invalid queue name")
33 }
34
35 if len(vh.queues) != 1 || vh.queues["test-queue"] != q {
36 t.Errorf("Failed to declare queue")
37 }
38
39 if q.Messages() != 0 || q.Consumers() != 0 {
40 t.Errorf("Invalid number of messages or consumers")
41 }
42
43 nwExchange, ok := vh.exchanges[""].(*DirectExchange)
44
45 if !ok {
46 t.Errorf("Exchange neoway not created")
47 return
48 }
49
50 if len(nwExchange.bindings) != 1 {
51 t.Errorf("Binding not created")
52 return
53 }
54 }
55
56 func TestBasicExchangeDeclare(t *testing.T) {
57 vh := NewVHost("/")
58
59 err := vh.ExchangeDeclare("neoway", "amq.direct", nil)
60
61 if err == nil {
62 t.Errorf("The exchange type correct name is direct")
63 return
64 }
65
66 err = vh.ExchangeDeclare("neoway", "direct", nil)
67
68 if err != nil {
69 t.Error(err)
70 return
71 }
72
73 if len(vh.exchanges) != 6 {
74 t.Errorf("Exchange not properly created: %d", len(vh.exchanges))
75 return
76 }
77 }
78
79 func TestQueueBind(t *testing.T) {
80 vh := NewVHost("/")
81
82 err := vh.ExchangeDeclare("neoway", "direct", nil)
83
84 if err != nil {
85 t.Error(err)
86 return
87 }
88
89 q, err := vh.QueueDeclare("queue-test", nil)
90
91 if err != nil {
92 t.Error(err)
93 return
94 }
95
96 if q.Name() != "queue-test" {
97 t.Errorf("Something wrong declaring queue")
98 return
99 }
100
101 err = vh.QueueBind("queue-test", "process.data", "neoway", nil)
102
103 if err != nil {
104 t.Error(err)
105 return
106 }
107
108 nwExchange, ok := vh.exchanges["neoway"].(*DirectExchange)
109
110 if !ok {
111 t.Errorf("Exchange neoway not created")
112 return
113 }
114
115 if len(nwExchange.bindings) != 1 {
116 t.Errorf("Binding not created")
117 return
118 }
119
120 err = nwExchange.route("process.data", NewDelivery(&Channel{}, []byte{}, 1, "", wabbit.Option{}, ""))
121
122 if err != nil {
123 t.Error(err)
124 return
125 }
126 }
127
128 func TestBasicPublish(t *testing.T) {
129 vh := NewVHost("/")
130
131 err := vh.ExchangeDeclare("neoway", "topic", nil)
132
133 if err != nil {
134 t.Error(err)
135 return
136 }
137
138 q, err := vh.QueueDeclare("data", nil)
139
140 if err != nil {
141 t.Error(err)
142 return
143 }
144
145 if q.Name() != "data" {
146 t.Errorf("Invalid queue name")
147 return
148 }
149
150 err = vh.QueueBind("data", "process.data", "neoway", nil)
151
152 if err != nil {
153 t.Error(err)
154 return
155 }
156
157 err = vh.Publish("neoway", "process.data", NewDelivery(&Channel{}, []byte("teste"), 1, "", wabbit.Option{}, ""), nil)
158
159 if err != nil {
160 t.Error(err)
161 return
162 }
163
164 serverQueue, ok := q.(*Queue)
165
166 if !ok {
167 t.Errorf("Queue isn't of type *server.Queue")
168 return
169 }
170
171 data := <-serverQueue.data
172
173 if string(data.Body()) != "teste" {
174 t.Errorf("Failed to publish message to specified route")
175 return
176 }
177 }