Skip to content

Commit

Permalink
Test rdbms casts
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 4, 2022
1 parent efa17e5 commit 73e02d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
24 changes: 21 additions & 3 deletions big_tests/tests/rdbms_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
-module(rdbms_SUITE).
-compile([export_all, nowarn_export_all]).

-include_lib("escalus/include/escalus.hrl").
-include_lib("common_test/include/ct.hrl").
-include_lib("exml/include/exml.hrl").
-include_lib("eunit/include/eunit.hrl").

%% We need assert from it
-include("mam_helper.hrl").
Expand Down Expand Up @@ -67,6 +65,7 @@ rdbms_queries_cases() ->
select_like_prep_case,

insert_batch_with_null_case,
test_cast_insert,
arguments_from_two_tables].

suite() ->
Expand Down Expand Up @@ -341,6 +340,19 @@ insert_batch_with_null_case(Config) ->
?assert_equal({selected, [{null}, {null}, {<<"check1">>}, {<<"check2">>}]},
selected_to_sorted(SelectResult)).

test_cast_insert(Config) ->
erase_table(Config),
sql_prepare(Config, insert_one, test_types, [unicode],
<<"INSERT INTO test_types(unicode) VALUES (?)">>),
sql_execute_cast(Config, insert_one, [<<"check1">>]),
sql_query_cast(Config, <<"INSERT INTO test_types(unicode) VALUES ('check2')">>),
mongoose_helper:wait_until(
fun() ->
SelectResult = sql_query(Config, "SELECT unicode FROM test_types"),
?assertEqual({selected, [{<<"check1">>}, {<<"check2">>}]},
selected_to_sorted(SelectResult))
end, ok, #{name => cast_queries}).

%%--------------------------------------------------------------------
%% Text searching
%%--------------------------------------------------------------------
Expand All @@ -365,6 +377,12 @@ sql_prepare(_Config, Name, Table, Fields, Query) ->
sql_execute(_Config, Name, Parameters) ->
slow_rpc(mongoose_rdbms, execute, [host_type(), Name, Parameters]).

sql_execute_cast(_Config, Name, Parameters) ->
slow_rpc(mongoose_rdbms, execute_cast, [host_type(), Name, Parameters]).

sql_query_cast(_Config, Query) ->
slow_rpc(mongoose_rdbms, sql_query_cast, [host_type(), Query]).

escape_null(_Config) ->
escalus_ejabberd:rpc(mongoose_rdbms, escape_null, []).

Expand Down
37 changes: 18 additions & 19 deletions src/rdbms/mongoose_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -547,17 +547,22 @@ init(Opts) ->


handle_call({sql_cmd, Command, Timestamp}, From, State) ->
run_sql_cmd(Command, From, State, Timestamp);
{Result, NewState} = run_sql_cmd(Command, From, State, Timestamp),
case abort_on_driver_error(Result) of
{stop, Reason} -> {stop, Reason, Result, NewState};
continue -> {reply, Result, NewState}
end;
handle_call(get_db_info, _, #state{db_ref = DbRef} = State) ->
{reply, {ok, db_engine(global), DbRef}, State};
handle_call(Request, From, State) ->
?UNEXPECTED_CALL(Request, From),
{reply, {error, badarg}, State}.

handle_cast({sql_cmd, Command, Timestamp}, State) ->
case run_sql_cmd(Command, undefined, State, Timestamp) of
{reply, _, NewState} -> {noreply, NewState};
Other -> Other
{Result, NewState} = run_sql_cmd(Command, undefined, State, Timestamp),
case abort_on_driver_error(Result) of
{stop, Reason} -> {stop, Reason, NewState};
continue -> {noreply, NewState}
end;
handle_cast(Request, State) ->
?UNEXPECTED_CAST(Request),
Expand Down Expand Up @@ -596,13 +601,12 @@ print_state(State) ->
%%%----------------------------------------------------------------------

-spec run_sql_cmd(Command :: any(), From :: any(), State :: state(), Timestamp :: integer()) ->
{reply, Reply :: any(), state()} | {stop, Reason :: term(), state()} |
{noreply, state()}.
{Result :: term(), state()}.
run_sql_cmd(Command, _From, State, Timestamp) ->
Now = erlang:monotonic_time(millisecond),
case Now - Timestamp of
Age when Age < ?TRANSACTION_TIMEOUT ->
abort_on_driver_error(outer_op(Command, State));
outer_op(Command, State);
Age ->
?LOG_ERROR(#{what => rdbms_db_not_available_or_too_slow,
text => <<"Discarding request">>, age => Age, command => Command}),
Expand Down Expand Up @@ -753,18 +757,13 @@ lookup_statement(Name) ->
[] -> error({lookup_statement_failed, Name})
end.

%% @doc Generate the OTP callback return tuple depending on the driver result.
-spec abort_on_driver_error({_, state()}) ->
{reply, Reply :: term(), state()} |
{stop, timeout | closed, state()}.
abort_on_driver_error({{error, "query timed out"} = Reply, State}) ->
%% mysql driver error
{stop, timeout, Reply, State};
abort_on_driver_error({{error, "Failed sending data on socket" ++ _} = Reply, State}) ->
%% mysql driver error
{stop, closed, Reply, State};
abort_on_driver_error({Reply, State}) ->
{reply, Reply, State}.
-spec abort_on_driver_error(_) -> continue | {stop, timeout | closed}.
abort_on_driver_error({error, "query timed out"}) -> %% mysql driver error
{stop, timeout};
abort_on_driver_error({error, "Failed sending data on socket" ++ _}) -> %% mysql driver error
{stop, closed};
abort_on_driver_error(_) ->
continue.

-spec db_engine(HostType :: server()) -> odbc | mysql | pgsql | undefined.
db_engine(_HostType) ->
Expand Down

0 comments on commit 73e02d9

Please sign in to comment.