-
Notifications
You must be signed in to change notification settings - Fork 11
/
example_test.go
54 lines (46 loc) · 926 Bytes
/
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
package courier_test
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/gojek/courier-go"
)
func ExampleNewClient() {
c, err := courier.NewClient(
courier.WithUsername("username"),
courier.WithPassword("password"),
courier.WithAddress("localhost", 1883),
)
if err != nil {
panic(err)
}
if err := c.Start(); err != nil {
panic(err)
}
stopCh := make(chan os.Signal, 1)
signal.Notify(stopCh, []os.Signal{os.Interrupt, syscall.SIGTERM}...)
go func() {
tick := time.NewTicker(time.Second)
for {
select {
case t := <-tick.C:
msg := map[string]interface{}{
"time": t.UnixNano(),
}
if err := c.Publish(context.Background(), "topic", msg, courier.QOSOne); err != nil {
fmt.Printf("Publish() error = %s\n", err)
} else {
fmt.Println("Publish() success")
}
case <-stopCh:
tick.Stop()
return
}
}
}()
<-stopCh
c.Stop()
}