xref: /unit/test/test_ruby_hooks.py (revision 1971:3410f9d2a662)
1import pytest
2from conftest import unit_stop
3from unit.applications.lang.ruby import TestApplicationRuby
4from unit.option import option
5from unit.utils import waitforglob
6
7
8class TestRubyHooks(TestApplicationRuby):
9    prerequisites = {'modules': {'ruby': 'all'}}
10
11    def _wait_cookie(self, pattern, count):
12        return waitforglob(
13            option.temp_dir + '/ruby/hooks/cookie_' + pattern, count
14        )
15
16    def test_ruby_hooks_eval(self):
17        processes = 2
18
19        self.load('hooks', processes=processes, hooks='eval.rb')
20
21        hooked = self._wait_cookie('eval.*', processes)
22
23        assert hooked, 'hooks evaluated'
24
25    def test_ruby_hooks_on_worker_boot(self):
26        processes = 2
27
28        self.load('hooks', processes=processes, hooks='on_worker_boot.rb')
29
30        hooked = self._wait_cookie('worker_boot.*', processes)
31
32        assert hooked, 'on_worker_boot called'
33
34    def test_ruby_hooks_on_worker_shutdown(self):
35        processes = 2
36
37        self.load('hooks', processes=processes, hooks='on_worker_shutdown.rb')
38
39        assert self.get()['status'] == 200, 'app response'
40
41        self.load('empty')
42
43        hooked = self._wait_cookie('worker_shutdown.*', processes)
44
45        assert hooked, 'on_worker_shutdown called'
46
47    def test_ruby_hooks_on_thread_boot(self):
48        processes = 1
49        threads = 2
50
51        self.load(
52            'hooks',
53            processes=processes,
54            threads=threads,
55            hooks='on_thread_boot.rb',
56        )
57
58        hooked = self._wait_cookie('thread_boot.*', processes * threads)
59
60        assert hooked, 'on_thread_boot called'
61
62    def test_ruby_hooks_on_thread_shutdown(self):
63        processes = 1
64        threads = 2
65
66        self.load(
67            'hooks',
68            processes=processes,
69            threads=threads,
70            hooks='on_thread_shutdown.rb',
71        )
72
73        assert self.get()['status'] == 200, 'app response'
74
75        self.load('empty')
76
77        hooked = self._wait_cookie('thread_shutdown.*', processes * threads)
78
79        assert hooked, 'on_thread_shutdown called'
80
81    def test_ruby_hooks_multiple(self):
82        processes = 1
83        threads = 1
84
85        self.load(
86            'hooks', processes=processes, threads=threads, hooks='multiple.rb',
87        )
88
89        hooked = self._wait_cookie('worker_boot.*', processes)
90        assert hooked, 'on_worker_boot called'
91
92        hooked = self._wait_cookie('thread_boot.*', threads)
93        assert hooked, 'on_thread_boot called'
94