1 #!/usr/bin/env python 2 # Copyright (C) 2010 Gabor Rapcsanyi (rgabor (at] inf.u-szeged.hu), University of Szeged 3 # 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions 8 # are met: 9 # 1. Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # 2. Redistributions in binary form must reproduce the above copyright 12 # notice, this list of conditions and the following disclaimer in the 13 # documentation and/or other materials provided with the distribution. 14 # 15 # THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY 16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR 19 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 import glob 28 import http_lock 29 import os 30 import unittest 31 32 33 class HttpLockTest(unittest.TestCase): 34 35 def __init__(self, testFunc): 36 self.http_lock_obj = http_lock.HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock") 37 self.lock_file_path_prefix = os.path.join(self.http_lock_obj._lock_path, 38 self.http_lock_obj._lock_file_prefix) 39 self.lock_file_name = self.lock_file_path_prefix + "0" 40 self.guard_lock_file = self.http_lock_obj._guard_lock_file 41 self.clean_all_lockfile() 42 unittest.TestCase.__init__(self, testFunc) 43 44 def clean_all_lockfile(self): 45 if os.path.exists(self.guard_lock_file): 46 os.unlink(self.guard_lock_file) 47 lock_list = glob.glob(self.lock_file_path_prefix + '*') 48 for file_name in lock_list: 49 os.unlink(file_name) 50 51 def assertEqual(self, first, second): 52 if first != second: 53 self.clean_all_lockfile() 54 unittest.TestCase.assertEqual(self, first, second) 55 56 def _check_lock_file(self): 57 if os.path.exists(self.lock_file_name): 58 pid = os.getpid() 59 lock_file = open(self.lock_file_name, 'r') 60 lock_file_pid = lock_file.readline() 61 lock_file.close() 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_obj._create_lock_file() 68 69 self.assertEqual(True, self._check_lock_file()) 70 self.assertEqual(1, self.http_lock_obj._next_lock_number()) 71 72 self.http_lock_obj.cleanup_http_lock() 73 74 self.assertEqual(False, self._check_lock_file()) 75 self.assertEqual(0, self.http_lock_obj._next_lock_number()) 76 77 def test_extract_lock_number(self,): 78 lock_file_list = ( 79 self.lock_file_path_prefix + "00", 80 self.lock_file_path_prefix + "9", 81 self.lock_file_path_prefix + "001", 82 self.lock_file_path_prefix + "021", 83 ) 84 85 expected_number_list = (0, 9, 1, 21) 86 87 for lock_file, expected in zip(lock_file_list, expected_number_list): 88 self.assertEqual(self.http_lock_obj._extract_lock_number(lock_file), expected) 89 90 def test_lock_file_list(self): 91 lock_file_list = [ 92 self.lock_file_path_prefix + "6", 93 self.lock_file_path_prefix + "1", 94 self.lock_file_path_prefix + "4", 95 self.lock_file_path_prefix + "3", 96 ] 97 98 expected_file_list = [ 99 self.lock_file_path_prefix + "1", 100 self.lock_file_path_prefix + "3", 101 self.lock_file_path_prefix + "4", 102 self.lock_file_path_prefix + "6", 103 ] 104 105 for file_name in lock_file_list: 106 open(file_name, 'w') 107 108 self.assertEqual(self.http_lock_obj._lock_file_list(), expected_file_list) 109 110 for file_name in lock_file_list: 111 os.unlink(file_name) 112