You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2020/08/31 23:00:42 UTC
[couchdb] branch handle_active_tasks_empty_map created (now f657df8)
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a change to branch handle_active_tasks_empty_map
in repository https://gitbox.apache.org/repos/asf/couchdb.git.
at f657df8 Handle empty maps in active_tasks data structure
This branch includes the following new commits:
new f657df8 Handle empty maps in active_tasks data structure
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[couchdb] 01/01: Handle empty maps in active_tasks data structure
Posted by va...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch handle_active_tasks_empty_map
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit f657df83f1296543c53780df8776c2a5fd02ed90
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Mon Aug 31 18:59:15 2020 -0400
Handle empty maps in active_tasks data structure
Empty maps maybe useful to initialize the data in some cases but we don't want
to emit an entry in the output with just an empty map.
While at it, add some tests to check the basics.
---
src/fabric/src/fabric2_active_tasks.erl | 3 +-
src/fabric/test/fabric2_active_tasks_tests.erl | 120 +++++++++++++++++++++++++
2 files changed, 122 insertions(+), 1 deletion(-)
diff --git a/src/fabric/src/fabric2_active_tasks.erl b/src/fabric/src/fabric2_active_tasks.erl
index 2c03ec3..e706eba 100644
--- a/src/fabric/src/fabric2_active_tasks.erl
+++ b/src/fabric/src/fabric2_active_tasks.erl
@@ -34,7 +34,8 @@ get_active_tasks() ->
{ok, Data} = couch_jobs:get_job_data(JTx, Type, JobId),
case maps:get(?ACTIVE_TASK_INFO, Data, not_found) of
not_found -> false;
- Info -> {true, Info}
+ #{} = Map when map_size(Map) == 0 -> false;
+ #{} = Info -> {true, Info}
end
end, JobIds),
TaskAcc ++ Tasks
diff --git a/src/fabric/test/fabric2_active_tasks_tests.erl b/src/fabric/test/fabric2_active_tasks_tests.erl
new file mode 100644
index 0000000..8914500
--- /dev/null
+++ b/src/fabric/test/fabric2_active_tasks_tests.erl
@@ -0,0 +1,120 @@
+% 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(fabric2_active_tasks_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include("fabric2_test.hrl").
+
+
+-define(JOB_TYPE, <<"fabric2_active_tasks_tests_type">>).
+-define(JOB_ID, <<"job_id">>).
+
+
+active_tasks_test_() ->
+ {
+ "Test cleanup of stale indices",
+ {
+ setup,
+ fun setup_all/0,
+ fun cleanup_all/1,
+ {
+ foreach,
+ fun setup/0,
+ fun cleanup/1,
+ [
+ ?TDEF_FE(no_active_tasks_defined),
+ ?TDEF_FE(empty_map_info),
+ ?TDEF_FE(can_read_active_tasks),
+ ?TDEF_FE(only_running_tasks_appear)
+ ]
+ }
+ }
+ }.
+
+
+setup_all() ->
+ Ctx = test_util:start_couch([fabric, couch_jobs]),
+ couch_jobs:set_type_timeout(?JOB_TYPE, 5000),
+ meck:new(couch_jobs, [passthrough]),
+ meck:expect(couch_jobs, get_types, 1, [?JOB_TYPE]),
+ Ctx.
+
+
+cleanup_all(Ctx) ->
+ meck:unload(),
+ test_util:stop_couch(Ctx).
+
+
+setup() ->
+ ok = couch_jobs:add(undefined, ?JOB_TYPE, ?JOB_ID, #{}),
+ ok.
+
+
+cleanup(_) ->
+ meck:reset(couch_jobs),
+ couch_jobs:remove(undefined, ?JOB_TYPE, ?JOB_ID).
+
+
+no_active_tasks_defined(_) ->
+ {ok, Job1, #{}} = couch_jobs:accept(?JOB_TYPE),
+ ?assertEqual([], fabric2_active_tasks:get_active_tasks()),
+ ok = couch_jobs:finish(undefined, Job1).
+
+
+empty_map_info(_) ->
+ {ok, Job1, Data} = couch_jobs:accept(?JOB_TYPE),
+
+ Data1 = fabric2_active_tasks:update_active_task_info(Data, #{}),
+ {ok, Job2} = couch_jobs:update(undefined, Job1, Data1),
+ ?assertEqual([], fabric2_active_tasks:get_active_tasks()),
+ ok = couch_jobs:finish(undefined, Job2).
+
+
+can_read_active_tasks(_) ->
+ {ok, Job1, Data} = couch_jobs:accept(?JOB_TYPE),
+
+ Info = #{<<"x">> => 1},
+ Data1 = fabric2_active_tasks:update_active_task_info(Data, Info),
+ {ok, Job2} = couch_jobs:update(undefined, Job1, Data1),
+ ?assertEqual([#{<<"x">> => 1}], fabric2_active_tasks:get_active_tasks()),
+
+ Info1 = fabric2_active_tasks:get_active_task_info(Data1),
+ Info2 = Info1#{<<"y">> => 2},
+ Data2 = fabric2_active_tasks:update_active_task_info(Data1, Info2),
+ {ok, Job3} = couch_jobs:update(undefined, Job2, Data2),
+ ?assertEqual([#{<<"x">> => 1, <<"y">> => 2}],
+ fabric2_active_tasks:get_active_tasks()),
+ ok = couch_jobs:finish(undefined, Job3).
+
+
+only_running_tasks_appear(_) ->
+ {ok, Job1, Data} = couch_jobs:accept(?JOB_TYPE),
+
+ Info = #{<<"x">> => 1},
+ Data1 = fabric2_active_tasks:update_active_task_info(Data, Info),
+ {ok, Job2} = couch_jobs:update(undefined, Job1, Data1),
+
+ ?assertEqual([#{<<"x">> => 1}], fabric2_active_tasks:get_active_tasks()),
+ {ok, _} = couch_jobs:resubmit(undefined, Job2),
+
+ ok = couch_jobs:finish(undefined, Job2),
+
+ ?assertEqual([], fabric2_active_tasks:get_active_tasks()),
+ {ok, Job3, #{}} = couch_jobs:accept(?JOB_TYPE),
+ ?assertEqual([#{<<"x">> => 1}], fabric2_active_tasks:get_active_tasks()),
+
+ ok = couch_jobs:finish(undefined, Job3),
+ ?assertEqual([], fabric2_active_tasks:get_active_tasks()).