summaryrefslogtreecommitdiffstats
path: root/scripts/external_libs/pyzmq-14.5.0/python3/ucs4/32bit/zmq/tests/test_win32_shim.py
blob: 55657bdae98ebf4823eb232fe6f0d02e3751d002 (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
from __future__ import print_function

import os

from functools import wraps
from zmq.tests import BaseZMQTestCase
from zmq.utils.win32 import allow_interrupt


def count_calls(f):
    @wraps(f)
    def _(*args, **kwds):
        try:
            return f(*args, **kwds)
        finally:
            _.__calls__ += 1
    _.__calls__ = 0
    return _


class TestWindowsConsoleControlHandler(BaseZMQTestCase):

    def test_handler(self):
        @count_calls
        def interrupt_polling():
            print('Caught CTRL-C!')

        if os.name == 'nt':
            from ctypes import windll
            from ctypes.wintypes import BOOL, DWORD

            kernel32 = windll.LoadLibrary('kernel32')

            # <http://msdn.microsoft.com/en-us/library/ms683155.aspx>
            GenerateConsoleCtrlEvent = kernel32.GenerateConsoleCtrlEvent
            GenerateConsoleCtrlEvent.argtypes = (DWORD, DWORD)
            GenerateConsoleCtrlEvent.restype = BOOL

            try:
                # Simulate CTRL-C event while handler is active.
                with allow_interrupt(interrupt_polling):
                    result = GenerateConsoleCtrlEvent(0, 0)
                    if result == 0:
                        raise WindowsError
            except KeyboardInterrupt:
                pass
            else:
                self.fail('Expecting `KeyboardInterrupt` exception!')

            # Make sure our handler was called.
            self.assertEqual(interrupt_polling.__calls__, 1)
        else:
            # On non-Windows systems, this utility is just a no-op!
            with allow_interrupt(interrupt_polling):
                pass
            self.assertEqual(interrupt_polling.__calls__, 0)