/
pool_test.go
149 lines (117 loc) · 3.39 KB
/
pool_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
137
138
139
140
141
142
143
144
145
146
147
148
149
package raptor_test
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/maddiesch/go-raptor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func TestNewPool(t *testing.T) {
t.Run("given a size of 0", func(t *testing.T) {
assert.Panics(t, func() {
raptor.NewPool(0, nil)
})
})
}
func TestPool(t *testing.T) {
tempPath := filepath.Join(t.TempDir(), "raptor_pool_test")
require.NoError(t, os.MkdirAll(tempPath, os.ModeDir|0755))
t.Cleanup(func() {
os.RemoveAll(tempPath)
})
p := raptor.NewPool(5, func(context.Context) (*raptor.Conn, error) {
return raptor.New(tempPath + "/testing.db?cache=shared")
})
t.Cleanup(func() {
require.NoError(t, p.Close(context.Background()))
})
_, err := p.Exec(context.Background(), `CREATE TABLE "TestTable" ("ID" INTEGER NOT NULL PRIMARY KEY, "Index" INTEGER);`)
require.NoError(t, err)
waitGroup, ctx := errgroup.WithContext(context.Background())
for v := 0; v < 100; v++ {
v := v
waitGroup.Go(func() error {
_, err := p.Exec(ctx, `INSERT INTO "TestTable" ("Index") VALUES (?);`, v)
assert.NoError(t, err)
return nil
})
}
for v := 0; v < 10; v++ {
v := v
waitGroup.Go(func() error {
return p.Transact(ctx, func(conn raptor.DB) error {
_, err := conn.Exec(ctx, `INSERT INTO "TestTable" ("Index") VALUES (?);`, v)
assert.NoError(t, err)
return nil
})
})
}
for v := 0; v < 100; v++ {
waitGroup.Go(func() error {
var count int64
err := p.QueryRow(ctx, `SELECT COUNT(*) FROM "TestTable";`).Scan(&count)
assert.NoError(t, err)
return nil
})
}
for v := 0; v < 8; v++ {
v := v
waitGroup.Go(func() error {
var id int64
err := p.ForWriting(ctx, func(d raptor.DB) error {
return d.QueryRow(ctx, `INSERT INTO "TestTable" ("Index") VALUES (?) RETURNING rowid;`, v).Scan(&id)
})
assert.NoError(t, err)
assert.NotEqual(t, 0, id)
return nil
})
}
waitGroup.Go(func() error {
rows, err := p.Query(ctx, `SELECT COUNT(*) FROM "TestTable";`)
require.NoError(t, err)
defer rows.Close()
for rows.Next() {
}
return nil
})
require.NoError(t, waitGroup.Wait())
t.Run("Pool.QueryRow", func(t *testing.T) {
p := raptor.NewPool(1, func(context.Context) (*raptor.Conn, error) {
return raptor.New("file:testing.db?cache=shared&mode=memory")
})
t.Cleanup(func() {
require.NoError(t, p.Close(context.Background()))
})
_, done, err := p.Reader(context.Background())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, done())
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
t.Cleanup(cancel)
row := p.QueryRow(ctx, `SELECT COUNT(*) FROM "TestTable";`)
assert.ErrorIs(t, row.Err(), context.DeadlineExceeded)
assert.ErrorIs(t, row.Scan(), context.DeadlineExceeded)
_, err = row.Columns()
assert.ErrorIs(t, err, context.DeadlineExceeded)
})
}
func TestPoolReader(t *testing.T) {
p := raptor.NewPool(1, func(context.Context) (*raptor.Conn, error) {
return raptor.New("file:testing.db?cache=shared&mode=memory")
})
t.Cleanup(func() {
require.NoError(t, p.Close(context.Background()))
})
_, done, err := p.Reader(context.Background())
require.NoError(t, err)
t.Cleanup(func() { done() })
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
t.Cleanup(cancel)
_, _, err = p.Reader(ctx)
require.Error(t, err)
}