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