| OLD | NEW |
| (Empty) |
| 1 # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Sze
ged | |
| 2 # | |
| 3 # All rights reserved. | |
| 4 # | |
| 5 # Redistribution and use in source and binary forms, with or without | |
| 6 # modification, are permitted provided that the following conditions | |
| 7 # are met: | |
| 8 # 1. Redistributions of source code must retain the above copyright | |
| 9 # notice, this list of conditions and the following disclaimer. | |
| 10 # 2. Redistributions in binary form must reproduce the above copyright | |
| 11 # notice, this list of conditions and the following disclaimer in the | |
| 12 # documentation and/or other materials provided with the distribution. | |
| 13 # | |
| 14 # THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY | |
| 15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR | |
| 18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 | |
| 26 from http_lock import HttpLock | |
| 27 import os # Used for os.getpid() | |
| 28 import unittest2 as unittest | |
| 29 | |
| 30 from webkitpy.common.system.filesystem_mock import MockFileSystem | |
| 31 from webkitpy.common.system.executive_mock import MockExecutive | |
| 32 | |
| 33 | |
| 34 # FIXME: These tests all touch the real disk, but could be written to a MockFile
System instead. | |
| 35 class HttpLockTestWithRealFileSystem(unittest.TestCase): | |
| 36 # FIXME: Unit tests do not use an __init__ method, but rather setUp and tear
Down methods. | |
| 37 def __init__(self, testFunc): | |
| 38 self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.loc
k") | |
| 39 self.filesystem = self.http_lock._filesystem # FIXME: We should be pass
ing in a MockFileSystem instead. | |
| 40 self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_p
ath, self.http_lock._lock_file_prefix) | |
| 41 self.lock_file_name = self.lock_file_path_prefix + "0" | |
| 42 self.guard_lock_file = self.http_lock._guard_lock_file | |
| 43 self.clean_all_lockfile() | |
| 44 unittest.TestCase.__init__(self, testFunc) | |
| 45 | |
| 46 def clean_all_lockfile(self): | |
| 47 if self.filesystem.exists(self.guard_lock_file): | |
| 48 self.filesystem.remove(self.guard_lock_file) | |
| 49 lock_list = self.filesystem.glob(self.lock_file_path_prefix + '*') | |
| 50 for file_name in lock_list: | |
| 51 self.filesystem.remove(file_name) | |
| 52 | |
| 53 def assertEqual(self, first, second): | |
| 54 if first != second: | |
| 55 self.clean_all_lockfile() | |
| 56 unittest.TestCase.assertEqual(self, first, second) | |
| 57 | |
| 58 def _check_lock_file(self): | |
| 59 if self.filesystem.exists(self.lock_file_name): | |
| 60 pid = os.getpid() | |
| 61 lock_file_pid = self.filesystem.read_text_file(self.lock_file_name) | |
| 62 self.assertEqual(pid, int(lock_file_pid)) | |
| 63 return True | |
| 64 return False | |
| 65 | |
| 66 def test_lock_lifecycle(self): | |
| 67 self.http_lock._create_lock_file() | |
| 68 | |
| 69 self.assertEqual(True, self._check_lock_file()) | |
| 70 self.assertEqual(1, self.http_lock._next_lock_number()) | |
| 71 | |
| 72 self.http_lock.cleanup_http_lock() | |
| 73 | |
| 74 self.assertEqual(False, self._check_lock_file()) | |
| 75 self.assertEqual(0, self.http_lock._next_lock_number()) | |
| 76 | |
| 77 | |
| 78 class HttpLockTest(unittest.TestCase): | |
| 79 def setUp(self): | |
| 80 self.filesystem = MockFileSystem() | |
| 81 self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.loc
k", filesystem=self.filesystem, executive=MockExecutive()) | |
| 82 # FIXME: Shouldn't we be able to get these values from the http_lock obj
ect directly? | |
| 83 self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_p
ath, self.http_lock._lock_file_prefix) | |
| 84 self.lock_file_name = self.lock_file_path_prefix + "0" | |
| 85 | |
| 86 def test_current_lock_pid(self): | |
| 87 # FIXME: Once Executive wraps getpid, we can mock this and not use a rea
l pid. | |
| 88 current_pid = os.getpid() | |
| 89 self.http_lock._filesystem.write_text_file(self.lock_file_name, str(curr
ent_pid)) | |
| 90 self.assertEqual(self.http_lock._current_lock_pid(), current_pid) | |
| 91 | |
| 92 def test_extract_lock_number(self): | |
| 93 lock_file_list = ( | |
| 94 self.lock_file_path_prefix + "00", | |
| 95 self.lock_file_path_prefix + "9", | |
| 96 self.lock_file_path_prefix + "001", | |
| 97 self.lock_file_path_prefix + "021", | |
| 98 ) | |
| 99 | |
| 100 expected_number_list = (0, 9, 1, 21) | |
| 101 | |
| 102 for lock_file, expected in zip(lock_file_list, expected_number_list): | |
| 103 self.assertEqual(self.http_lock._extract_lock_number(lock_file), exp
ected) | |
| 104 | |
| 105 def test_lock_file_list(self): | |
| 106 self.http_lock._filesystem = MockFileSystem({ | |
| 107 self.lock_file_path_prefix + "6": "", | |
| 108 self.lock_file_path_prefix + "1": "", | |
| 109 self.lock_file_path_prefix + "4": "", | |
| 110 self.lock_file_path_prefix + "3": "", | |
| 111 }) | |
| 112 | |
| 113 expected_file_list = [ | |
| 114 self.lock_file_path_prefix + "1", | |
| 115 self.lock_file_path_prefix + "3", | |
| 116 self.lock_file_path_prefix + "4", | |
| 117 self.lock_file_path_prefix + "6", | |
| 118 ] | |
| 119 | |
| 120 self.assertEqual(self.http_lock._lock_file_list(), expected_file_list) | |
| OLD | NEW |