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 "test.h"
40 #include <sys/resource.h>
41 
42 // try to open N databases when N > open file limit.  should fail gracefully.
43 
test_main(int argc,char * const argv[])44 int test_main (int argc __attribute__((__unused__)), char *const argv[] __attribute__((__unused__))) {
45     int r;
46 
47     const int N = 200;
48 
49     struct rlimit nofile_limit = { N, N };
50     r = setrlimit(RLIMIT_NOFILE, &nofile_limit);
51     // assert(r == 0); // valgrind does not like this
52     if (r != 0) {
53         printf("warning: set nofile limit to %d failed %d %s\n", N, errno, strerror(errno));
54     }
55 
56     toku_os_recursive_delete(TOKU_TEST_FILENAME);
57     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
58     assert(r == 0);
59 
60     DB_ENV *env;
61     r = db_env_create(&env, 0);
62     assert(r == 0);
63 
64     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO);
65     assert(r == 0);
66 
67     DB **dbs = new DB *[N];
68     for (int i = 0; i < N; i++) {
69         dbs[i] = NULL;
70     }
71     bool emfile_happened = false; // should happen since there are less than N unused file descriptors
72     for (int i = 0; i < N; i++) {
73         r = db_create(&dbs[i], env, 0);
74         assert(r == 0);
75 
76         char dbname[32]; sprintf(dbname, "%d.test", i);
77         r = dbs[i]->open(dbs[i], NULL, dbname, NULL, DB_BTREE, DB_AUTO_COMMIT+DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
78         if (r == EMFILE) {
79             emfile_happened = true;
80             break;
81         }
82         assert(r == 0);
83     }
84 
85     assert(emfile_happened);
86 
87     for (int i = 0; i < N; i++) {
88         if (dbs[i]) {
89             r = dbs[i]->close(dbs[i], 0);
90             assert(r == 0);
91         }
92     }
93 
94     r = env->close(env, 0);
95     assert(r == 0);
96 
97     delete [] dbs;
98 
99     return 0;
100 }
101