diff --git a/test/test_popen.py b/test/test_popen.py index 28bb8e898bbd634e073d551b529b8c92aa9f9b71..53d9a9fbd58f71b20951f6ef231d8ac9ea6ac5f0 100644 --- a/test/test_popen.py +++ b/test/test_popen.py @@ -1,25 +1,12 @@ -#!/usr/bin/env python -import unittest - import time -import signal +import pcl_expect from pcl_expect import Controller from pcl_expect.popen import Popen -class Timeout(Exception): pass - -def handler(signo, stk): - raise Timeout() - -class TestPopen(unittest.TestCase): - def setUp(self): - signal.signal(signal.SIGALRM, handler) - signal.alarm(20) - - def tearDown(self): - signal.alarm(0) +import test.base +class TestPopen(test.base.TestCase): def test_popen_sleep_1(self): t0 = time.time() sleeper = Popen('sleep 1') @@ -34,8 +21,8 @@ class TestPopen(unittest.TestCase): status = sleeper.close() self.assertEqual(status, None) t1 = time.time() - self.assertAlmostEqual(t0 + 1.0, t1, 1, - 'sleep 1 slept too long or too short') + self.assertTimeDiff(t0, t1, 1.0) + def test_popen_sleep_1_compat(self): t0 = time.time() sleeper = Popen('sleep 1') @@ -50,8 +37,7 @@ class TestPopen(unittest.TestCase): status = sleeper.close() self.assertEqual(status, None) t1 = time.time() - self.assertAlmostEqual(t0 + 1.0, t1, 1, - 'sleep 1 slept too long or too short') + self.assertTimeDiff(t0, t1, 1.0) def test_read_hello(self): hello = Popen('echo hello, world') @@ -100,5 +86,35 @@ class TestPopen(unittest.TestCase): status = hello.close() self.assertEqual(status, None) -if __name__ == '__main__': - unittest.main() + def test_read_hello_no_eof_handling(self): + hello = Popen('echo hello, world') + seen = False + x = Controller() + try: + while x.loop(): + if hello.re(x, 'hello, world\n'): + if seen: + self.fail("got hello twice") + seen = 1 + self.fail('no exception raised') + except pcl_expect.UnhandledEof: + pass + status = hello.close() + self.assertEqual(status, None) + self.assertEqual(seen, 1) + + def test_read_hello_no_eof_handling_no_exception(self): + hello = Popen('echo hello, world') + seen = False + env = pcl_expect.Environment() + env.set_eof_raises_exception(False) + x = env.controller() + while x.loop(): + if hello.re(x, 'hello, world\n'): + if seen: + self.fail("got hello twice") + seen = 1 + status = hello.close() + self.assertEqual(status, None) + self.assertEqual(seen, 1) +