|
| 1 | +# 2026 libsql |
| 2 | +# |
| 3 | +# The author disclaims copyright to this source code. In place of |
| 4 | +# a legal notice, here is a blessing: |
| 5 | +# |
| 6 | +# May you do good and not evil. |
| 7 | +# May you find forgiveness for yourself and forgive others. |
| 8 | +# May you share freely, never taking more than you give. |
| 9 | +# |
| 10 | +#*********************************************************************** |
| 11 | +# This file implements regression tests for the libsql_stmt_interrupt() |
| 12 | +# API, which interrupts a single prepared statement rather than every |
| 13 | +# statement on the connection the way sqlite3_interrupt() does. |
| 14 | +# |
| 15 | +# The interesting property is that an interrupt requested while a statement |
| 16 | +# is *executing* (inside sqlite3_step()) is observed from within the VDBE |
| 17 | +# loop, not merely at the next entry to sqlite3_step(). Like interrupt.test, |
| 18 | +# this is exercised deterministically via the ::sqlite_stmt_interrupt_count |
| 19 | +# test variable: when positive it is decremented once per VDBE opcode and, |
| 20 | +# on reaching zero, fires libsql_stmt_interrupt() on the running statement. |
| 21 | + |
| 22 | +set testdir [file dirname $argv0] |
| 23 | +source $testdir/tester.tcl |
| 24 | +set DB [sqlite3_connection_pointer db] |
| 25 | + |
| 26 | +# A table large enough that a scan/join loops through the VDBE's |
| 27 | +# check_for_interrupt label far more than 100 times, so that the small |
| 28 | +# trigger values below all land mid-execution. (Note: the queries below scan |
| 29 | +# the rows -- e.g. sum(a) -- rather than bare count(*), which the planner |
| 30 | +# satisfies with a single OP_Count opcode and never loops.) |
| 31 | +do_test interruptstmt-1.0 { |
| 32 | + execsql { |
| 33 | + CREATE TABLE t1(a); |
| 34 | + WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<400) |
| 35 | + INSERT INTO t1 SELECT i FROM c; |
| 36 | + SELECT count(*) FROM t1; |
| 37 | + } |
| 38 | +} {400} |
| 39 | + |
| 40 | +# Run $sql with the statement-interrupt trigger armed to fire after $n VDBE |
| 41 | +# opcodes. Returns a list of {errorcode catchsql-result}. |
| 42 | +proc run_with_trigger {sql n} { |
| 43 | + set ::sqlite_stmt_interrupt_count $n |
| 44 | + set r [catchsql $sql] |
| 45 | + set ::sqlite_stmt_interrupt_count 0 |
| 46 | + list [db errorcode] $r |
| 47 | +} |
| 48 | + |
| 49 | +# For a statement that runs many opcodes, arming the trigger at a low count |
| 50 | +# must abort it mid-execution with SQLITE_INTERRUPT -- and must never set the |
| 51 | +# connection-wide interrupt flag (that is the whole point of doing this at the |
| 52 | +# statement level rather than with sqlite3_interrupt()). |
| 53 | +foreach {tn sql} { |
| 54 | + 1 {SELECT sum(a) FROM t1} |
| 55 | + 2 {SELECT count(*) FROM t1 a, t1 b} |
| 56 | +} { |
| 57 | + foreach n {1 2 5 20 100} { |
| 58 | + do_test interruptstmt-2.$tn.$n.rc { |
| 59 | + run_with_trigger $sql $n |
| 60 | + } {9 {1 interrupted}} |
| 61 | + do_test interruptstmt-2.$tn.$n.flag { |
| 62 | + sqlite3_is_interrupted $DB |
| 63 | + } 0 |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +# With the trigger set higher than the whole program, the statement runs to |
| 68 | +# completion and returns the correct result. |
| 69 | +do_test interruptstmt-3.1 { |
| 70 | + run_with_trigger {SELECT sum(a) FROM t1} 1000000 |
| 71 | +} {0 {0 80200}} |
| 72 | +do_test interruptstmt-3.2 { |
| 73 | + run_with_trigger {SELECT count(*) FROM t1 a, t1 b} 100000000 |
| 74 | +} {0 {0 160000}} |
| 75 | + |
| 76 | +# The checks above confirm the statement ends in SQLITE_INTERRUPT, but a |
| 77 | +# trailing sqlite3_step() would report that at step entry even if the running |
| 78 | +# step had been allowed to finish first. To prove the interrupt is honored |
| 79 | +# *mid-execution* (the in-loop check, not just the step-entry check), count how |
| 80 | +# many rows the scan actually visited via a Tcl function and confirm it stopped |
| 81 | +# well short of the full table. |
| 82 | +set ::nrows 0 |
| 83 | +proc rowtick {} { incr ::nrows; return 1 } |
| 84 | +db function rowtick -argcount 0 rowtick |
| 85 | + |
| 86 | +do_test interruptstmt-5.1 { |
| 87 | + set ::nrows 0 |
| 88 | + set r [run_with_trigger {SELECT count(*) FROM t1 WHERE rowtick()} 50] |
| 89 | + list $r [expr {$::nrows>0 && $::nrows<400}] |
| 90 | +} {{9 {1 interrupted}} 1} |
| 91 | + |
| 92 | +# Sanity check the instrument: without an interrupt the same scan visits every |
| 93 | +# row. |
| 94 | +do_test interruptstmt-5.2 { |
| 95 | + set ::nrows 0 |
| 96 | + set r [run_with_trigger {SELECT count(*) FROM t1 WHERE rowtick()} 100000000] |
| 97 | + list $r $::nrows |
| 98 | +} {{0 {0 400}} 400} |
| 99 | + |
| 100 | +# After all of the above the connection must be left un-interrupted, and a |
| 101 | +# fresh statement runs normally. |
| 102 | +do_test interruptstmt-4.1 { |
| 103 | + sqlite3_is_interrupted $DB |
| 104 | +} 0 |
| 105 | +do_test interruptstmt-4.2 { |
| 106 | + execsql {SELECT count(*) FROM t1} |
| 107 | +} {400} |
| 108 | + |
| 109 | +finish_test |
0 commit comments