2008-12-12 17:14:03 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% bdberl: DB API Tests
|
|
|
|
%% Copyright (c) 2008 The Hive. All rights reserved.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
2008-12-12 18:39:51 +00:00
|
|
|
-module(bdberl_SUITE).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
-compile(export_all).
|
|
|
|
|
|
|
|
-include_lib("ct.hrl").
|
|
|
|
|
|
|
|
all() ->
|
|
|
|
[open_should_create_database_if_none_exists,
|
2008-12-12 20:56:00 +00:00
|
|
|
open_should_allow_opening_multiple_databases,
|
|
|
|
close_should_fail_with_invalid_db_handle,
|
2008-12-12 17:14:03 +00:00
|
|
|
get_should_fail_when_getting_a_nonexistant_record,
|
|
|
|
get_should_return_a_value_when_getting_a_valid_record,
|
2008-12-12 20:56:00 +00:00
|
|
|
put_should_succeed_with_manual_transaction,
|
|
|
|
put_should_rollback_with_failed_manual_transaction,
|
2008-12-12 17:14:03 +00:00
|
|
|
transaction_should_commit_on_success,
|
|
|
|
transaction_should_abort_on_exception,
|
|
|
|
transaction_should_abort_on_user_abort,
|
2008-12-12 20:56:00 +00:00
|
|
|
update_should_save_value_if_successful,
|
2008-12-12 21:50:44 +00:00
|
|
|
update_should_accept_args_for_fun,
|
2008-12-15 18:25:03 +00:00
|
|
|
port_should_tune_transaction_timeouts,
|
|
|
|
cursor_should_iterate, cursor_should_fail_if_not_open].
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
init_per_testcase(_TestCase, Config) ->
|
2008-12-12 18:39:51 +00:00
|
|
|
{ok, Db} = bdberl:open("api_test.db", btree, [create, exclusive]),
|
|
|
|
[{db, Db}|Config].
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
end_per_testcase(_TestCase, Config) ->
|
2008-12-12 18:39:51 +00:00
|
|
|
ok = bdberl:close(?config(db, Config)),
|
2008-12-12 17:14:03 +00:00
|
|
|
ok = file:delete("api_test.db").
|
|
|
|
|
|
|
|
|
|
|
|
open_should_create_database_if_none_exists(_Config) ->
|
|
|
|
true = filelib:is_file("api_test.db").
|
|
|
|
|
2008-12-12 20:56:00 +00:00
|
|
|
open_should_allow_opening_multiple_databases(_Config) ->
|
|
|
|
%% Open up another db -- should use dbref 1 as that's the first available
|
|
|
|
{ok, 1} = bdberl:open("api_test2.db", btree).
|
|
|
|
|
|
|
|
close_should_fail_with_invalid_db_handle(_Config) ->
|
|
|
|
{error, invalid_db} = bdberl:close(21000).
|
|
|
|
|
2008-12-12 17:14:03 +00:00
|
|
|
get_should_fail_when_getting_a_nonexistant_record(Config) ->
|
2008-12-12 18:39:51 +00:00
|
|
|
not_found = bdberl:get(?config(db, Config), bad_key).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
get_should_return_a_value_when_getting_a_valid_record(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
2008-12-12 18:39:51 +00:00
|
|
|
ok = bdberl:put(Db, mykey, avalue),
|
|
|
|
{ok, avalue} = bdberl:get(Db, mykey).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
2008-12-12 20:56:00 +00:00
|
|
|
put_should_succeed_with_manual_transaction(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
|
|
|
ok = bdberl:txn_begin(),
|
|
|
|
ok = bdberl:put(Db, mykey, avalue),
|
|
|
|
ok = bdberl:txn_commit(),
|
|
|
|
{ok, avalue} = bdberl:get(Db, mykey).
|
|
|
|
|
|
|
|
put_should_rollback_with_failed_manual_transaction(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
|
|
|
ok = bdberl:txn_begin(),
|
|
|
|
ok = bdberl:put(Db, mykey, avalue),
|
|
|
|
ok = bdberl:txn_abort(),
|
|
|
|
not_found = bdberl:get(Db, mykey).
|
|
|
|
|
2008-12-12 17:14:03 +00:00
|
|
|
transaction_should_commit_on_success(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
2008-12-12 18:39:51 +00:00
|
|
|
F = fun() -> bdberl:put(Db, mykey, avalue) end,
|
|
|
|
{ok, ok} = bdberl:transaction(F),
|
|
|
|
{ok, avalue} = bdberl:get(Db, mykey).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
transaction_should_abort_on_exception(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
|
|
|
|
|
|
|
F = fun() ->
|
2008-12-12 18:39:51 +00:00
|
|
|
bdberl:put(Db, mykey, should_not_see_this),
|
2008-12-12 17:14:03 +00:00
|
|
|
throw(testing)
|
|
|
|
end,
|
|
|
|
|
2008-12-12 18:39:51 +00:00
|
|
|
{error, {transaction_failed, testing}} = bdberl:transaction(F),
|
|
|
|
not_found = bdberl:get(Db, mykey).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
transaction_should_abort_on_user_abort(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
|
|
|
|
|
|
|
F = fun() ->
|
2008-12-12 18:39:51 +00:00
|
|
|
bdberl:put(Db, mykey, should_not_see_this),
|
2008-12-12 17:14:03 +00:00
|
|
|
abort
|
|
|
|
end,
|
|
|
|
|
2008-12-12 18:39:51 +00:00
|
|
|
{error, transaction_aborted} = bdberl:transaction(F),
|
|
|
|
not_found = bdberl:get(Db, mykey).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
update_should_save_value_if_successful(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
2008-12-12 18:39:51 +00:00
|
|
|
ok = bdberl:put(Db, mykey, avalue),
|
2008-12-12 17:14:03 +00:00
|
|
|
|
|
|
|
F = fun(Key, Value) ->
|
|
|
|
mykey = Key,
|
|
|
|
avalue = Value,
|
|
|
|
newvalue
|
|
|
|
end,
|
|
|
|
|
2008-12-12 18:39:51 +00:00
|
|
|
{ok, newvalue} = bdberl:update(Db, mykey, F),
|
|
|
|
{ok, newvalue} = bdberl:get(Db, mykey).
|
2008-12-12 17:14:03 +00:00
|
|
|
|
2008-12-12 21:50:44 +00:00
|
|
|
update_should_accept_args_for_fun(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
|
|
|
ok = bdberl:put(Db, mykey, avalue),
|
|
|
|
|
|
|
|
F = fun(_Key, _Value, Args) ->
|
|
|
|
look_at_me = Args, % This is all we are interested in
|
|
|
|
newvalue
|
|
|
|
end,
|
|
|
|
|
|
|
|
{ok, newvalue} = bdberl:update(Db, mykey, F, look_at_me).
|
|
|
|
|
2008-12-12 20:56:00 +00:00
|
|
|
port_should_tune_transaction_timeouts(_Config) ->
|
|
|
|
%% Test transaction timeouts
|
|
|
|
{ok, 500000} = bdberl:get_txn_timeout(),
|
|
|
|
ok = bdberl:set_txn_timeout(250000),
|
|
|
|
{ok, 250000} = bdberl:get_txn_timeout().
|
2008-12-15 18:25:03 +00:00
|
|
|
|
|
|
|
cursor_should_iterate(Config) ->
|
|
|
|
Db = ?config(db, Config),
|
|
|
|
|
|
|
|
%% Store some sample values in the db
|
|
|
|
ok = bdberl:put(Db, key1, value1),
|
|
|
|
ok = bdberl:put(Db, key2, value2),
|
|
|
|
ok = bdberl:put(Db, key3, value3),
|
|
|
|
|
|
|
|
%% Validate that the cursor returns each value in order (ASSUME btree db)
|
|
|
|
ok = bdberl:cursor_open(Db),
|
|
|
|
{ok, key1, value1} = bdberl:cursor_next(),
|
|
|
|
{ok, key2, value2} = bdberl:cursor_next(),
|
|
|
|
{ok, key3, value3} = bdberl:cursor_next(),
|
|
|
|
not_found = bdberl:cursor_next(),
|
|
|
|
|
|
|
|
%% Validate that the "current" key is key3
|
|
|
|
{ok, key3, value3} = bdberl:cursor_current(),
|
|
|
|
|
|
|
|
%% Now move backwards (should jump to key2, since we are "on" key3)
|
|
|
|
{ok, key2, value2} = bdberl:cursor_prev(),
|
|
|
|
{ok, key1, value1} = bdberl:cursor_prev(),
|
|
|
|
not_found = bdberl:cursor_prev(),
|
|
|
|
|
|
|
|
ok = bdberl:cursor_close().
|
|
|
|
|
|
|
|
cursor_should_fail_if_not_open(Config) ->
|
|
|
|
{error, no_cursor} = bdberl:cursor_next(),
|
|
|
|
{error, no_cursor} = bdberl:cursor_prev(),
|
|
|
|
{error, no_cursor} = bdberl:cursor_current(),
|
|
|
|
{error, no_cursor} = bdberl:cursor_close().
|
|
|
|
|
|
|
|
|
|
|
|
|