Skip to content

Commit c65eacb

Browse files
committed
Refactor test error handling: fix blanket rescues and state threading
Two critical improvements to test/pool_load_test.exs: 1. Replace blanket rescue clauses with specific exception handling (lines 602-623, 695-722) - Changed 'rescue _ -> :ok' to pattern match only ArgumentError and RuntimeError - Unexpected exceptions are logged with ERROR level and re-raised for visibility - Expected exceptions are logged with DEBUG level to aid troubleshooting - Makes failures in close_stmt/1 immediately visible for debugging - Added 'require Logger' to module 2. Add explicit error handling to state threading in transaction test (lines 848-879) - Replaced implicit success assumption in Enum.reduce with Enum.reduce_while - Now explicitly handles insert_edge_case_value/2 failures instead of letting MatchError occur - Captures errors with context (insert_failed reason) instead of crashing - Wrapped entire sequence in 'with' clause to handle errors from either inserts or commit - Makes test failures clearer and prevents masking of real issues Benefits: - Unexpected exceptions now surface for debugging instead of being silently swallowed - Test errors are more informative with explicit error capture and context - Code is more maintainable by showing what exceptions are explicitly expected
1 parent 4f61f06 commit c65eacb

1 file changed

Lines changed: 77 additions & 29 deletions

File tree

test/pool_load_test.exs

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule EctoLibSql.PoolLoadTest do
1313
concurrent access patterns and verify robustness.
1414
"""
1515
use ExUnit.Case
16+
require Logger
1617

1718
alias EctoLibSql
1819

@@ -598,11 +599,27 @@ defmodule EctoLibSql.PoolLoadTest do
598599

599600
{:ok, :prepared_and_cleaned}
600601
after
601-
# Always close the prepared statement, ignore errors
602+
# Always close the prepared statement, catching only expected errors
602603
try do
603604
EctoLibSql.Native.close_stmt(stmt)
604605
rescue
605-
_ -> :ok
606+
e ->
607+
case e do
608+
%ArgumentError{} ->
609+
# Expected exception from close_stmt - log and continue
610+
Logger.debug("Expected error closing prepared statement: #{inspect(e)}")
611+
:ok
612+
613+
%RuntimeError{} ->
614+
# Expected exception from close_stmt - log and continue
615+
Logger.debug("Expected error closing prepared statement: #{inspect(e)}")
616+
:ok
617+
618+
_ ->
619+
# Unexpected exception - re-raise for debugging
620+
Logger.error("Unexpected error closing prepared statement: #{inspect(e)}")
621+
raise e
622+
end
606623
end
607624
end
608625
after
@@ -678,26 +695,42 @@ defmodule EctoLibSql.PoolLoadTest do
678695
{:error, :some_edge_case_inserts_failed}
679696
end
680697
after
681-
# Always close the prepared statement, ignore errors
698+
# Always close the prepared statement, catching only expected errors
682699
try do
683700
EctoLibSql.Native.close_stmt(stmt)
684701
rescue
685-
_ -> :ok
702+
e ->
703+
case e do
704+
%ArgumentError{} ->
705+
# Expected exception from close_stmt - log and continue
706+
Logger.debug("Expected error closing prepared statement: #{inspect(e)}")
707+
:ok
708+
709+
%RuntimeError{} ->
710+
# Expected exception from close_stmt - log and continue
711+
Logger.debug("Expected error closing prepared statement: #{inspect(e)}")
712+
:ok
713+
714+
_ ->
715+
# Unexpected exception - re-raise for debugging
716+
Logger.error("Unexpected error closing prepared statement: #{inspect(e)}")
717+
raise e
718+
end
686719
end
687720
end
688-
after
721+
after
689722
EctoLibSql.disconnect([], state)
690-
end
691-
end)
692-
end)
723+
end
724+
end)
725+
end)
693726

694-
results = Task.await_many(tasks, 30_000)
727+
results = Task.await_many(tasks, 30_000)
695728

696-
# Verify all prepared statement operations succeeded
697-
Enum.each(results, fn result ->
698-
case result do
699-
{:ok, :prepared_with_edge_cases} ->
700-
:ok
729+
# Verify all prepared statement operations succeeded
730+
Enum.each(results, fn result ->
731+
case result do
732+
{:ok, :prepared_with_edge_cases} ->
733+
:ok
701734

702735
{:error, reason} ->
703736
flunk("Prepared statement with edge-case data failed: #{inspect(reason)}")
@@ -812,22 +845,37 @@ defmodule EctoLibSql.PoolLoadTest do
812845
# Insert edge-case values within transaction, threading state through
813846
edge_values = generate_edge_case_values(task_num)
814847

815-
final_trx_state =
816-
Enum.reduce(edge_values, trx_state, fn value, acc_state ->
817-
{:ok, _query, _result, new_state} = insert_edge_case_value(acc_state, value)
818-
new_state
819-
end)
820-
821-
# Slight delay to increase overlap with other transactions
822-
Process.sleep(10)
823-
824-
# Commit the transaction containing all edge-case values
825-
case EctoLibSql.Native.commit(final_trx_state) do
826-
{:ok, _committed_state} ->
827-
{:ok, :committed_with_edge_cases}
828-
848+
# Reduce with explicit error handling to surface failures clearly
849+
with {:ok, final_trx_state} <-
850+
Enum.reduce_while(edge_values, {:ok, trx_state}, fn value, acc ->
851+
case acc do
852+
{:ok, acc_state} ->
853+
case insert_edge_case_value(acc_state, value) do
854+
{:ok, _query, _result, new_state} ->
855+
{:cont, {:ok, new_state}}
856+
857+
{:error, _query, reason, _state} ->
858+
{:halt, {:error, {:insert_failed, reason}}}
859+
end
860+
861+
error ->
862+
{:halt, error}
863+
end
864+
end) do
865+
# Slight delay to increase overlap with other transactions
866+
Process.sleep(10)
867+
868+
# Commit the transaction containing all edge-case values
869+
case EctoLibSql.Native.commit(final_trx_state) do
870+
{:ok, _committed_state} ->
871+
{:ok, :committed_with_edge_cases}
872+
873+
{:error, reason} ->
874+
{:error, {:commit_failed, reason}}
875+
end
876+
else
829877
{:error, reason} ->
830-
{:error, {:commit_failed, reason}}
878+
{:error, reason}
831879
end
832880
after
833881
EctoLibSql.disconnect([], state)

0 commit comments

Comments
 (0)