couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [1/3] couchdb-couch-epi git commit: Ensure calling order of plugins during dispatch
Date Tue, 27 Oct 2015 19:31:25 GMT
Repository: couchdb-couch-epi
Updated Branches:
  refs/heads/master 727cf7132 -> 77dfaf413


Ensure calling order of plugins during dispatch

If there are multiple plugins providing same service they will be called
in the order they listed in application:get_env(couch_epi, plugins).


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/commit/926d3b13
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/tree/926d3b13
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/diff/926d3b13

Branch: refs/heads/master
Commit: 926d3b13207fe829bcefd51d4465f9cacd35315e
Parents: be34447
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Tue Oct 6 12:17:31 2015 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Tue Oct 6 12:17:31 2015 -0700

----------------------------------------------------------------------
 README.md                   |  4 ++++
 src/couch_epi_functions.erl |  5 +++--
 src/couch_epi_plugin.erl    |  2 +-
 test/couch_epi_tests.erl    | 36 ++++++++++++++++++++++++++++++------
 4 files changed, 38 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/926d3b13/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 3f15a62..685d314 100644
--- a/README.md
+++ b/README.md
@@ -96,6 +96,10 @@ There are multiple ways of doing the apply which is controlled by Opts
 Notes:
 
   - `concurrent` is incompatible with `pipe`
+  - if there are multiple plugins providing same service they will be called in the order
+    they listed in application:get_env(couch_epi, plugins)
+  - if the same plugin provides multiple implementations of the same service
+    the order is undefined (fixable)
 
 # couch_epi_plugin behaviour
 

http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/926d3b13/src/couch_epi_functions.erl
----------------------------------------------------------------------
diff --git a/src/couch_epi_functions.erl b/src/couch_epi_functions.erl
index 34d1a06..ac93739 100644
--- a/src/couch_epi_functions.erl
+++ b/src/couch_epi_functions.erl
@@ -43,6 +43,7 @@ definitions(Modules) ->
     [{M, M:module_info(exports) -- Blacklist} || M <- Modules].
 
 group(KV) ->
-    dict:to_list(lists:foldr(fun({K,V}, D) ->
+    Dict = lists:foldr(fun({K,V}, D) ->
         dict:append_list(K, V, D)
-    end, dict:new(), KV)).
+    end, dict:new(), KV),
+    [{K, lists:reverse(V)} || {K, V} <- dict:to_list(Dict)].

http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/926d3b13/src/couch_epi_plugin.erl
----------------------------------------------------------------------
diff --git a/src/couch_epi_plugin.erl b/src/couch_epi_plugin.erl
index 05aa591..718e5fb 100644
--- a/src/couch_epi_plugin.erl
+++ b/src/couch_epi_plugin.erl
@@ -68,7 +68,7 @@ definitions(Kind, Key) ->
     Filtered = filter_by_key(Definitions, Kind, Key),
     case group_specs(Filtered) of
         [] -> [];
-        [{_, Defs}] -> Defs
+        [{_, Defs}] -> lists:reverse(Defs)
     end.
 
 notify(Key, OldData, NewData, Specs) ->

http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/926d3b13/test/couch_epi_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_epi_tests.erl b/test/couch_epi_tests.erl
index fc2daa8..56733b1 100644
--- a/test/couch_epi_tests.erl
+++ b/test/couch_epi_tests.erl
@@ -86,7 +86,7 @@
 %% couch_epi_plugin behaviour
 %% ------------------------------------------------------------------
 
-plugin_module([KV, Spec]) ->
+plugin_module([KV, Spec]) when is_tuple(Spec) ->
     SpecStr = io_lib:format("~w", [Spec]),
     KVStr = "'" ++ atom_to_list(KV) ++ "'",
     "
@@ -114,7 +114,7 @@ plugin_module([KV, Spec]) ->
         notify(Key, OldData, Data) ->
             couch_epi_tests:notify_cb(Key, OldData, Data, " ++ KVStr ++ ").
     ";
-plugin_module([KV]) ->
+plugin_module([KV, Provider]) when is_atom(Provider) ->
     KVStr = "'" ++ atom_to_list(KV) ++ "'",
     "
         -compile([export_all]).
@@ -122,13 +122,12 @@ plugin_module([KV]) ->
         app() -> test_app.
         providers() ->
             [
-                {my_service, provider1},
-                {my_service, provider2}
+                {my_service, " ++ atom_to_list(Provider) ++ "}
             ].
 
         services() ->
             [
-                {my_service, provider1}
+                {my_service, " ++ atom_to_list(Provider) ++ "}
             ].
 
         data_providers() ->
@@ -203,7 +202,10 @@ setup(functions) ->
 
     KV = start_state_storage(),
 
-    ok = start_epi([{provider_epi, plugin_module([KV])}]),
+    ok = start_epi([
+        {provider_epi1, plugin_module([KV, provider1])},
+        {provider_epi2, plugin_module([KV, provider2])}
+    ]),
 
     Pid = whereis(couch_epi:get_handle(Key)),
     Handle = couch_epi:get_handle(Key),
@@ -285,6 +287,20 @@ epi_apply_test_() ->
         }
     }.
 
+epi_providers_order_test_() ->
+    {
+        "epi providers' order test",
+        {
+            foreach,
+            fun() -> setup(functions) end,
+            fun teardown/1,
+            [
+                fun check_providers_order/1
+            ]
+        }
+    }.
+
+
 epi_reload_test_() ->
     Cases = [
         data_file,
@@ -525,6 +541,14 @@ ensure_no_reload_when_no_change(_Case,
         ?assertEqual(error, get(Ctx, is_called))
     end).
 
+check_providers_order(#ctx{handle = Handle, kv = KV, key = Key} = Ctx) ->
+    ?_test(begin
+        Result = couch_epi:apply(Handle, Key, inc, [KV, 2], [pipe]),
+        ?assertMatch([KV, 4], Result),
+        Order = [element(2, get(Ctx, K)) || K <- [inc1, inc2]],
+        ?assertEqual(Order, [3, 4]),
+        ok
+    end).
 
 %% ------------------------------------------------------------------
 %% Internal Function Definitions


Mime
View raw message