diff --git a/c_src/bdberl_drv.c b/c_src/bdberl_drv.c index d7e9bd6..4e54707 100644 --- a/c_src/bdberl_drv.c +++ b/c_src/bdberl_drv.c @@ -34,6 +34,7 @@ static void* zalloc(unsigned int size); */ static DB_ENV* G_DB_ENV; + /** * Global variable to track the return code from opening the DB_ENV. We track this * value so as to provide a useful error code when the user attempts to open the @@ -162,8 +163,7 @@ static void bdberl_drv_finish() { // Driver is unloading -- cleanup and shut down the BDB environment. Note that we assume // all ports have been released and thuse all databases/txns/etc are also gone. - G_DB_ENV->close(G_DB_ENV, 0); - + G_DB_ENV->close(G_DB_ENV, 0); driver_free(G_DATABASES); erl_drv_rwlock_destroy(G_DATABASES_RWLOCK); hive_hash_destroy(G_DATABASES_NAMES); diff --git a/test/port_SUITE.erl b/test/port_SUITE.erl new file mode 100644 index 0000000..fd8564f --- /dev/null +++ b/test/port_SUITE.erl @@ -0,0 +1,48 @@ +%% ------------------------------------------------------------------- +%% +%% bdberl: Port Tests +%% Copyright (c) 2008 The Hive. All rights reserved. +%% +%% ------------------------------------------------------------------- +-module(port_SUITE). + +-compile(export_all). + +all() -> + [test_db]. + +init_per_testcase(TestCase, Config) -> + Config. + +end_per_testcase(TestCase, _Config) -> + ok. + + +test_db(_Config) -> + {ok, P} = bdberl_port:new(), + + %% Create two databases + {ok, 0} = bdberl_port:open_database(P, "test1", hash), + {ok, 1} = bdberl_port:open_database(P, "test2", btree), + + %% Open another port and open the same databases in reverse order. The ref system should + %% ensure that the databases return the same refs as previously + {ok, P2} = bdberl_port:new(), + {ok, 1} = bdberl_port:open_database(P2, "test2", btree), + {ok, 0} = bdberl_port:open_database(P2, "test1", hash), + + %% Close one of the databases + ok = bdberl_port:close_database(P, 0), + ok = bdberl_port:close_database(P2, 0), + + %% Attempt to close an invalid ref + {error, invalid_dbref} = bdberl_port:close_database(P, 21000), + + %% Open up another db -- should re-use dbref 0 as that's the first available + {ok, 0} = bdberl_port:open_database(P, "test3", btree), + + %% Close both ports + true = port_close(P), + true = port_close(P2). + +