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).
diff --git a/README.md b/README.md
index 3f15a62..685d314 100644
--- a/README.md
+++ b/README.md
@@ -96,6 +96,10 @@
 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
 
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 @@
     [{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)].
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 @@
     Filtered = filter_by_key(Definitions, Kind, Key),
     case group_specs(Filtered) of
         [] -> [];
-        [{_, Defs}] -> Defs
+        [{_, Defs}] -> lists:reverse(Defs)
     end.
 
 notify(Key, OldData, NewData, Specs) ->
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 @@
         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 @@
         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 @@
 
     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_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 @@
         ?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