Changeset 624 for trunk


Ignore:
Timestamp:
Nov 25, 2015, 8:37:33 AM (4 years ago)
Author:
cito
Message:

Avoid test thread running forever if it fails

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/TEST_PyGreSQL_classic.py

    r621 r624  
    262262        self.notify_timeout = False
    263263        # Listen for 'event_1'.
    264         target = fut('event_1', self.notify_callback, arg_dict)
     264        target = fut('event_1', self.notify_callback, arg_dict, 5)
    265265        thread = Thread(None, target)
    266266        thread.start()
    267         # Wait until the thread has started.
    268         for n in range(500):
    269             if target.listening:
    270                 break
    271             sleep(0.01)
    272         self.assertTrue(target.listening)
    273         self.assertTrue(thread.isAlive())
    274         # Open another connection for sending notifications.
    275         db2 = opendb()
    276         # Generate notification from the other connection.
    277         if two_payloads:
    278             db2.begin()
    279         if call_notify:
     267        try:
     268            # Wait until the thread has started.
     269            for n in range(500):
     270                if target.listening:
     271                    break
     272                sleep(0.01)
     273            self.assertTrue(target.listening)
     274            self.assertTrue(thread.isAlive())
     275            # Open another connection for sending notifications.
     276            db2 = opendb()
     277            # Generate notification from the other connection.
    280278            if two_payloads:
    281                 target.notify(db2, payload='payload 0')
    282             target.notify(db2, payload='payload 1')
    283         else:
     279                db2.begin()
     280            if call_notify:
     281                if two_payloads:
     282                    target.notify(db2, payload='payload 0')
     283                target.notify(db2, payload='payload 1')
     284            else:
     285                if two_payloads:
     286                    db2.query("notify event_1, 'payload 0'")
     287                db2.query("notify event_1, 'payload 1'")
    284288            if two_payloads:
    285                 db2.query("notify event_1, 'payload 0'")
    286             db2.query("notify event_1, 'payload 1'")
    287         if two_payloads:
    288             db2.commit()
    289         # Wait until the notification has been caught.
    290         for n in range(500):
    291             if arg_dict['called'] or self.notify_timeout:
    292                 break
    293             sleep(0.01)
    294         # Check that callback has been invoked.
    295         self.assertTrue(arg_dict['called'])
    296         self.assertEqual(arg_dict['event'], 'event_1')
    297         self.assertEqual(arg_dict['extra'], 'payload 1')
    298         self.assertTrue(isinstance(arg_dict['pid'], int))
    299         self.assertFalse(self.notify_timeout)
    300         arg_dict['called'] = False
    301         self.assertTrue(thread.isAlive())
    302         # Generate stop notification.
    303         if call_notify:
    304             target.notify(db2, stop=True, payload='payload 2')
    305         else:
    306             db2.query("notify stop_event_1, 'payload 2'")
    307         db2.close()
    308         # Wait until the notification has been caught.
    309         for n in range(500):
    310             if arg_dict['called'] or self.notify_timeout:
    311                 break
    312             sleep(0.01)
    313         # Check that callback has been invoked.
    314         self.assertTrue(arg_dict['called'])
    315         self.assertEqual(arg_dict['event'], 'stop_event_1')
    316         self.assertEqual(arg_dict['extra'], 'payload 2')
    317         self.assertTrue(isinstance(arg_dict['pid'], int))
    318         self.assertFalse(self.notify_timeout)
    319         thread.join(5)
    320         self.assertFalse(thread.isAlive())
    321         self.assertFalse(target.listening)
    322         target.close()
     289                db2.commit()
     290            # Wait until the notification has been caught.
     291            for n in range(500):
     292                if arg_dict['called'] or self.notify_timeout:
     293                    break
     294                sleep(0.01)
     295            # Check that callback has been invoked.
     296            self.assertTrue(arg_dict['called'])
     297            self.assertEqual(arg_dict['event'], 'event_1')
     298            self.assertEqual(arg_dict['extra'], 'payload 1')
     299            self.assertTrue(isinstance(arg_dict['pid'], int))
     300            self.assertFalse(self.notify_timeout)
     301            arg_dict['called'] = False
     302            self.assertTrue(thread.isAlive())
     303            # Generate stop notification.
     304            if call_notify:
     305                target.notify(db2, stop=True, payload='payload 2')
     306            else:
     307                db2.query("notify stop_event_1, 'payload 2'")
     308            db2.close()
     309            # Wait until the notification has been caught.
     310            for n in range(500):
     311                if arg_dict['called'] or self.notify_timeout:
     312                    break
     313                sleep(0.01)
     314            # Check that callback has been invoked.
     315            self.assertTrue(arg_dict['called'])
     316            self.assertEqual(arg_dict['event'], 'stop_event_1')
     317            self.assertEqual(arg_dict['extra'], 'payload 2')
     318            self.assertTrue(isinstance(arg_dict['pid'], int))
     319            self.assertFalse(self.notify_timeout)
     320            thread.join(5)
     321            self.assertFalse(thread.isAlive())
     322            self.assertFalse(target.listening)
     323            target.close()
     324        except Exception:
     325            target.close()
     326            if thread.is_alive():
     327                thread.join(5)
    323328
    324329    def test_notify_other_options(self):
Note: See TracChangeset for help on using the changeset viewer.