OSDN Git Service

b2615a3731f8b9f60b5b10b98963787be307a73f
[android-x86/external-webkit.git] / WebKitTools / Scripts / webkitpy / layout_tests / port / http_lock.py
1 #!/usr/bin/env python
2 # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Szeged
3 # Copyright (C) 2010 Andras Becsi (abecsi@inf.u-szeged.hu), University of Szeged
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 """This class helps to block NRWT threads when more NRWTs run
29 http and websocket tests in a same time."""
30
31 import glob
32 import os
33 import sys
34 import tempfile
35 import time
36
37
38 class HttpLock(object):
39
40     def __init__(self, lock_path, lock_file_prefix="WebKitHttpd.lock.",
41                  guard_lock="WebKit.lock"):
42         if not lock_path:
43             self._lock_path = tempfile.gettempdir()
44         self._lock_file_prefix = lock_file_prefix
45         self._lock_file_path_prefix = os.path.join(self._lock_path,
46                                                    self._lock_file_prefix)
47         self._guard_lock_file = os.path.join(self._lock_path, guard_lock)
48         self._process_lock_file_name = ""
49
50     def cleanup_http_lock(self):
51         """Delete the lock file if exists."""
52         if os.path.exists(self._process_lock_file_name):
53             os.unlink(self._process_lock_file_name)
54
55     def _extract_lock_number(self, lock_file_name):
56         """Return the lock number from lock file."""
57         prefix_length = len(self._lock_file_path_prefix)
58         return int(lock_file_name[prefix_length:])
59
60     def _lock_file_list(self):
61         """Return the list of lock files sequentially."""
62         lock_list = glob.glob(self._lock_file_path_prefix + '*')
63         lock_list.sort(key=self._extract_lock_number)
64         return lock_list
65
66     def _next_lock_number(self):
67         """Return the next available lock number."""
68         lock_list = self._lock_file_list()
69         if not lock_list:
70             return 0
71         return self._extract_lock_number(lock_list[-1]) + 1
72
73     def _check_pid(self, current_pid):
74         """Return True if pid is alive, otherwise return False.
75         FIXME: os.kill() doesn't work on Windows for checking if
76         a pid is alive, so always return True"""
77         if sys.platform in ('darwin', 'linux2'):
78             try:
79                 os.kill(current_pid, 0)
80             except OSError:
81                 return False
82         return True
83
84     def _curent_lock_pid(self):
85         """Return with the current lock pid. If the lock is not valid
86         it deletes the lock file."""
87         lock_list = self._lock_file_list()
88         if not lock_list:
89             return
90         try:
91             current_lock_file = open(lock_list[0], 'r')
92             current_pid = current_lock_file.readline()
93             current_lock_file.close()
94             if not (current_pid and self._check_pid(int(current_pid))):
95                 os.unlink(lock_list[0])
96                 return
97         except IOError, OSError:
98             return
99         return int(current_pid)
100
101     def _create_lock_file(self):
102         """The lock files are used to schedule the running test sessions in first
103         come first served order. The sequential guard lock ensures that the lock
104         numbers are sequential."""
105         while(True):
106             try:
107                 sequential_guard_lock = os.open(self._guard_lock_file, os.O_CREAT | os.O_EXCL)
108                 self._process_lock_file_name = (self._lock_file_path_prefix +
109                                                 str(self._next_lock_number()))
110                 lock_file = open(self._process_lock_file_name, 'w')
111                 lock_file.write(str(os.getpid()))
112                 lock_file.close()
113                 os.close(sequential_guard_lock)
114                 os.unlink(self._guard_lock_file)
115                 break
116             except OSError:
117                 pass
118
119     def wait_for_httpd_lock(self):
120         """Create a lock file and wait until it's turn comes."""
121         self._create_lock_file()
122         while self._curent_lock_pid() != os.getpid():
123             time.sleep(1)