-
Notifications
You must be signed in to change notification settings - Fork 4
/
listener_example_test.go
126 lines (103 loc) · 2.91 KB
/
listener_example_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
118
119
120
121
122
123
124
125
126
package pg
import (
"context"
"fmt"
"time"
)
// go test -coverprofile=cov
// go tool cover -html=cov
func ExampleDB_Listen() {
db, err := openEmptyTestConnection()
if err != nil {
handleExampleError(err)
return
}
// defer db.Close()
const channel = "chat_db"
conn, err := db.Listen(context.Background(), channel)
if err != nil {
fmt.Println(fmt.Errorf("listen: %w\n", err))
return
}
go func() {
// To just terminate this listener's connection and unlisten from the channel:
defer conn.Close(context.Background())
for {
notification, err := conn.Accept(context.Background())
if err != nil {
fmt.Println(fmt.Errorf("accept: %w\n", err))
return
}
fmt.Printf("channel: %s, payload: %s\n", notification.Channel, notification.Payload)
}
}()
if err = db.Notify(context.Background(), channel, "hello"); err != nil {
fmt.Println(fmt.Errorf("notify: hello: %w", err))
return
}
if err = db.Notify(context.Background(), channel, "world"); err != nil {
fmt.Println(fmt.Errorf("notify: world: %w", err))
return
}
time.Sleep(5 * time.Second) // give it sometime to receive the notifications.
// Output:
// channel: chat_db, payload: hello
// channel: chat_db, payload: world
}
type Message struct {
BaseEntity
Sender string `pg:"type=varchar(255)" json:"sender"`
Body string `pg:"type=text" json:"body"`
}
func Example_notify_JSON() {
schema := NewSchema()
db, err := Open(context.Background(), schema, getTestConnString())
if err != nil {
fmt.Println(err)
return
}
// defer db.Close()
const channel = "chat_json"
conn, err := db.Listen(context.Background(), channel)
if err != nil {
fmt.Println(fmt.Errorf("listen: %w", err))
}
go func() {
// To just terminate this listener's connection and unlisten from the channel:
defer conn.Close(context.Background())
for {
notification, err := conn.Accept(context.Background())
if err != nil {
fmt.Println(fmt.Errorf("accept: %w\n", err))
return
}
payload, err := UnmarshalNotification[Message](notification)
if err != nil {
fmt.Println(fmt.Errorf("N: %w", err))
return
}
fmt.Printf("channel: %s, payload.sender: %s, payload.body: %s\n",
notification.Channel, payload.Sender, payload.Body)
}
}()
firstMessage := Message{
Sender: "kataras",
Body: "hello",
}
if err = db.Notify(context.Background(), channel, firstMessage); err != nil {
fmt.Println(fmt.Errorf("notify: first message: %w", err))
return
}
secondMessage := Message{
Sender: "kataras",
Body: "world",
}
if err = db.Notify(context.Background(), channel, secondMessage); err != nil {
fmt.Println(fmt.Errorf("notify: second message: %w", err))
return
}
time.Sleep(5 * time.Second) // give it sometime to receive the notifications, this is too much though.
// Output:
// channel: chat_json, payload.sender: kataras, payload.body: hello
// channel: chat_json, payload.sender: kataras, payload.body: world
}