-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_example_test.go
128 lines (116 loc) · 2.94 KB
/
db_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
127
128
package dbtools_test
import (
"context"
"fmt"
"strings"
"time"
"github.com/arsham/dbtools/v4"
"github.com/arsham/retry/v3"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
)
func ExampleNewPGX() {
// This setup tries the transaction only once.
dbtools.New(&exampleConn{})
// This setup tries 100 times until succeeds. The delay is set to 10ms and
// it uses the retry.IncrementalDelay method, which means every time it
// increments the delay between retries with a jitter to avoid thunder herd
// problem.
dbtools.New(&exampleConn{},
dbtools.Retry(10, 100*time.Millisecond),
)
// Output:
}
func ExamplePGX_Transaction() {
tr, err := dbtools.New(&exampleConn{})
if err != nil {
panic(err)
}
err = tr.Transaction(context.Background(), func(pgx.Tx) error {
fmt.Println("Running first query.")
return nil
}, func(pgx.Tx) error {
fmt.Println("Running second query.")
return nil
})
fmt.Printf("Transaction's error: %v", err)
// Output:
// Running first query.
// Running second query.
// Transaction's error: <nil>
}
func ExamplePGX_Transaction_retries() {
tr, err := dbtools.New(&exampleConn{}, dbtools.Retry(10, 100*time.Millisecond))
if err != nil {
panic(err)
}
called := false
err = tr.Transaction(context.Background(), func(pgx.Tx) error {
fmt.Println("Running first query.")
return nil
}, func(pgx.Tx) error {
if !called {
called = true
fmt.Println("Second query error.")
return assert.AnError
}
fmt.Println("Running second query.")
return nil
})
fmt.Printf("Transaction's error: %v", err)
// Output:
// Running first query.
// Second query error.
// Running first query.
// Running second query.
// Transaction's error: <nil>
}
func ExamplePGX_Transaction_stopTrying() {
// This example shows how to stop trying when we know an error is not
// recoverable.
tr, err := dbtools.New(&exampleConn{},
dbtools.Retry(10, time.Second),
)
if err != nil {
panic(err)
}
err = tr.Transaction(context.Background(), func(pgx.Tx) error {
fmt.Println("Running first query.")
return nil
}, func(pgx.Tx) error {
fmt.Println("Running second query.")
return &retry.StopError{Err: assert.AnError}
})
fmt.Printf("Transaction returns my error: %t", strings.Contains(err.Error(), assert.AnError.Error()))
// Output:
// Running first query.
// Running second query.
// Transaction returns my error: true
}
func ExamplePGX_Transaction_panics() {
tr, err := dbtools.New(&exampleConn{}, dbtools.Retry(10, 100*time.Millisecond))
if err != nil {
panic(err)
}
calls := 0
err = tr.Transaction(context.Background(), func(pgx.Tx) error {
calls++
fmt.Printf("Call #%d.\n", calls)
if calls < 5 {
panic("We have a panic!")
}
fmt.Println("All done.")
return nil
})
fmt.Printf("Transaction's error: %v\n", err)
fmt.Printf("Called %d times.\n", calls)
// Output:
// Call #1.
// Call #2.
// Call #3.
// Call #4.
// Call #5.
// All done.
// Transaction's error: <nil>
// Called 5 times.
}