Repository navigation
Expand file tree
/
Copy pathqueue_merge_test.go
More file actions
148 lines (141 loc) · 3.91 KB
/
Copy pathqueue_merge_test.go
File metadata and controls
148 lines (141 loc) · 3.91 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
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
// Copyright (c) 2026 The liustack/sqlite Authors.
// SPDX-License-Identifier: MIT
// Use of this source code is governed by the MIT license in LICENSE at the
// root of this repository.
package sqlite
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
mq "github.com/liustack/sqlite/queue"
)
// The statement of Publish is one group commit merges: with many goroutines
// publishing, they share commits and no call gets database is locked. So it
// is with named subscriptions, where the statement writes a row for each,
// and with a key, where it reads the table first.
func TestQueueMerges(t *testing.T) {
for _, c := range []struct {
name string
subs []string
key bool
}{
{name: "default subscription"},
{name: "subscriptions", subs: []string{"a", "b", "c"}},
{name: "keys", key: true},
} {
t.Run(c.name, func(t *testing.T) {
db := mOpen(t, "file:"+mPath(t))
db.SetMaxOpenConns(64)
var opts []mq.Option
if c.subs != nil {
opts = append(opts, mq.Subscriptions(c.subs...))
}
q, err := mq.New(t.Context(), db, "jobs", opts...)
if err != nil {
t.Fatal(err)
}
sc, err := db.Conn(t.Context())
if err != nil {
t.Fatal(err)
}
var wl *writeLock
sc.Raw(func(dc any) error { wl = dc.(*conn).wl; return nil })
sc.Close()
commits0, merged0 := wl.stats.commits.Load(), wl.stats.merged.Load()
d := time.Second
if testing.Short() {
d = 200 * time.Millisecond
}
ctx, cancel := context.WithTimeout(t.Context(), d)
defer cancel()
var published atomic.Int64
var wg sync.WaitGroup
for g := range 64 {
wg.Go(func() {
for i := 0; ctx.Err() == nil; i++ {
var popts []mq.PublishOption
if c.key {
popts = append(popts, mq.Dedup(fmt.Sprint(g, "-", i)))
}
if err := q.Publish(context.Background(), []byte("x"), popts...); err != nil {
t.Errorf("Publish: %v", err)
return
}
published.Add(1)
}
})
}
wg.Wait()
n := published.Load()
commits, merged := wl.stats.commits.Load()-commits0, wl.stats.merged.Load()-merged0
t.Logf("%d messages in %v: %d of them were merged into %d commits", n, d, merged, commits)
if merged < n/2 {
t.Errorf("%d of %d Publish calls merged: the statement of Publish does not merge", merged, n)
}
rows := n * int64(max(1, len(c.subs)))
if got := mCount(t, db, `SELECT count(*) FROM _liustack_queue`); int64(got) != rows {
t.Errorf("%d rows, want %d", got, rows)
}
})
}
}
// The take of a Consume is a write the driver keeps on its connection: SQLite
// does not compile it again for its arguments, and so a take costs no
// compile. The take is the write of the queue with UNION ALL.
func TestQueueTakeKept(t *testing.T) {
db := mOpen(t, "file:"+mPath(t))
db.SetMaxOpenConns(2)
q, err := mq.New(t.Context(), db, "jobs")
if err != nil {
t.Fatal(err)
}
const n = 5
for range n {
if err := q.Publish(t.Context(), []byte("x")); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
var handled atomic.Int64
err = q.Consume(ctx, func(context.Context, *mq.Message) error {
if handled.Add(1) == n {
cancel()
}
return nil
}, mq.Workers(1))
if err != nil && !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
// The connections of the pool, each with the writes it keeps.
var kept []string
for _, sc := range []*sql.Conn{mustConn(t, db), mustConn(t, db)} {
sc.Raw(func(dc any) error {
for k := range dc.(*conn).writes {
kept = append(kept, k)
}
return nil
})
defer sc.Close()
}
for _, k := range kept {
if strings.HasPrefix(strings.TrimSpace(k), "UPDATE") && strings.Contains(k, "UNION ALL") {
return
}
}
t.Errorf("no connection keeps the take; they keep %d writes", len(kept))
}
func mustConn(t *testing.T, db *sql.DB) *sql.Conn {
t.Helper()
c, err := db.Conn(t.Context())
if err != nil {
t.Fatal(err)
}
return c
}