Skip to content

Commit 28347b6

Browse files
author
clowwindy
committed
add unit test for daemon
1 parent 134497c commit 28347b6

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ script:
1818
- pyflakes .
1919
- coverage run tests/nose_plugin.py -v
2020
- python setup.py sdist
21+
- tests/test_daemon.sh
2122
- python tests/test.py --with-coverage -c tests/aes.json
2223
- python tests/test.py --with-coverage -c tests/aes-ctr.json
2324
- python tests/test.py --with-coverage -c tests/aes-cfb1.json

shadowsocks/daemon.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ def daemon_start(pid_file, log_file):
105105
assert pid != -1
106106

107107
def handle_exit(signum, _):
108-
sys.exit(0)
108+
if signum == signal.SIGTERM:
109+
sys.exit(0)
110+
sys.exit(1)
109111

110112
if pid > 0:
111113
# parent waits for its child
112114
signal.signal(signal.SIGINT, handle_exit)
115+
signal.signal(signal.SIGTERM, handle_exit)
113116
time.sleep(5)
114117
sys.exit(0)
115118

@@ -121,11 +124,16 @@ def handle_exit(signum, _):
121124
sys.exit(1)
122125

123126
print('started')
124-
os.kill(ppid, signal.SIGINT)
127+
os.kill(ppid, signal.SIGTERM)
125128

126129
sys.stdin.close()
127-
freopen(log_file, 'a', sys.stdout)
128-
freopen(log_file, 'a', sys.stderr)
130+
try:
131+
freopen(log_file, 'a', sys.stdout)
132+
freopen(log_file, 'a', sys.stderr)
133+
except IOError as e:
134+
logging.error(e)
135+
os.kill(ppid, signal.SIGINT)
136+
sys.exit(1)
129137

130138

131139
def daemon_stop(pid_file):

tests/test_daemon.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
function test {
4+
expected=$1
5+
shift
6+
echo "running test: $command $@"
7+
$command $@
8+
status=$?
9+
if [ $status -ne $expected ]; then
10+
echo "exit $status != $expected"
11+
exit 1
12+
fi
13+
echo "exit status $status == $expected"
14+
echo OK
15+
return
16+
}
17+
18+
for module in local server
19+
do
20+
21+
command="coverage run -p -a shadowsocks/$module.py"
22+
23+
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
24+
25+
test 0 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
26+
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
27+
28+
test 0 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
29+
test 1 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
30+
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
31+
32+
test 0 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
33+
test 0 -c tests/aes.json -d restart --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
34+
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
35+
36+
test 0 -c tests/aes.json -d restart --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
37+
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
38+
39+
test 1 -c tests/aes.json -d start --pid-file /tmp/not_exist/shadowsocks.pid --log-file /tmp/shadowsocks.log
40+
test 1 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/not_exist/shadowsocks.log
41+
42+
done

0 commit comments

Comments
 (0)