couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From klaus_trai...@apache.org
Subject couchdb-peruser git commit: More refactoring and improvements
Date Thu, 13 Aug 2015 15:48:51 GMT
Repository: couchdb-peruser
Updated Branches:
  refs/heads/master 1b4641e18 -> 5ea516867


More refactoring and improvements


Project: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/commit/5ea51686
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/tree/5ea51686
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/diff/5ea51686

Branch: refs/heads/master
Commit: 5ea516867d02ebc0a195fad84d3f62e00744af6f
Parents: 1b4641e
Author: Klaus Trainer <klaus_trainer@posteo.de>
Authored: Thu Aug 13 17:48:03 2015 +0200
Committer: Klaus Trainer <klaus_trainer@posteo.de>
Committed: Thu Aug 13 17:48:03 2015 +0200

----------------------------------------------------------------------
 src/couchdb_peruser.erl       | 66 ++++++++++++++++++++++----------------
 test/couchdb_peruser_test.erl | 25 +++++++--------
 2 files changed, 49 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-peruser/blob/5ea51686/src/couchdb_peruser.erl
----------------------------------------------------------------------
diff --git a/src/couchdb_peruser.erl b/src/couchdb_peruser.erl
index ce0ec54..e6598da 100644
--- a/src/couchdb_peruser.erl
+++ b/src/couchdb_peruser.erl
@@ -29,39 +29,26 @@
 
 -record(state, {parent, db_name, delete_dbs, changes_pid, changes_ref}).
 
+
 start_link() ->
     gen_server:start_link(?MODULE, [], []).
 
-init([]) ->
-    Server = self(),
-    ok = config:listen_for_changes(?MODULE, Server),
+init() ->
     case config:get_boolean("couchdb_peruser", "enable", false) of
     false ->
-        {ok, #state{parent = Server}};
+        #state{};
     true ->
         DbName = ?l2b(config:get(
                          "couch_httpd_auth", "authentication_db", "_users")),
         DeleteDbs = config:get_boolean("couchdb_peruser", "delete_dbs", false),
-        State = #state{parent = Server,
+        State = #state{parent = self(),
                        db_name = DbName,
                        delete_dbs = DeleteDbs},
         {Pid, Ref} = spawn_opt(
             ?MODULE, init_changes_handler, [State], [link, monitor]),
