-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
136 lines (108 loc) · 2.69 KB
/
worker_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
127
128
129
130
131
132
133
134
135
136
package outbox_test
import (
"context"
"database/sql"
"os"
"os/signal"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/kamal-github/outbox"
"github.com/kamal-github/outbox/datastore"
"github.com/kamal-github/outbox/pubsub"
"go.uber.org/zap"
)
func ExampleWorker_rabbit_with_pg() {
ctx := context.Background()
// Setup log
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
// Connect to Postgres
dsName := "postgres://postgres:password@localhost:5432/test-outbox?sslmode=disable"
dbConn, err := connectToSQLDB("postgres", dsName)
if err != nil {
panic(err)
}
// Setup Postgres as Minesweeper
mineSweeper, err := datastore.NewPostgres(dbConn, "outbox", logger)
if err != nil {
panic(err)
}
defer mineSweeper.Close()
// Setup RabbitMQ as PubSub
dispatcher, err := pubsub.NewRabbitMQ("", mineSweeper, logger)
if err != nil {
panic(err)
}
defer dispatcher.Close()
// Graceful shutdown
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
workerDone := make(chan struct{})
// Run worker in a separate go routine.
go outbox.Worker{
MineSweeper: mineSweeper,
Dispatcher: dispatcher,
Logger: logger,
MineInterval: 2 * time.Second,
}.Start(ctx, workerDone)
<-sig
cancel()
<-workerDone
}
func ExampleWorker_sqs_with_pg() {
ctx := context.Background()
// Setup log
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
// Connect to Postgres
dsName := "postgres://postgres:password@localhost:5432/test-outbox?sslmode=disable"
dbConn, err := connectToSQLDB("postgres", dsName)
if err != nil {
panic(err)
}
// Setup Postgres as Minesweeper
mineSweeper, err := datastore.NewPostgres(dbConn, "outbox", logger)
if err != nil {
panic(err)
}
defer mineSweeper.Close()
// Setup AWS session and SQS connection
awsSession := session.Must(session.NewSession())
sqsConn := sqs.New(awsSession)
// Setup SQS as PubSub
dispatcher, err := pubsub.NewSimpleQueueService(sqsConn, mineSweeper, logger)
if err != nil {
panic(err)
}
defer dispatcher.Close()
// Graceful shutdown
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
workerDone := make(chan struct{})
// Run worker in a separate go routine.
go outbox.Worker{
MineSweeper: mineSweeper,
Dispatcher: dispatcher,
Logger: logger,
MineInterval: 2 * time.Second,
}.Start(ctx, workerDone)
<-sig
cancel()
<-workerDone
}
func connectToSQLDB(driver, dsName string) (*sql.DB, error) {
db, err := sql.Open(driver, dsName)
if err != nil {
return db, err
}
return db, nil
}