-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_test.go
More file actions
71 lines (62 loc) · 1.82 KB
/
Copy pathexample_test.go
File metadata and controls
71 lines (62 loc) · 1.82 KB
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
package blockqueue_test
import (
"context"
"database/sql"
"fmt"
"github.com/yudhasubki/blockqueue"
"github.com/yudhasubki/blockqueue/store/sqlite"
)
func ExampleQueue_Publish() {
queue, topic := newExampleQueue()
defer queue.Close()
receipt, err := queue.Publish(context.Background(), topic, blockqueue.Message{
Message: `{"order_id":"order-1022"}`,
IdempotencyKey: "order-1022",
})
if err != nil {
panic(err)
}
fmt.Println(receipt.State, *receipt.Duplicate)
// Output: persisted false
}
func ExampleQueue_WithTx() {
queue, topic := newExampleQueue()
defer queue.Close()
ctx := context.Background()
var publishState string
err := queue.WithTx(ctx, nil, func(tx *sql.Tx) error {
if _, err := tx.ExecContext(ctx, `
CREATE TABLE orders (id TEXT PRIMARY KEY, status TEXT NOT NULL)
`); err != nil {
return err
}
if _, err := tx.ExecContext(ctx,
"INSERT INTO orders (id, status) VALUES (?, ?)", "order-1022", "pending"); err != nil {
return err
}
receipt, err := queue.PublishTx(ctx, tx, topic, blockqueue.Message{
Message: `{"order_id":"order-1022"}`,
IdempotencyKey: "fulfill-order-1022",
})
publishState = receipt.State
return err
})
fmt.Println(publishState, err == nil)
// Output: staged true
}
func newExampleQueue() (*blockqueue.Queue, blockqueue.Topic) {
driver, err := sqlite.Open(":memory:", sqlite.Config{})
if err != nil {
panic(err)
}
queue := blockqueue.New(driver, blockqueue.Options{DisableMetrics: true})
if err := queue.Run(context.Background()); err != nil {
panic(err)
}
topic := blockqueue.NewTopic("orders")
subscriber := blockqueue.NewSubscriber(topic, "fulfillment", blockqueue.SubscriberOptions{})
if err := queue.CreateTopic(context.Background(), topic, blockqueue.Subscribers{subscriber}); err != nil {
panic(err)
}
return queue, topic
}