-        {ok, State#state{changes_pid=Pid, changes_ref=Ref}}
+        State#state{changes_pid=Pid, changes_ref=Ref}
     end.
 
-handle_config_change("couch_httpd_auth", "authentication_db", _Value, _Persist, Server) ->
-    gen_server:cast(Server, stop),
-    remove_handler;
-handle_config_change("couchdb_peruser", _Key, _Value, _Persist, Server) ->
-    gen_server:cast(Server, stop),
-    remove_handler;
-handle_config_change(_Section, _Key, _Value, _Persist, Server) ->
-    {ok, Server}.
-
-handle_config_terminate(_Self, Reason, _Server) ->
-    {stop, Reason}.
-
 init_changes_handler(#state{db_name=DbName} = State) ->
     try
         {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX, sys_db]),
@@ -97,11 +84,6 @@ changes_handler({change, {Doc}, _Prepend}, _ResType, State=#state{}) ->
 changes_handler(_Event, _ResType, State) ->
     State.
 
-terminate(_Reason, _State) ->
-    %% Everything should be linked or monitored, let nature
-    %% take its course.
-    ok.
-
 delete_user_db(User) ->
     UserDb = user_db_name(User),
     try
@@ -158,24 +140,52 @@ user_db_name(User) ->
         [string:to_lower(integer_to_list(X, 16)) || <<X>> <= User]),
     <<?USERDB_PREFIX,HexUser/binary>>.
 
+
+%% gen_server callbacks
+
+init([]) ->
+    ok = config:listen_for_changes(?MODULE, self()),
+    {ok, init()}.
+
 handle_call(_Msg, _From, State) ->
     {reply, error, State}.
 
-handle_cast(stop, State) when State#state.changes_pid =/= undefined ->
+handle_cast(update_config, State) when State#state.changes_pid =/= undefined ->
     % we don't want to have multiple changes handler at the same time
+    demonitor(State#state.changes_ref, [flush]),
     exit(State#state.changes_pid, kill),
-    {Pid, Ref} = spawn_opt(
-        ?MODULE, init_changes_handler, [State], [link, monitor]),
-    {noreply, State#state{changes_pid=Pid, changes_ref=Ref}};
+    {noreply, init()};
+handle_cast(update_config, _State) ->
+    {noreply, init()};
 handle_cast(stop, State) ->
     {stop, normal, State};
 handle_cast(_Msg, State) ->
     {noreply, State}.
 
-handle_info({'DOWN', Ref, _, _, _Reason}, State=#state{changes_ref=Ref}) ->
+handle_info({'DOWN', Ref, _, _, _Reason}, #state{changes_ref=Ref} = State) ->
     {stop, normal, State};
 handle_info(_Msg, State) ->
     {noreply, State}.
 
+terminate(_Reason, _State) ->
+    %% Everything should be linked or monitored, let nature
+    %% take its course.
+    ok.
+
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
+
+
+%% config_listener callbacks
+
+handle_config_change("couch_httpd_auth", "authentication_db", _Value, _Persist, Server) ->
+    gen_server:cast(Server, update_config),
+    {ok, Server};
+handle_config_change("couchdb_peruser", _Key, _Value, _Persist, Server) ->
+    gen_server:cast(Server, update_config),
+    {ok, Server};
+handle_config_change(_Section, _Key, _Value, _Persist, Server) ->
+    {ok, Server}.
+
+handle_config_terminate(_Self, Reason, _Server) ->
+    {stop, Reason}.

http://git-wip-us.apache.org/repos/asf/couchdb-peruser/blob/5ea51686/test/couchdb_peruser_test.erl
----------------------------------------------------------------------
diff --git a/test/couchdb_peruser_test.erl b/test/couchdb_peruser_test.erl
index 9ea408b..bfbf652 100644
--- a/test/couchdb_peruser_test.erl
+++ b/test/couchdb_peruser_test.erl
@@ -19,13 +19,7 @@
 -define(ADMIN_PASSWORD, "secret").
 
 start_couch() ->
-    Deps = [
-        couchdb_peruser,
-        jiffy,
-        mochiweb,
-        chttpd
-    ],
-    test_util:start_couch(Deps).
+    test_util:start_couch([chttpd]).
 
 stop_couch(TestCtx) ->
     test_util:stop_couch(TestCtx).
@@ -65,7 +59,9 @@ create_user(AuthDb, Name) ->
         "\",\"type\":\"user\",\"roles\":[],\"password\":\"secret\"}",
     Url = lists:concat([
         get_base_url(), "/", ?b2l(AuthDb), "/org.couchdb.user:", Name]),
-    do_request(put, Url, Body).
+    {ok, 201, _, _} = do_request(put, Url, Body),
+    % let's proceed after giving couchdb_peruser some time to create the user db
+    timer:sleep(50).
 
 delete_user(AuthDb, Name) ->
     Url = lists:concat([get_base_url(), "/", ?b2l(AuthDb),
@@ -73,7 +69,9 @@ delete_user(AuthDb, Name) ->
     {ok, 200, _, Body} = do_request(get, Url),
     {DocProps} = jiffy:decode(Body),
     Rev = proplists:get_value(<<"_rev">>, DocProps),
-    {ok, 200, _, _} = do_request(delete, Url ++ "?rev=" ++ ?b2l(Rev)).
+    {ok, 200, _, _} = do_request(delete, Url ++ "?rev=" ++ ?b2l(Rev)),
+    % let's proceed after giving couchdb_peruser some time to delete the user db
+    timer:sleep(50).
 
 all_dbs() ->
     {ok, 200, _, Body} = do_request(get, get_cluster_base_url() ++ "/_all_dbs"),
@@ -102,8 +100,8 @@ should_not_delete_user_db(TestAuthDb) ->
     ?_assert(lists:member(UserDbName, all_dbs())).
 
 should_delete_user_db(TestAuthDb) ->
-    User = "foo",
-    UserDbName = <<"userdb-666f6f">>,
+    User = "bar",
+    UserDbName = <<"userdb-626172">>,
     set_config("couchdb_peruser", "delete_dbs", "true"),
     create_user(TestAuthDb, User),
     ?assert(lists:member(UserDbName, all_dbs())),
@@ -111,15 +109,14 @@ should_delete_user_db(TestAuthDb) ->
     ?_assert(not lists:member(UserDbName, all_dbs())).
 
 should_reflect_config_changes(TestAuthDb) ->
-    User = "foo",
-    UserDbName = <<"userdb-666f6f">>,
+    User = "baz",
+    UserDbName = <<"userdb-62617a">>,
     set_config("couchdb_peruser", "delete_dbs", "true"),
     create_user(TestAuthDb, User),
     ?assert(lists:member(UserDbName, all_dbs())),
     delete_user(TestAuthDb, User),
     ?assert(not lists:member(UserDbName, all_dbs())),
     create_user(TestAuthDb, User),
-    timer:sleep(100),
     ?assert(lists:member(UserDbName, all_dbs())),
     set_config("couchdb_peruser", "delete_dbs", "false"),
     delete_user(TestAuthDb, User),


Mime
View raw message