| 1 |
// Copyright (c) 2024 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package dbutil_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"fmt" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
|
| 18 |
"go.mau.fi/util/dbutil" |
| 19 |
_ "go.mau.fi/util/dbutil/litestream" |
| 20 |
) |
| 21 |
|
| 22 |
func initTestDB(t *testing.T) *dbutil.Database { |
| 23 |
db, err := dbutil.NewFromConfig("", dbutil.Config{ |
| 24 |
PoolConfig: dbutil.PoolConfig{ |
| 25 |
Type: "sqlite3-fk-wal", |
| 26 |
URI: ":memory:?_txlock=immediate", |
| 27 |
MaxOpenConns: 1, |
| 28 |
MaxIdleConns: 1, |
| 29 |
}, |
| 30 |
DeadlockDetection: true, |
| 31 |
}, nil) |
| 32 |
require.NoError(t, err) |
| 33 |
ctx := context.Background() |
| 34 |
_, err = db.Exec(ctx, ` |
| 35 |
CREATE TABLE meow (id INTEGER PRIMARY KEY, value TEXT); |
| 36 |
INSERT INTO meow (id, value) VALUES (1, 'meow'); |
| 37 |
INSERT INTO meow (id, value) VALUES (2, 'meow 2'); |
| 38 |
INSERT INTO meow (value) VALUES ('meow 3'); |
| 39 |
`) |
| 40 |
require.NoError(t, err) |
| 41 |
return db |
| 42 |
} |
| 43 |
|
| 44 |
func getMeow(ctx context.Context, db dbutil.Execable, id int) (value string, err error) { |
| 45 |
err = db.QueryRowContext(ctx, "SELECT value FROM meow WHERE id = ?", id).Scan(&value) |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
func TestDatabase_NoDeadlock(t *testing.T) { |
| 50 |
db := initTestDB(t) |
| 51 |
ctx := context.Background() |
| 52 |
require.NoError(t, db.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 53 |
_, err := db.Exec(ctx, "INSERT INTO meow (value) VALUES ('meow 4');") |
| 54 |
require.NoError(t, err) |
| 55 |
return nil |
| 56 |
})) |
| 57 |
val, err := getMeow(ctx, db.Execable(ctx), 4) |
| 58 |
require.NoError(t, err) |
| 59 |
require.Equal(t, "meow 4", val) |
| 60 |
} |
| 61 |
|
| 62 |
func TestDatabase_NoDeadlock_Goroutine(t *testing.T) { |
| 63 |
db := initTestDB(t) |
| 64 |
ctx := context.Background() |
| 65 |
require.NoError(t, db.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 66 |
_, err := db.Exec(ctx, "INSERT INTO meow (value) VALUES ('meow 4');") |
| 67 |
require.NoError(t, err) |
| 68 |
go func() { |
| 69 |
_, err := db.Exec(context.Background(), "INSERT INTO meow (value) VALUES ('meow 5');") |
| 70 |
require.NoError(t, err) |
| 71 |
}() |
| 72 |
time.Sleep(50 * time.Millisecond) |
| 73 |
return nil |
| 74 |
})) |
| 75 |
val, err := getMeow(ctx, db.Execable(ctx), 4) |
| 76 |
require.NoError(t, err) |
| 77 |
require.Equal(t, "meow 4", val) |
| 78 |
val, err = getMeow(ctx, db.Execable(ctx), 5) |
| 79 |
require.NoError(t, err) |
| 80 |
require.Equal(t, "meow 5", val) |
| 81 |
} |
| 82 |
|
| 83 |
func TestDatabase_Deadlock(t *testing.T) { |
| 84 |
db := initTestDB(t) |
| 85 |
ctx := context.Background() |
| 86 |
_ = db.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 87 |
assert.PanicsWithError(t, dbutil.ErrQueryDeadlock.Error(), func() { |
| 88 |
_, _ = db.Exec(context.Background(), "INSERT INTO meow (value) VALUES ('meow 4');") |
| 89 |
}) |
| 90 |
return fmt.Errorf("meow") |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
func TestDatabase_Deadlock_Acquire(t *testing.T) { |
| 95 |
db := initTestDB(t) |
| 96 |
ctx := context.Background() |
| 97 |
_ = db.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 98 |
assert.PanicsWithError(t, dbutil.ErrAcquireDeadlock.Error(), func() { |
| 99 |
_, _ = db.AcquireConn(context.Background()) |
| 100 |
}) |
| 101 |
return fmt.Errorf("meow") |
| 102 |
}) |
| 103 |
} |
| 104 |
|
| 105 |
func TestDatabase_Deadlock_Txn(t *testing.T) { |
| 106 |
db := initTestDB(t) |
| 107 |
ctx := context.Background() |
| 108 |
_ = db.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 109 |
assert.PanicsWithError(t, dbutil.ErrTransactionDeadlock.Error(), func() { |
| 110 |
_ = db.DoTxn(context.Background(), nil, func(ctx context.Context) error { |
| 111 |
return nil |
| 112 |
}) |
| 113 |
}) |
| 114 |
return fmt.Errorf("meow") |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
func TestDatabase_Deadlock_Child(t *testing.T) { |
| 119 |
db := initTestDB(t) |
| 120 |
ctx := context.Background() |
| 121 |
childDB := db.Child("", nil, nil) |
| 122 |
_ = db.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 123 |
assert.PanicsWithError(t, dbutil.ErrQueryDeadlock.Error(), func() { |
| 124 |
_, _ = childDB.Exec(context.Background(), "INSERT INTO meow (value) VALUES ('meow 4');") |
| 125 |
}) |
| 126 |
return fmt.Errorf("meow") |
| 127 |
}) |
| 128 |
} |
| 129 |
|
| 130 |
func TestDatabase_Deadlock_Child2(t *testing.T) { |
| 131 |
db := initTestDB(t) |
| 132 |
ctx := context.Background() |
| 133 |
childDB := db.Child("", nil, nil) |
| 134 |
_ = childDB.DoTxn(ctx, nil, func(ctx context.Context) error { |
| 135 |
assert.PanicsWithError(t, dbutil.ErrQueryDeadlock.Error(), func() { |
| 136 |
_, _ = db.Exec(context.Background(), "INSERT INTO meow (value) VALUES ('meow 4');") |
| 137 |
}) |
| 138 |
return fmt.Errorf("meow") |
| 139 |
}) |
| 140 |
} |