Skip to content

Commit

Permalink
feat(7.3): add tenant tests and implement missing features
Browse files Browse the repository at this point in the history
  • Loading branch information
savonarola committed Apr 4, 2024
1 parent 73b3400 commit 7b99ac8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUN set -ex; \
rm -rf /var/lib/apt/lists/*

# FDB bindings tester uses the Python bindings
RUN pip3 install foundationdb==${FDB_VERSION}
RUN pip3 install foundationdb==${FDB_VERSION} --break-system-packages

COPY create_cluster_file.bash /usr/local/bin/

Expand Down
32 changes: 32 additions & 0 deletions c_src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,37 @@ erlfdb_database_open_tenant(
}


static ERL_NIF_TERM
erlfdb_tenant_get_id(
ErlNifEnv* env,
int argc,
const ERL_NIF_TERM argv[]
)
{
ErlFDBSt* st = (ErlFDBSt*) enif_priv_data(env);
ErlFDBTenant* t;
void* res;
FDBFuture* future;

if(st->lib_state != ErlFDB_CONNECTED) {
return enif_make_badarg(env);
}

if(argc != 1) {
return enif_make_badarg(env);
}

if(!enif_get_resource(env, argv[0], ErlFDBTenantRes, &res)) {
return enif_make_badarg(env);
}
t = (ErlFDBTenant*) res;

future = fdb_tenant_get_id(t->tenant);

return erlfdb_create_future(env, future, erlfdb_future_get_int64);
}


static ERL_NIF_TERM
erlfdb_tenant_create_transaction(
ErlNifEnv* env,
Expand Down Expand Up @@ -2272,6 +2303,7 @@ static ErlNifFunc funcs[] =
NIF_FUNC(erlfdb_create_database, 1),
NIF_FUNC(erlfdb_database_set_option, 3),
NIF_FUNC(erlfdb_database_open_tenant, 2),
NIF_FUNC(erlfdb_tenant_get_id, 1),
NIF_FUNC(erlfdb_database_create_transaction, 1),

NIF_FUNC(erlfdb_tenant_create_transaction, 1),
Expand Down
6 changes: 6 additions & 0 deletions src/erlfdb_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
database_create_transaction/1,

tenant_create_transaction/1,
tenant_get_id/1,

transaction_set_option/2,
transaction_set_option/3,
Expand Down Expand Up @@ -294,6 +295,10 @@ database_create_transaction({erlfdb_database, Db}) ->
tenant_create_transaction({erlfdb_tenant, Db}) ->
erlfdb_tenant_create_transaction(Db).

-spec tenant_get_id(tenant()) -> future().
tenant_get_id({erlfdb_tenant, Db}) ->
erlfdb_tenant_get_id(Db).

-spec transaction_set_option(transaction(), Option :: transaction_option()) -> ok.
transaction_set_option(Transaction, Option) ->
transaction_set_option(Transaction, Option, <<>>).
Expand Down Expand Up @@ -580,6 +585,7 @@ erlfdb_database_create_transaction(_Database) -> ?NOT_LOADED.

%% Tenants
erlfdb_tenant_create_transaction(_Database) -> ?NOT_LOADED.
erlfdb_tenant_get_id(_Tenant) -> ?NOT_LOADED.

% Transactions
erlfdb_transaction_set_option(_Transaction, _TransactionOption, _Value) -> ?NOT_LOADED.
Expand Down
7 changes: 5 additions & 2 deletions src/erlfdb_tenant.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

-module(erlfdb_tenant).

-export([create_tenant/2, open_tenant/2, delete_tenant/2, list_tenants/4]).
-export([create_tenant/2, open_tenant/2, delete_tenant/2, list_tenants/4, get_id/1]).

-define(IS_DB, {erlfdb_database, _}).
-define(TENANT_MAP_PREFIX, <<16#FF, 16#FF, "/management/tenant_map/">>).
-define(TENANT_MAP_PREFIX, <<16#FF, 16#FF, "/management/tenant/map/">>).
-define(MAX_LIMIT, 2147483647).

-import(erlfdb, [get/2, clear/2, set/3, wait/1, transactional/2, set_option/2]).
Expand Down Expand Up @@ -59,6 +59,9 @@ list_tenants(Db, From, To, Limit) ->
[]
end.

get_id(Tenant) ->
erlfdb_nif:tenant_get_id(Tenant).

do_list_tenants(?IS_DB = Db, From, To, Limit) ->
transactional(Db, fun(Tx) ->
list_tenants(Tx, From, To, Limit)
Expand Down
47 changes: 47 additions & 0 deletions test/erlfdb_07_tenant_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(erlfdb_07_tenant_test).

-include_lib("eunit/include/eunit.hrl").

tenant_management_test() ->
Db = erlfdb_util:get_test_db(),
Tenant = gen(10),
?assertEqual(ok, erlfdb_tenant:create_tenant(Db, Tenant)),
?assertEqual({error, tenant_already_exists}, erlfdb_tenant:create_tenant(Db, Tenant)),
?assertEqual(ok, erlfdb_tenant:delete_tenant(Db, Tenant)),
?assertEqual({error, tenant_not_found}, erlfdb_tenant:delete_tenant(Db, Tenant)).

tenant_list_test() ->
Db = erlfdb_util:get_test_db(),
Tenant1 = gen(10),
Tenant2 = gen(10),
?assertEqual(ok, erlfdb_tenant:create_tenant(Db, Tenant1)),
?assertEqual(ok, erlfdb_tenant:create_tenant(Db, Tenant2)),
{Tenants, _} = lists:unzip(erlfdb_tenant:list_tenants(Db, <<>>, <<16#FF>>, 99999999)),
?assert(lists:member(Tenant1, Tenants)),
?assert(lists:member(Tenant2, Tenants)).

tenant_get_id_test() ->
Db = erlfdb_util:get_test_db(),
Tenant = gen(10),
?assertEqual(ok, erlfdb_tenant:create_tenant(Db, Tenant)),
Tn = erlfdb_tenant:open_tenant(Db, Tenant),
?assert(is_integer(erlfdb:wait(erlfdb_tenant:get_id(Tn)))).

gen(N) ->
<< <<(gen_char())>> || _ <- lists:seq(1, N) >>.

gen_char() ->
$a - 1 + rand:uniform($z - $a + 1).

2 changes: 1 addition & 1 deletion test/tester.es
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ execute(_TxObj, #st{tenant = Tenant} = St, <<"TENANT_GET_ID">>) ->
case Tenant of
undefined -> stack_push(St, <<"NO_ACTIVE_TENANT">>);
_ ->
%% TODO: get tenant id actually
_Id = erlfdb:wait(erlfdb_tenant:get_id(Tenant)),
stack_push(St, <<"GOT_TENANT_ID">>)
end,
St;
Expand Down

0 comments on commit 7b99ac8

Please sign in to comment.