OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for rietveld.py.""" | 6 """Unit tests for rietveld.py.""" |
7 | 7 |
8 import logging | 8 import logging |
9 import os | 9 import os |
| 10 import socket |
10 import ssl | 11 import ssl |
11 import sys | 12 import sys |
12 import time | 13 import time |
13 import traceback | 14 import traceback |
14 import unittest | 15 import unittest |
15 | 16 |
16 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 17 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
17 | 18 |
18 from testing_support.patches_data import GIT, RAW | 19 from testing_support.patches_data import GIT, RAW |
19 from testing_support import auto_stub | 20 from testing_support import auto_stub |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 | 428 |
428 class ProbeException(Exception): | 429 class ProbeException(Exception): |
429 """Deep-probe a value.""" | 430 """Deep-probe a value.""" |
430 value = None | 431 value = None |
431 | 432 |
432 def __init__(self, value): | 433 def __init__(self, value): |
433 super(ProbeException, self).__init__() | 434 super(ProbeException, self).__init__() |
434 self.value = value | 435 self.value = value |
435 | 436 |
436 | 437 |
437 def MockSend(request_path, payload=None, | 438 def MockSend(*args, **kwargs): |
438 content_type="application/octet-stream", | |
439 timeout=None, | |
440 extra_headers=None, | |
441 **kwargs): | |
442 """Mock upload.py's Send() to probe the timeout value""" | 439 """Mock upload.py's Send() to probe the timeout value""" |
443 raise ProbeException(timeout) | 440 raise ProbeException(kwargs['timeout']) |
444 | 441 |
445 def MockSendTimeout(request_path, payload=None, | 442 |
446 content_type="application/octet-stream", | 443 def MockSendTimeout(*args, **kwargs): |
447 timeout=None, | |
448 extra_headers=None, | |
449 **kwargs): | |
450 """Mock upload.py's Send() to raise SSLError""" | 444 """Mock upload.py's Send() to raise SSLError""" |
451 raise ssl.SSLError('The read operation timed out') | 445 raise ssl.SSLError('The read operation timed out') |
452 | 446 |
453 | 447 |
| 448 def MockSocketConnectTimeout(*args, **kwargs): |
| 449 """Mock upload.py's Send() to raise socket.timeout""" |
| 450 raise socket.timeout('timed out') |
| 451 |
| 452 |
454 class DefaultTimeoutTest(auto_stub.TestCase): | 453 class DefaultTimeoutTest(auto_stub.TestCase): |
455 TESTED_CLASS = rietveld.Rietveld | 454 TESTED_CLASS = rietveld.Rietveld |
456 | 455 |
457 def setUp(self): | 456 def setUp(self): |
458 super(DefaultTimeoutTest, self).setUp() | 457 super(DefaultTimeoutTest, self).setUp() |
459 self.rietveld = self.TESTED_CLASS('url', None, 'email') | 458 self.rietveld = self.TESTED_CLASS('url', None, 'email') |
460 self.mock(self.rietveld.rpc_server, 'Send', MockSend) | 459 self.mock(self.rietveld.rpc_server, 'Send', MockSend) |
461 self.sleep_time = 0 | 460 self.sleep_time = 0 |
462 | 461 |
463 def test_timeout_get(self): | 462 def test_timeout_get(self): |
464 with self.assertRaises(ProbeException) as cm: | 463 with self.assertRaises(ProbeException) as cm: |
465 self.rietveld.get('/api/1234') | 464 self.rietveld.get('/api/1234') |
466 | 465 |
467 self.assertIsNotNone(cm.exception.value, 'Rietveld timeout was not set: %s' | 466 self.assertIsNotNone(cm.exception.value, 'Rietveld timeout was not set: %s' |
468 % traceback.format_exc()) | 467 % traceback.format_exc()) |
469 | 468 |
470 def test_timeout_post(self): | 469 def test_timeout_post(self): |
471 with self.assertRaises(ProbeException) as cm: | 470 with self.assertRaises(ProbeException) as cm: |
472 self.rietveld.post('/api/1234', [('key', 'data')]) | 471 self.rietveld.post('/api/1234', [('key', 'data')]) |
473 | 472 |
474 self.assertIsNotNone(cm.exception.value, 'Rietveld timeout was not set: %s' | 473 self.assertIsNotNone(cm.exception.value, 'Rietveld timeout was not set: %s' |
475 % traceback.format_exc()) | 474 % traceback.format_exc()) |
476 | 475 |
477 def MockSleep(self, t): | 476 def MockSleep(self, t): |
478 self.sleep_time = t | 477 self.sleep_time = t |
479 | 478 |
480 def test_ssl_timeout_post(self): | 479 def test_ssl_timeout_post(self): |
481 self.mock(self.rietveld.rpc_server, 'Send', MockSendTimeout) | 480 self.mock(self.rietveld.rpc_server, 'Send', MockSendTimeout) |
482 self.mock(time, 'sleep', self.MockSleep) | 481 self.mock(time, 'sleep', self.MockSleep) |
483 self.sleep_time = 0 | |
484 with self.assertRaises(ssl.SSLError): | 482 with self.assertRaises(ssl.SSLError): |
485 self.rietveld.post('/api/1234', [('key', 'data')]) | 483 self.rietveld.post('/api/1234', [('key', 'data')]) |
486 self.assertNotEqual(self.sleep_time, 0) | 484 self.assertNotEqual(self.sleep_time, 0) |
487 | 485 |
| 486 def test_socket_connect_timeout_post(self): |
| 487 self.mock(self.rietveld.rpc_server, 'Send', MockSocketConnectTimeout) |
| 488 self.mock(time, 'sleep', self.MockSleep) |
| 489 with self.assertRaises(socket.timeout): |
| 490 self.rietveld.post('/api/1234', [('key', 'data')]) |
| 491 self.assertNotEqual(self.sleep_time, 0) |
| 492 |
488 if __name__ == '__main__': | 493 if __name__ == '__main__': |
489 logging.basicConfig(level=[ | 494 logging.basicConfig(level=[ |
490 logging.ERROR, logging.INFO, logging.DEBUG][min(2, sys.argv.count('-v'))]) | 495 logging.ERROR, logging.INFO, logging.DEBUG][min(2, sys.argv.count('-v'))]) |
491 unittest.main() | 496 unittest.main() |
OLD | NEW |