Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
6bf07aed TJ |
20 | /** |
21 | Pid file handling unit tests | |
22 | ||
23 | @copyright Intra2net AG | |
6bf07aed TJ |
24 | */ |
25 | ||
26 | #include <unistd.h> | |
27 | ||
28 | #include <iostream> | |
29 | #include <string> | |
30 | #include <sstream> | |
31 | #include <stdexcept> | |
32 | ||
9fe0853b TJ |
33 | #define BOOST_TEST_DYN_LINK |
34 | #include <boost/test/unit_test.hpp> | |
6bf07aed TJ |
35 | |
36 | #include <pidfile.hpp> | |
37 | #include <filefunc.hxx> | |
38 | ||
39 | using namespace std; | |
6bf07aed | 40 | |
9fe0853b | 41 | class TestPidFileFixture |
6bf07aed | 42 | { |
9fe0853b TJ |
43 | protected: |
44 | string Filename; | |
6bf07aed | 45 | |
9fe0853b TJ |
46 | public: |
47 | TestPidFileFixture() | |
48 | { | |
49 | ostringstream out; | |
bb96045b | 50 | out << "test_pidfile.run." << getpid(); |
6bf07aed | 51 | |
9fe0853b TJ |
52 | Filename = out.str(); |
53 | } | |
6bf07aed | 54 | |
9fe0853b TJ |
55 | ~TestPidFileFixture() |
56 | { | |
57 | } | |
6bf07aed TJ |
58 | }; |
59 | ||
9fe0853b TJ |
60 | BOOST_FIXTURE_TEST_SUITE(TestPidFile, TestPidFileFixture) |
61 | ||
62 | BOOST_AUTO_TEST_CASE(WriteFile) | |
63 | { | |
64 | PidFile pidfile(Filename); | |
65 | bool rtn_write = pidfile.write(); | |
66 | ||
67 | BOOST_CHECK_EQUAL(true, rtn_write); | |
68 | } | |
69 | ||
70 | BOOST_AUTO_TEST_CASE(AutoRemoval) | |
71 | { | |
72 | bool rtn_write = false; | |
73 | { | |
74 | PidFile pidfile(Filename); | |
75 | rtn_write = pidfile.write(); | |
76 | } | |
77 | bool exists = I2n::file_exists(Filename); | |
78 | ||
79 | BOOST_CHECK_EQUAL(true, rtn_write); | |
80 | BOOST_CHECK_EQUAL(false, exists); | |
81 | } | |
82 | ||
83 | BOOST_AUTO_TEST_CASE(NoAutoRemoval) | |
84 | { | |
85 | bool rtn_write = false; | |
86 | { | |
87 | PidFile pidfile(Filename, false); | |
88 | rtn_write = pidfile.write(); | |
89 | } | |
90 | bool exists = I2n::file_exists(Filename); | |
91 | I2n::unlink(Filename); | |
92 | ||
93 | BOOST_CHECK_EQUAL(true, rtn_write); | |
94 | BOOST_CHECK_EQUAL(true, exists); | |
95 | } | |
96 | ||
97 | BOOST_AUTO_TEST_CASE(NotRunning) | |
98 | { | |
99 | PidFile pidfile(Filename); | |
100 | bool rtn_check = pidfile.check_already_running(); | |
101 | ||
102 | BOOST_CHECK_EQUAL(false, rtn_check); | |
103 | } | |
104 | ||
105 | BOOST_AUTO_TEST_CASE(IsAlreadyRunning) | |
106 | { | |
107 | PidFile pidfile1(Filename); | |
108 | bool rtn_file1_check = pidfile1.check_already_running(); | |
109 | bool rtn_file1_write = pidfile1.write(); | |
110 | ||
111 | BOOST_CHECK_EQUAL(false, rtn_file1_check); | |
112 | BOOST_CHECK_EQUAL(true, rtn_file1_write); | |
113 | ||
114 | PidFile pidfile2(Filename); | |
115 | pid_t my_pid = 0; | |
116 | bool rtn_file2_check = pidfile2.check_already_running(&my_pid); | |
117 | ||
118 | BOOST_CHECK_EQUAL(true, rtn_file2_check); | |
119 | BOOST_CHECK_EQUAL(getpid(), my_pid); | |
120 | } | |
121 | ||
122 | BOOST_AUTO_TEST_SUITE_END() |