• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2010 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import unittest
30
31from mechanize import HTTPError
32from webkitpy.networktransaction import NetworkTransaction, NetworkTimeout
33
34class NetworkTransactionTest(unittest.TestCase):
35    exception = Exception("Test exception")
36
37    def test_success(self):
38        transaction = NetworkTransaction()
39        self.assertEqual(transaction.run(lambda: 42), 42)
40
41    def _raise_exception(self):
42        raise self.exception
43
44    def test_exception(self):
45        transaction = NetworkTransaction()
46        did_process_exception = False
47        did_throw_exception = True
48        try:
49            transaction.run(lambda: self._raise_exception())
50            did_throw_exception = False
51        except Exception, e:
52            did_process_exception = True
53            self.assertEqual(e, self.exception)
54        self.assertTrue(did_throw_exception)
55        self.assertTrue(did_process_exception)
56
57    def _raise_http_error(self):
58        self._run_count += 1
59        if self._run_count < 3:
60            raise HTTPError("http://example.com/", 500, "inteneral server error", None, None)
61        return 42
62
63    def test_retry(self):
64        self._run_count = 0
65        transaction = NetworkTransaction(initial_backoff_seconds=0)
66        self.assertEqual(transaction.run(lambda: self._raise_http_error()), 42)
67        self.assertEqual(self._run_count, 3)
68
69    def test_timeout(self):
70        self._run_count = 0
71        transaction = NetworkTransaction(initial_backoff_seconds=60*60, timeout_seconds=60)
72        did_process_exception = False
73        did_throw_exception = True
74        try:
75            transaction.run(lambda: self._raise_http_error())
76            did_throw_exception = False
77        except NetworkTimeout, e:
78            did_process_exception = True
79        self.assertTrue(did_throw_exception)
80        self.assertTrue(did_process_exception)
81