1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "cachetable/background_job_manager.h"
40 
41 #include "test.h"
42 
43 BACKGROUND_JOB_MANAGER bjm;
44 
finish_bjm(void * arg)45 static void *finish_bjm(void *arg) {
46     bjm_wait_for_jobs_to_finish(bjm);
47     return arg;
48 }
49 
50 
bjm_test(void)51 static void bjm_test(void) {
52     int r = 0;
53     bjm = NULL;
54     bjm_init(&bjm);
55     // test simple add/remove of background job works
56     r = bjm_add_background_job(bjm);
57     assert_zero(r);
58     bjm_remove_background_job(bjm);
59     bjm_wait_for_jobs_to_finish(bjm);
60     // assert that you cannot add a background job
61     // without resetting bjm after waiting
62     // for finish
63     r = bjm_add_background_job(bjm);
64     assert(r != 0);
65     // test that after a reset, we can resume adding background jobs
66     bjm_reset(bjm);
67     r = bjm_add_background_job(bjm);
68     assert_zero(r);
69     bjm_remove_background_job(bjm);
70     bjm_wait_for_jobs_to_finish(bjm);
71 
72     bjm_reset(bjm);
73     r = bjm_add_background_job(bjm);
74     assert_zero(r);
75     toku_pthread_t tid;
76     r = toku_pthread_create(toku_uninstrumented,
77                             &tid,
78                             nullptr,
79                             finish_bjm,
80                             nullptr);
81     assert_zero(r);
82     usleep(2 * 1024 * 1024);
83     // should return non-zero because tid is waiting
84     // for background jobs to finish
85     r = bjm_add_background_job(bjm);
86     assert(r != 0);
87     bjm_remove_background_job(bjm);
88     void *ret;
89     r = toku_pthread_join(tid, &ret);
90     assert_zero(r);
91 
92     bjm_destroy(bjm);
93 }
94 
95 int
test_main(int argc,const char * argv[])96 test_main (int argc , const char *argv[]) {
97     default_parse_args(argc, argv);
98 
99     bjm_test();
100     if (verbose) printf("test ok\n");
101     return 0;
102 }
103 
104 
105