def testPromptWithTimeoutTimedOut(self, rawInputMock): # NOTE: py.test by default captures console output and patches console input # such that raw_input fails. Although not ideal, we have to patch # raw_input with something else that blocks and will be interrupted by # SIGINT. rawInputMock.side_effect = lambda *args: time.sleep(10) with self.assertRaises(prompt_utils.PromptTimeout): prompt_utils.promptWithTimeout(promptText="Ask user something", timeout=0.001)
def _warnAboutDestructiveAction(): """Warn user about destructive action. :raises nta.utils.prompt_utils.PromptTimeout: if prompt timed out :raises UserAbortedOperation: if user chose to abort the operation """ expectedAnswer = "Yes-{randomNum}".format(randomNum=random.randint(1, 30),) promptText = ( "\n" "ATTENTION! You are about to reset Taurus Engine's message " "queues/exchanges, model checkpoints, and repository.\n" "\n" "To back out immediately without making any changes, feel free to type " "anything but \"{expectedAnswer}\" in the prompt below, and press " "return. (auto-abort in {timeout} seconds)\n" "\n" "Are you sure you want to continue? " .format(expectedAnswer=expectedAnswer, timeout=_WARNING_PROMPT_TIMEOUT_SEC)) answer = prompt_utils.promptWithTimeout( promptText=promptText, timeout=_WARNING_PROMPT_TIMEOUT_SEC) if answer != expectedAnswer: raise UserAbortedOperation( "User aborted operation from warning prompt; " "expectedAnswer={expectedAnswer}, but got actual={actual}".format( expectedAnswer=expectedAnswer, actual=answer))
def _warnAboutDestructiveAction(): """Warn user about destructive action. :raises nta.utils.prompt_utils.PromptTimeout: if prompt timed out :raises UserAbortedOperation: if user chose to abort the operation """ expectedAnswer = "Yes-{randomNum}".format(randomNum=random.randint(1, 30), ) promptText = ( "\n" "ATTENTION! You are about to reset Taurus Engine's message " "queues/exchanges, model checkpoints, and repository.\n" "\n" "To back out immediately without making any changes, feel free to type " "anything but \"{expectedAnswer}\" in the prompt below, and press " "return. (auto-abort in {timeout} seconds)\n" "\n" "Are you sure you want to continue? ".format( expectedAnswer=expectedAnswer, timeout=_WARNING_PROMPT_TIMEOUT_SEC)) answer = prompt_utils.promptWithTimeout( promptText=promptText, timeout=_WARNING_PROMPT_TIMEOUT_SEC) if answer != expectedAnswer: raise UserAbortedOperation( "User aborted operation from warning prompt; " "expectedAnswer={expectedAnswer}, but got actual={actual}".format( expectedAnswer=expectedAnswer, actual=answer))
def testPromptWithTimeoutNoTimeout(self, rawInputMock): rawInputMock.return_value = "Yes" answer = prompt_utils.promptWithTimeout(promptText="Ask user something", timeout=10) self.assertEqual(answer, "Yes") rawInputMock.assert_called_once_with("Ask user something")
def _warnAboutDestructiveAction(timeout, tickerSymbols, engineServer): """Prompt user about continuing with the destructive action :param float timeout: Timeout for the warning prompt :param sequence tickerSymbols: stock ticker symbols of companies to be deleted :param str engineServer: dns name of ip addres of Taurus API server :raises WarningPromptTimeout: if warning prompt timed out :raises UserAbortedOperation: if user chose to abort the operation """ expectedAnswer = "Yes-{randomNum}".format(randomNum=random.randint(1, 30), ) if len(tickerSymbols) <= 5: tickersInPrompt = "[{tickers}]".format( tickers=", ".join(tickerSymbols)) else: tickersInPrompt = "[{tickers}, ... and {more} more]".format( tickers=tickerSymbols[:5], more=len(tickerSymbols) - 5) promptText = ( "Attention! You are about to delete {total} companies {tickers} from " "Taurus Collector and their metrics/models from Taurus Engine {engine}\n" "\n" "To back out immediately without making any changes, feel free to type " "anything but \"{expectedAnswer}\" in the prompt below, and press " "return. (auto-abort in {timeout} seconds)\n" "\n" "Are you sure you want to continue? ".format( total=len(tickerSymbols), tickers=tickersInPrompt, engine=engineServer, expectedAnswer=expectedAnswer, timeout=timeout)) try: answer = prompt_utils.promptWithTimeout(promptText=promptText, timeout=timeout) except prompt_utils.PromptTimeout: raise WarningPromptTimeout("Warning prompt timed out") if answer.strip() != expectedAnswer: raise UserAbortedOperation( "User aborted operation from warning prompt")
def _warnAboutDestructiveAction(timeout, tickerSymbols, engineServer): """Prompt user about continuing with the destructive action :param float timeout: Timeout for the warning prompt :param sequence tickerSymbols: stock ticker symbols of companies to be deleted :param str engineServer: dns name of ip addres of Taurus API server :raises WarningPromptTimeout: if warning prompt timed out :raises UserAbortedOperation: if user chose to abort the operation """ expectedAnswer = "Yes-{randomNum}".format(randomNum=random.randint(1, 30)) if len(tickerSymbols) <= 5: tickersInPrompt = "[{tickers}]".format(tickers=", ".join(tickerSymbols)) else: tickersInPrompt = "[{tickers}, ... and {more} more]".format( tickers=tickerSymbols[:5], more=len(tickerSymbols) - 5 ) promptText = ( "Attention! You are about to delete {total} companies {tickers} from " "Taurus Collector and their metrics/models from Taurus Engine {engine}\n" "\n" "To back out immediately without making any changes, feel free to type " 'anything but "{expectedAnswer}" in the prompt below, and press ' "return. (auto-abort in {timeout} seconds)\n" "\n" "Are you sure you want to continue? ".format( total=len(tickerSymbols), tickers=tickersInPrompt, engine=engineServer, expectedAnswer=expectedAnswer, timeout=timeout, ) ) try: answer = prompt_utils.promptWithTimeout(promptText=promptText, timeout=timeout) except prompt_utils.PromptTimeout: raise WarningPromptTimeout("Warning prompt timed out") if answer.strip() != expectedAnswer: raise UserAbortedOperation("User aborted operation from warning prompt")