aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio_tests/tests/unit/_cython/_fork_test.py
blob: aeb02458a7e7ead1cb02b497e43d3c88720175c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import threading
import unittest

from grpc._cython import cygrpc


def _get_number_active_threads():
    return cygrpc._fork_state.active_thread_count._num_active_threads


@unittest.skipIf(os.name == 'nt', 'Posix-specific tests')
class ForkPosixTester(unittest.TestCase):

    def setUp(self):
        cygrpc._GRPC_ENABLE_FORK_SUPPORT = True

    def testForkManagedThread(self):

        def cb():
            self.assertEqual(1, _get_number_active_threads())

        thread = cygrpc.ForkManagedThread(cb)
        thread.start()
        thread.join()
        self.assertEqual(0, _get_number_active_threads())

    def testForkManagedThreadThrowsException(self):

        def cb():
            self.assertEqual(1, _get_number_active_threads())
            raise Exception("expected exception")

        thread = cygrpc.ForkManagedThread(cb)
        thread.start()
        thread.join()
        self.assertEqual(0, _get_number_active_threads())


@unittest.skipUnless(os.name == 'nt', 'Windows-specific tests')
class ForkWindowsTester(unittest.TestCase):

    def testForkManagedThreadIsNoOp(self):

        def cb():
            pass

        thread = cygrpc.ForkManagedThread(cb)
        thread.start()
        thread.join()


if __name__ == '__main__':
    unittest.main(verbosity=2)