Repository navigation
Expand file tree
/
Copy pathbusy_test.go
More file actions
58 lines (51 loc) · 1.83 KB
/
Copy pathbusy_test.go
File metadata and controls
58 lines (51 loc) · 1.83 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
// SPDX-License-Identifier: Apache-2.0
package sqlite_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/gopherium/framework/dbkit"
"github.com/gopherium/framework/dbkit/sqlite/sqlitetest"
"github.com/gopherium/framework/gonsole"
"github.com/gopherium/framework/gonsole/auth"
"github.com/gopherium/framework/gonsole/testkit"
)
func TestRecordWaitsNoLongerThanTheBusyTimeout(t *testing.T) {
t.Parallel()
opts := testOptions()
opts.BusyTimeout = 2 * time.Second
opts.MaxConns = 3
db := sqlitetest.Open(t, opts)
applied(t, db)
writer, err := db.BeginTx(t.Context(), nil)
if err != nil {
t.Fatalf("beginning the writer: %v", err)
}
t.Cleanup(func() { _ = writer.Rollback() })
if _, err := writer.ExecContext(t.Context(), "CREATE TABLE core_items (id TEXT NOT NULL PRIMARY KEY)"); err != nil {
t.Fatalf("writing inside the writer: %v", err)
}
cfg := testConfig()
cfg.RecordTimeout = 20 * time.Millisecond
call := gonsole.Call{
Env: gonsole.Env{Prefix: "MYAPP_", Getenv: testkit.Getenv(nil)}, Actor: "admin@example.com",
Args: []string{"author@example.com"},
}.WithDB(db)
start := time.Now()
err = auth.Record(cfg)(context.Background(), call, "account:disable")
waited := time.Since(start)
if !strings.HasPrefix(fmt.Sprint(err), "record account:disable: sqlite: insert a record: ") ||
!errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Record() behind a held write = %v, want the record's deadline under the insert", err)
}
if errors.Is(err, dbkit.ErrBusy) {
t.Errorf("Record() behind a held write = %v, want the record's deadline before the busy timeout", err)
}
if waited < cfg.RecordTimeout || waited >= 2*opts.BusyTimeout {
t.Errorf("Record() returned after %v, want at least the record timeout %v and under twice the busy timeout %v",
waited, cfg.RecordTimeout, opts.BusyTimeout)
}
}