From e4edf17f173d370767b7a5792b8e9033c83f44ce Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Fri, 19 Nov 2010 10:12:39 +0300 Subject: [PATCH] Fixed 'concurrent' inserts test --- test/sqlite3_test.erl | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/test/sqlite3_test.erl b/test/sqlite3_test.erl index a877642..8f1ffcc 100644 --- a/test/sqlite3_test.erl +++ b/test/sqlite3_test.erl @@ -145,15 +145,29 @@ select_many_records() -> 1024, length(rows(sqlite3:sql_exec(ct, "select * from many_records;")))). -%% concurrent_inserts_test() -> -%% sqlite3:open(concurrent, [in_memory]), %% doing this test not in memory is much slower! -%% drop_table_if_exists(concurrent, t), -%% sqlite3:create_table(concurrent, t, [{id, integer}]), -%% [spawn(fun () -> sqlite3:write(concurrent, t, [{id, X}]) end) || X <- lists:seq(1, 1024)], -%% ?assertEqual( -%% 1024, -%% length(rows(sqlite3:read_all(concurrent, t)))), -%% sqlite3:close(concurrent). %% doesn't pass at the moment! +%% note that inserts are actually serialized by gen_server +concurrent_inserts_test() -> + N = 1024, + sqlite3:open(concurrent, [in_memory]), %% doing this test not in memory is much slower! + drop_table_if_exists(concurrent, t), + sqlite3:create_table(concurrent, t, [{id0, integer}]), + Self = self(), + [spawn(fun () -> + sqlite3:write(concurrent, t, [{id0, X}]), + Self ! {finished, N} + end) || X <- lists:seq(1, N)], + loop_concurrent_inserts(N), + ?assertEqual( + N, length(rows(sqlite3:read_all(concurrent, t)))), + sqlite3:close(concurrent). + +loop_concurrent_inserts(0) -> + ok; +loop_concurrent_inserts(N) -> + receive + {finished, _} -> + loop_concurrent_inserts(N - 1) + end. nonexistent_table_info() -> ?assertEqual(table_does_not_exist, sqlite3:table_info(ct, nonexistent)).