This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathwriter_test.go
117 lines (110 loc) · 2.81 KB
/
writer_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
package rabbitmq
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"
"time"
"github.com/compose/transporter/log"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/ops"
"github.com/streadway/amqp"
)
var (
writerTestData = TestData{"writer_queue", "writer_key", 0}
writerTestData2 = TestData{"writer_queue_2", "writer_key_2", 0}
writerTestData3 = TestData{"writer_queue_3", "", 0}
)
func TestWriteWithKeyInField(t *testing.T) {
w := &Writer{amqp.Transient, "my_key", true}
for i := 0; i < 10; i++ {
_, err := w.Write(
message.From(
ops.Insert,
testExchange,
map[string]interface{}{"my_key": writerTestData.RoutingKey, "i": i},
),
)(defaultSession)
if err != nil {
t.Fatalf("unexpected Write error, %s\n", err)
}
}
_, err := w.Write(
message.From(
ops.Delete,
testExchange,
map[string]interface{}{"my_key": writerTestData.RoutingKey, "i": 100},
),
)(defaultSession)
if err != nil {
t.Fatalf("unexpected Write error, %s\n", err)
}
checkQueueCount(writerTestData.Queue, 10, t)
}
func TestWriteWithStaticKey(t *testing.T) {
w := &Writer{amqp.Transient, writerTestData2.RoutingKey, false}
for i := 0; i < 10; i++ {
_, err := w.Write(
message.From(
ops.Insert,
testExchange,
map[string]interface{}{"i": i},
),
)(defaultSession)
if err != nil {
t.Fatalf("unexpected Write error, %s\n", err)
}
}
checkQueueCount(writerTestData2.Queue, 10, t)
}
func TestWriteWithEmptyKey(t *testing.T) {
w := &Writer{amqp.Transient, writerTestData3.RoutingKey, false}
for i := 0; i < 10; i++ {
_, err := w.Write(
message.From(
ops.Insert,
testExchange,
map[string]interface{}{"i": i},
),
)(defaultSession)
if err != nil {
t.Fatalf("unexpected Write error, %s\n", err)
}
}
checkQueueCount(writerTestData3.Queue, 10, t)
}
func checkQueueCount(queue string, count int, t *testing.T) {
time.Sleep(5 * time.Second)
u, _ := url.Parse(DefaultURI)
vhost := u.Path
if vhost != "/" {
vhost = vhost[1:]
}
apiURL := fmt.Sprintf(
"http://%s:%d/api/queues/%s/%s",
u.Hostname(), DefaultAPIPort, url.QueryEscape(vhost), queue,
)
log.With("apiURL", apiURL).Infoln("requesting queue info")
req, _ := http.NewRequest(http.MethodGet, apiURL, nil)
if u.User != nil {
if pwd, ok := u.User.Password(); ok {
req.SetBasicAuth(u.User.Username(), pwd)
}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("unexpected http error, %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close()
var queueInfo struct {
MessagesReady float64 `json:"messages_ready"`
}
json.NewDecoder(resp.Body).Decode(&queueInfo)
if queueInfo.MessagesReady != 10 {
t.Errorf("wrong message count, expected 10, got %f", queueInfo.MessagesReady)
}
}