Skip to content

Commit 34938b9

Browse files
committed
Improve test robustness and error handling
cursor_streaming_large_test.exs: - Add error handling to fetch_all_ids_acc, fetch_all_cursor_rows_acc, and count_batches to report failures clearly instead of CaseClauseError - Wrap insert_rows in try/after to ensure prepared statement cleanup on error - Fix on_exit to use conn_id instead of potentially stale state pool_load_test.exs: - Capture updated state from handle_execute and use conn_id in on_exit for reliable connection cleanup
1 parent cbffa16 commit 34938b9

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

test/cursor_streaming_large_test.exs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
88

99
setup do
1010
{:ok, state} = EctoLibSql.connect(database: ":memory:")
11+
conn_id = state.conn_id
1112

1213
# Create a test table for large data
1314
{:ok, _, _, state} =
@@ -27,7 +28,8 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
2728
)
2829

2930
on_exit(fn ->
30-
EctoLibSql.disconnect([], state)
31+
# Use conn_id to ensure we disconnect the correct connection
32+
EctoLibSql.disconnect([], %{conn_id: conn_id})
3133
end)
3234

3335
{:ok, state: state}
@@ -687,7 +689,7 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
687689
"INSERT INTO large_data (id, batch_id, sequence, value) VALUES (?, ?, ?, ?)"
688690
)
689691

690-
state =
692+
try do
691693
Enum.reduce(start_id..end_id, state, fn id, acc_state ->
692694
value = "value_#{id}_batch_#{batch_id}"
693695

@@ -701,10 +703,10 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
701703

702704
acc_state
703705
end)
704-
705-
# Clean up prepared statement
706-
:ok = EctoLibSql.Native.close_stmt(stmt)
707-
state
706+
after
707+
# Always clean up prepared statement, even on error
708+
EctoLibSql.Native.close_stmt(stmt)
709+
end
708710
end
709711

710712
defp fetch_all_rows(state, cursor, query, opts) do
@@ -751,6 +753,9 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
751753
{:halt, result, _state} ->
752754
ids = Enum.map(result.rows, fn [id] -> id end)
753755
[ids | acc]
756+
757+
{:error, reason, _state} ->
758+
flunk("Cursor fetch failed in fetch_all_ids_acc: #{inspect(reason)}")
754759
end
755760
end
756761

@@ -770,6 +775,9 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
770775

771776
{:halt, result, _state} ->
772777
[result.rows | acc]
778+
779+
{:error, reason, _state} ->
780+
flunk("Cursor fetch failed in fetch_all_cursor_rows_acc: #{inspect(reason)}")
773781
end
774782
end
775783

@@ -797,6 +805,9 @@ defmodule EctoLibSql.CursorStreamingLargeTest do
797805

798806
{:halt, _result, _state} ->
799807
1
808+
809+
{:error, reason, _state} ->
810+
flunk("Cursor fetch failed in count_batches: #{inspect(reason)}")
800811
end
801812
end
802813
end

test/pool_load_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ defmodule EctoLibSql.PoolLoadTest do
2222
# Create test table
2323
{:ok, state} = EctoLibSql.connect(database: test_db)
2424

25-
{:ok, _query, _result, _state} =
25+
{:ok, _query, _result, state} =
2626
EctoLibSql.handle_execute(
2727
"CREATE TABLE test_data (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, duration INTEGER)",
2828
[],
2929
[],
3030
state
3131
)
3232

33+
# Capture conn_id for reliable cleanup
34+
conn_id = state.conn_id
35+
3336
on_exit(fn ->
34-
EctoLibSql.disconnect([], state)
37+
EctoLibSql.disconnect([], %{conn_id: conn_id})
3538
EctoLibSql.TestHelpers.cleanup_db_files(test_db)
3639
end)
3740

0 commit comments

Comments
 (0)