-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis_test.go
91 lines (76 loc) · 1.59 KB
/
redis_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
package plasma_client
import (
"encoding/json"
"fmt"
"testing"
"time"
redis "gopkg.in/redis.v5"
"github.com/openfresh/plasma-go/config"
"github.com/openfresh/plasma-go/event"
)
func receive(config config.Config) (string, error) {
redisConf := config.Redis
opt := &redis.Options{
Addr: redisConf.Addr,
Password: redisConf.Password,
DB: redisConf.DB,
}
client := redis.NewClient(opt)
ps, err := client.Subscribe(redisConf.Channel)
if err != nil {
return "", err
}
msg, err := ps.ReceiveMessage()
if err != nil {
return "", err
}
return msg.Payload, nil
}
func TestRedisPublish(t *testing.T) {
conf := config.Config{
Redis: config.Redis{
Addr: "localhost:6379",
DB: 0,
Channel: "plasma",
},
}
pub, err := newRedis(conf)
if err != nil {
t.Fatal(err)
}
payload := event.Payload{
Meta: event.MetaData{
Type: "test",
},
Data: json.RawMessage(`{"data":"test message"}`),
}
msgChan := make(chan string)
go func() {
msg, err := receive(conf)
if err != nil {
t.Error(err)
close(msgChan)
return
}
msgChan <- msg
}()
time.Sleep(10 * time.Millisecond)
if err := pub.Publish(payload); err != nil {
t.Fatal(err)
}
msg, ok := <-msgChan
if !ok {
t.Fatal("msgChan closed")
return
}
p := event.Payload{}
if err := json.Unmarshal([]byte(msg), &p); err != nil {
t.Fatal(err)
}
if p.Meta.Type != payload.Meta.Type {
t.Error(fmt.Sprintf("Expected: %s, Actual: %s", payload.Meta.Type, p.Meta.Type))
}
if string(p.Data) != string(payload.Data) {
t.Error(fmt.Sprintf("Expected: %s, Actual: %s", payload.Data, p.Data))
}
}