-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer_publish_test.go
112 lines (94 loc) · 2.78 KB
/
consumer_publish_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
package tests
import (
"context"
"fmt"
"testing"
"time"
"github.com/google/uuid"
"github.com/pmorelli92/bunnify/bunnify"
"go.uber.org/goleak"
)
func TestConsumerPublisher(t *testing.T) {
// Setup
queueName := uuid.NewString()
exchangeName := uuid.NewString()
routingKey := "order.orderCreated"
type orderCreated struct {
ID string `json:"id"`
}
exitCh := make(chan bool)
notificationChannel := make(chan bunnify.Notification)
go func() {
for {
select {
case n := <-notificationChannel:
fmt.Println(n)
case <-exitCh:
return
}
}
}()
// Exercise
connection := bunnify.NewConnection(
bunnify.WithURI("amqp://localhost:5672"),
bunnify.WithReconnectInterval(1*time.Second),
bunnify.WithNotificationChannel(notificationChannel))
if err := connection.Start(); err != nil {
t.Fatal(err)
}
var consumedEvent bunnify.ConsumableEvent[orderCreated]
eventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[orderCreated]) error {
consumedEvent = event
return nil
}
consumer := connection.NewConsumer(
queueName,
bunnify.WithQuorumQueue(),
bunnify.WithBindingToExchange(exchangeName),
bunnify.WithHandler(routingKey, eventHandler))
if err := consumer.Consume(); err != nil {
t.Fatal(err)
}
publisher := connection.NewPublisher()
orderCreatedID := uuid.NewString()
eventToPublish := bunnify.NewPublishableEvent(orderCreated{
ID: orderCreatedID,
})
err := publisher.Publish(
context.TODO(),
exchangeName,
routingKey,
eventToPublish)
if err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if err := connection.Close(); err != nil {
t.Fatal(err)
}
// Stop the notification go routine so goleak does not fail
exitCh <- true
// Assert
if eventToPublish.ID != consumedEvent.ID {
t.Fatalf("expected event ID %s, got %s", eventToPublish.ID, consumedEvent.ID)
}
if eventToPublish.CorrelationID != consumedEvent.CorrelationID {
t.Fatalf("expected correlation ID %s, got %s", eventToPublish.CorrelationID, consumedEvent.CorrelationID)
}
if !eventToPublish.Timestamp.Equal(consumedEvent.Timestamp) {
t.Fatalf("expected timestamp %s, got %s", eventToPublish.Timestamp, consumedEvent.Timestamp)
}
if orderCreatedID != consumedEvent.Payload.ID {
t.Fatalf("expected order created ID %s, got %s", orderCreatedID, consumedEvent.Payload.ID)
}
if exchangeName != consumedEvent.DeliveryInfo.Exchange {
t.Fatalf("expected exchange %s, got %s", exchangeName, consumedEvent.DeliveryInfo.Exchange)
}
if queueName != consumedEvent.DeliveryInfo.Queue {
t.Fatalf("expected queue %s, got %s", queueName, consumedEvent.DeliveryInfo.Queue)
}
if routingKey != consumedEvent.DeliveryInfo.RoutingKey {
t.Fatalf("expected routing key %s, got %s", routingKey, consumedEvent.DeliveryInfo.RoutingKey)
}
goleak.VerifyNone(t)
}