|
8 | 8 | package sqlite3
|
9 | 9 |
|
10 | 10 | import (
|
| 11 | + "context" |
11 | 12 | "database/sql"
|
| 13 | + "fmt" |
| 14 | + "math/rand" |
12 | 15 | "os"
|
13 | 16 | "testing"
|
| 17 | + "time" |
14 | 18 | )
|
15 | 19 |
|
16 | 20 | func TestNamedParams(t *testing.T) {
|
@@ -48,3 +52,91 @@ func TestNamedParams(t *testing.T) {
|
48 | 52 | t.Error("Failed to db.QueryRow: not matched results")
|
49 | 53 | }
|
50 | 54 | }
|
| 55 | + |
| 56 | +var ( |
| 57 | + testTableStatements = []string{ |
| 58 | + `DROP TABLE IF EXISTS test_table`, |
| 59 | + ` |
| 60 | +CREATE TABLE IF NOT EXISTS test_table ( |
| 61 | + key1 VARCHAR(64) PRIMARY KEY, |
| 62 | + key_id VARCHAR(64) NOT NULL, |
| 63 | + key2 VARCHAR(64) NOT NULL, |
| 64 | + key3 VARCHAR(64) NOT NULL, |
| 65 | + key4 VARCHAR(64) NOT NULL, |
| 66 | + key5 VARCHAR(64) NOT NULL, |
| 67 | + key6 VARCHAR(64) NOT NULL, |
| 68 | + data BLOB NOT NULL |
| 69 | +);`, |
| 70 | + } |
| 71 | + letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 72 | +) |
| 73 | + |
| 74 | +func randStringBytes(n int) string { |
| 75 | + b := make([]byte, n) |
| 76 | + for i := range b { |
| 77 | + b[i] = letterBytes[rand.Intn(len(letterBytes))] |
| 78 | + } |
| 79 | + return string(b) |
| 80 | +} |
| 81 | + |
| 82 | +func initDatabase(t *testing.T, db *sql.DB, rowCount int64) { |
| 83 | + t.Logf("Executing db initializing statements") |
| 84 | + for _, query := range testTableStatements { |
| 85 | + _, err := db.Exec(query) |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + } |
| 90 | + for i := int64(0); i < rowCount; i++ { |
| 91 | + query := `INSERT INTO test_table |
| 92 | + (key1, key_id, key2, key3, key4, key5, key6, data) |
| 93 | + VALUES |
| 94 | + (?, ?, ?, ?, ?, ?, ?, ?);` |
| 95 | + args := []interface{}{ |
| 96 | + randStringBytes(50), |
| 97 | + fmt.Sprint(i), |
| 98 | + randStringBytes(50), |
| 99 | + randStringBytes(50), |
| 100 | + randStringBytes(50), |
| 101 | + randStringBytes(50), |
| 102 | + randStringBytes(50), |
| 103 | + randStringBytes(50), |
| 104 | + randStringBytes(2048), |
| 105 | + } |
| 106 | + _, err := db.Exec(query, args...) |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestShortTimeout(t *testing.T) { |
| 114 | + db, err := sql.Open("sqlite3", "file::memory:?mode=memory&cache=shared") |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + defer db.Close() |
| 119 | + initDatabase(t, db, 10000) |
| 120 | + |
| 121 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Microsecond) |
| 122 | + defer cancel() |
| 123 | + query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data |
| 124 | + FROM test_table |
| 125 | + ORDER BY key2 ASC` |
| 126 | + rows, err := db.QueryContext(ctx, query) |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + defer rows.Close() |
| 131 | + for rows.Next() { |
| 132 | + var key1, keyid, key2, key3, key4, key5, key6 string |
| 133 | + var data []byte |
| 134 | + err = rows.Scan(&key1, &keyid, &key2, &key3, &key4, &key5, &key6, &data) |
| 135 | + if err != nil { |
| 136 | + break |
| 137 | + } |
| 138 | + } |
| 139 | + if context.DeadlineExceeded != ctx.Err() { |
| 140 | + t.Fatal(ctx.Err()) |
| 141 | + } |
| 142 | +} |
0 commit comments