-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch_test.go
71 lines (55 loc) · 1.32 KB
/
dispatch_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
package dispatch_test
import (
"encoding/json"
"errors"
"os"
. "github.com/hyperboloide/dispatch"
"github.com/hyperboloide/dispatch/amqp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Dispatch", func() {
var queue Queue
var result = make(chan bool, 1)
var host string
It("should create a new queue and purge it", func() {
if host = os.Getenv("RABBIT_HOST"); host == "" {
host = "amqp://guest:guest@localhost:5672/"
}
q, err := amqp.NewAMQP("test", host)
Ω(err).To(BeNil())
Ω(q.Purge()).To(BeNil())
queue = q
})
It("should send a message", func() {
msg, err := json.Marshal(struct {
Msg string `json:"msg"`
}{"ok"})
Ω(err).To(BeNil())
Ω(queue.SendBytes(msg)).To(BeNil())
// send message to stop
msg, err = json.Marshal(struct {
Msg string `json:"msg"`
}{"ko"})
Ω(err).To(BeNil())
Ω(queue.SendBytes(msg)).To(BeNil())
})
It("should Listen for messages", func() {
var listenner = func(b []byte) error {
var data = struct {
Msg string `json:"msg"`
}{}
if err := json.Unmarshal(b, &data); err != nil {
return err
} else if data.Msg == "ok" {
result <- true
} else {
return errors.New("Test Error")
}
return nil
}
Ω(queue.Listen(listenner)).ToNot(BeNil())
Ω(<-result).To(BeTrue())
close(result)
})
})