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 217
/
reader_test.go
77 lines (71 loc) · 2.07 KB
/
reader_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
package rabbitmq
import (
"sync"
"testing"
"time"
"github.com/compose/transporter/client"
"github.com/streadway/amqp"
)
var (
readerTestData = TestData{"reader_queue", "reader_key", 10}
readerBadDataTest = TestData{"reader_bad_data_queue", "reader_bad_data_key", 10}
)
func TestRead(t *testing.T) {
if testing.Short() {
t.Skip("skipping Read in short mode")
}
reader := &Reader{DefaultURI, DefaultAPIPort}
readFunc := reader.Read(map[string]client.MessageSet{},
func(queue string) bool { return queue == readerTestData.Queue })
done := make(chan struct{})
msgChan, err := readFunc(defaultSession, done)
if err != nil {
t.Fatalf("unexpected Read error, %s\n", err)
}
checkCount(readerTestData.Queue, readerTestData.InsertCount, msgChan, t)
close(done)
}
func TestReadBadData(t *testing.T) {
if testing.Short() {
t.Skip("skipping Read in short mode")
}
reader := &Reader{DefaultURI, DefaultAPIPort}
readFunc := reader.Read(map[string]client.MessageSet{},
func(queue string) bool { return queue == readerBadDataTest.Queue })
done := make(chan struct{})
msgChan, err := readFunc(defaultSession, done)
if err != nil {
t.Fatalf("unexpected Read error, %s\n", err)
}
msg := amqp.Publishing{
DeliveryMode: amqp.Transient,
Timestamp: time.Now(),
ContentType: "application/json",
Body: []byte(`{"id": %d, "message": "hello"`),
}
if err := defaultSession.channel.Publish(testExchange, readerBadDataTest.RoutingKey, false, false, msg); err != nil {
t.Fatalf("failed to publish to queue (%s), %s", readerBadDataTest.Queue, err)
}
checkCount(readerBadDataTest.Queue, readerBadDataTest.InsertCount, msgChan, t)
close(done)
}
func checkCount(desc string, expected int, msgChan <-chan client.MessageSet, t *testing.T) {
var numMsgs int
var wg sync.WaitGroup
wg.Add(1)
go func(wg *sync.WaitGroup) {
for {
select {
case <-msgChan:
numMsgs++
case <-time.After(1 * time.Second):
wg.Done()
return
}
}
}(&wg)
wg.Wait()
if numMsgs != expected {
t.Errorf("[%s] bad message count, expected %d, got %d\n", desc, expected, numMsgs)
}
}