async def test_signals_slots_mgr(app_mock, signal): signals_slots_mgr = SignalsSlotsManager() Signal.setup_aio_support() func = mock.MagicMock() app_mock.test_signal = signal # test if add method works signals_slots_mgr.add('app.test_signal', func) signals_slots_mgr.initialize(app_mock) signal.emit() await asyncio.sleep(0.1) # schedule signal callbacks with aioqueue enabled assert func.called is True # test if add method works after initialized func_lator = mock.MagicMock() signals_slots_mgr.add('app.test_signal', func_lator) signal.emit() await asyncio.sleep(0.1) assert func_lator.called is True # test if remove method works signals_slots_mgr.remove('app.test_signal', func_lator) signal.emit() await asyncio.sleep(0.1) assert func_lator.call_count == 1 assert func.call_count == 3 Signal.teardown_aio_support()
async def test_add_and_remove_symbol(app_mock, signal_mgr, signal, mocker): """ test add slot symbol """ signal_mgr.initialize(app_mock) Signal.setup_aio_support() app_mock.test_signal = signal func = mock.MagicMock() func.__name__ = 'fake_func' mock_F = mocker.patch('feeluown.fuoexec.signal_manager.fuoexec_F', return_value=func) signal_mgr.add('app.test_signal', func, use_symbol=True) signal.emit() await asyncio.sleep(0.1) mock_F.assert_called_once_with('fake_func') # fuoexec_F should be called once. assert func.called assert func.call_count == 1 signal_mgr.remove('app.test_signal', func, use_symbol=True) signal.emit() await asyncio.sleep(0.1) # fuoexec_F should not be called anymore assert func.call_count == 1 Signal.teardown_aio_support()
def about_to_exit(self): try: logger.info('Do graceful shutdown') self.about_to_shutdown.emit(self) Signal.teardown_aio_support() self.player.stop() self.exit_player() except: # noqa logger.exception("about-to-exit failed")
async def start_app(args, config, sentinal=None): """ The param sentinal is currently only used for unittest. """ Signal.setup_aio_support() app = create_app(args, config) # Do fuoexec initialization before app initialization. fuoexec_init(app) # Initialize app with config. # # all objects can do initialization here. some objects may emit signal, # some objects may connect with others signals. app.initialize() app.initialized.emit(app) # Load last state. app.load_state() def sighanlder(signum, _): logger.info('Signal %d is received', signum) app.exit() # Handle signals. signal.signal(signal.SIGTERM, sighanlder) signal.signal(signal.SIGINT, sighanlder) if sentinal is None: sentinal: asyncio.Future = asyncio.Future() def shutdown(_): # Since about_to_shutdown signal may emit multiple times # (QApplication.aboutToQuit emits multiple times), # we should check if it is already done firstly. if not sentinal.done(): sentinal.set_result(0) app.about_to_shutdown.connect(shutdown, weak=False) # App can exit in several ways. # # GUI mode: # 1. QApplication.quit. QApplication.quit can be called under several circumstances # 1. User press CMD-Q on macOS. # 2. User clicks the tray icon exit button. # 2. SIGTERM is received. # # Daemon mode: # 1. Ctrl-C # 2. SIGTERM app.run() await sentinal Signal.teardown_aio_support()
async def test_add_and_remove_after_initialization(app_mock, signal_mgr, signal): Signal.setup_aio_support() # test if add method works after initialized func_lator = mock.MagicMock() app_mock.test_signal = signal signal_mgr.initialize(app_mock) signal_mgr.add('app.test_signal', func_lator, use_symbol=False) signal.emit() await asyncio.sleep(0.1) assert func_lator.called is True # test if remove method works signal_mgr.remove('app.test_signal', func_lator, use_symbol=False) signal.emit() await asyncio.sleep(0.1) assert func_lator.call_count == 1 Signal.teardown_aio_support()
async def test_signals_slots_mgr_add_slot_symbol(app_mock, signal, mocker): """ test add slot symbol """ signals_slots_mgr = SignalsSlotsManager() signals_slots_mgr.initialize(app_mock) Signal.setup_aio_support() app_mock.test_signal = signal func = mock.MagicMock() func.__name__ = 'fake_func' mock_F = mocker.patch('feeluown.fuoexec.fuoexec_F', return_value=func) signals_slots_mgr.add('app.test_signal', 'fake_func') signal.emit() await asyncio.sleep(0.1) mock_F.assert_called_once_with('fake_func') assert func.called is True Signal.teardown_aio_support()
async def signal_aio_support(): Signal.setup_aio_support() yield Signal.teardown_aio_support()
def exit(self): self.about_to_shutdown.emit(self) Signal.teardown_aio_support() self.player.stop()
def _shutdown_app(app): app.about_to_shutdown.emit(app) app.player.stop() app.player.shutdown() Signal.teardown_aio_support()