-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging_test.go
107 lines (100 loc) · 2.35 KB
/
messaging_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
package rabbitmq
import "testing"
const url = "amqp://guest:guest@localhost:5672/"
type Address struct {
City string
ZipCode uint
}
type User struct {
Id string
Name string
Address Address
}
// TestMessaging_AddWorker_SendPush define a worker and checking send push message in worker.
func TestMessaging_AddWorker_SendPush(t *testing.T) {
var messaging, err = NewMessaging(url)
if err != nil {
t.Fatal(err)
}
c := make(chan interface{})
err = messaging.AddWorker(
"bar",
func(message Message) (interface{}, Acknowledge) {
c <- message.Body
return nil, None
},
)
if err != nil {
t.Fatal(err)
}
var data interface{} = "foo"
err = messaging.SendPush("bar", &data)
if err != nil {
t.Fatal(err)
}
if data != <-c {
t.Fatalf(`The data received in the worker must be equal to the sent in SendPush`)
}
}
// TestMessaging_AddWorker_RpcCall define a worker and checking result return from Rpc call.
func TestMessaging_AddWorker_RpcCall(t *testing.T) {
var messaging, err = NewMessaging(url)
if err != nil {
t.Fatal(err)
}
messaging.RegisterType(User{})
err = messaging.AddWorker(
"foo",
func(message Message) (interface{}, Acknowledge) {
var id = message.Body.(string)
var result interface{} = User{id, "Farhad", Address{"San Francisco", 5451}}
return &result, None
},
)
if err != nil {
t.Fatal(err)
}
var id interface{} = "12"
err, result := messaging.RpcCall("foo", &id)
if err != nil {
t.Fatal(err)
}
user := result.(User)
if user.Id != id.(string) || user.Name != "Farhad" {
t.Fatalf(`The return value of the worker must be equal to the value sent in RpcCall`)
}
}
// TestMessaging_Publish_Subscribe define a subscriber and checking publish.
func TestMessaging_Publish_Subscribe(t *testing.T) {
var messaging, err = NewMessaging(url)
if err != nil {
t.Fatal(err)
}
a, b := make(chan interface{}), make(chan interface{})
err = messaging.Subscribe(
"topic1",
func(message Message) {
a <- message.Body
},
)
if err != nil {
t.Fatal(err)
}
err = messaging.Subscribe(
"topic1",
func(message Message) {
b <- message.Body
},
)
if err != nil {
t.Fatal(err)
}
var data interface{} = "foo"
err = messaging.Publish("topic1", &data)
if err != nil {
t.Fatal(err)
}
if data != <-a || data != <-b {
t.Fatalf(`The data received in the subscribers must be equal to the sent in Publish`)
}
}