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
|
package server
import (
"testing"
"github.com/NeowayLabs/wabbit"
)
func TestVHostWithDefaults(t *testing.T) {
vh := NewVHost("/")
if vh.name != "/" {
t.Errorf("Invalid broker name: %s", vh.name)
}
if len(vh.exchanges) < 5 || vh.exchanges[""] == nil ||
vh.exchanges["amq.direct"] == nil || vh.exchanges["amq.topic"] == nil {
t.Errorf("VHost created without the required exchanges specified by amqp 0.9.1")
}
}
func TestQueueDeclare(t *testing.T) {
vh := NewVHost("/")
q, err := vh.QueueDeclare("test-queue", nil)
if err != nil {
t.Error(err)
}
if q.Name() != "test-queue" {
t.Errorf("Invalid queue name")
}
if len(vh.queues) != 1 || vh.queues["test-queue"] != q {
t.Errorf("Failed to declare queue")
}
if q.Messages() != 0 || q.Consumers() != 0 {
t.Errorf("Invalid number of messages or consumers")
}
nwExchange, ok := vh.exchanges[""].(*DirectExchange)
if !ok {
t.Errorf("Exchange neoway not created")
return
}
if len(nwExchange.bindings) != 1 {
t.Errorf("Binding not created")
return
}
}
func TestBasicExchangeDeclare(t *testing.T) {
vh := NewVHost("/")
err := vh.ExchangeDeclare("neoway", "amq.direct", nil)
if err == nil {
t.Errorf("The exchange type correct name is direct")
return
}
err = vh.ExchangeDeclare("neoway", "direct", nil)
if err != nil {
t.Error(err)
return
}
if len(vh.exchanges) != 6 {
t.Errorf("Exchange not properly created: %d", len(vh.exchanges))
return
}
}
func TestQueueBind(t *testing.T) {
vh := NewVHost("/")
err := vh.ExchangeDeclare("neoway", "direct", nil)
if err != nil {
t.Error(err)
return
}
q, err := vh.QueueDeclare("queue-test", nil)
if err != nil {
t.Error(err)
return
}
if q.Name() != "queue-test" {
t.Errorf("Something wrong declaring queue")
return
}
err = vh.QueueBind("queue-test", "process.data", "neoway", nil)
if err != nil {
t.Error(err)
return
}
nwExchange, ok := vh.exchanges["neoway"].(*DirectExchange)
if !ok {
t.Errorf("Exchange neoway not created")
return
}
if len(nwExchange.bindings) != 1 {
t.Errorf("Binding not created")
return
}
err = nwExchange.route("process.data", NewDelivery(&Channel{}, []byte{}, 1, "", wabbit.Option{}))
if err != nil {
t.Error(err)
return
}
}
func TestBasicPublish(t *testing.T) {
vh := NewVHost("/")
err := vh.ExchangeDeclare("neoway", "topic", nil)
if err != nil {
t.Error(err)
return
}
q, err := vh.QueueDeclare("data", nil)
if err != nil {
t.Error(err)
return
}
if q.Name() != "data" {
t.Errorf("Invalid queue name")
return
}
err = vh.QueueBind("data", "process.data", "neoway", nil)
if err != nil {
t.Error(err)
return
}
err = vh.Publish("neoway", "process.data", NewDelivery(&Channel{}, []byte("teste"), 1, "", wabbit.Option{}), nil)
if err != nil {
t.Error(err)
return
}
serverQueue, ok := q.(*Queue)
if !ok {
t.Errorf("Queue isn't of type *server.Queue")
return
}
data := <-serverQueue.data
if string(data.Body()) != "teste" {
t.Errorf("Failed to publish message to specified route")
return
}
}
|