initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / layout_tests / port / http_lock_unittest.py
1 #!/usr/bin/env python
2 # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@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 from http_lock import HttpLock
28 import os  # Used for os.getpid()
29 import unittest
30
31 from webkitpy.common.system.filesystem_mock import MockFileSystem
32 from webkitpy.tool.mocktool import MockExecutive
33
34
35 # FIXME: These tests all touch the real disk, but could be written to a MockFileSystem instead.
36 class HttpLockTestWithRealFileSystem(unittest.TestCase):
37     # FIXME: Unit tests do not use an __init__ method, but rather setUp and tearDown methods.
38     def __init__(self, testFunc):
39         self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock")
40         self.filesystem = self.http_lock._filesystem  # FIXME: We should be passing in a MockFileSystem instead.
41         self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_path, self.http_lock._lock_file_prefix)
42         self.lock_file_name = self.lock_file_path_prefix + "0"
43         self.guard_lock_file = self.http_lock._guard_lock_file
44         self.clean_all_lockfile()
45         unittest.TestCase.__init__(self, testFunc)
46
47     def clean_all_lockfile(self):
48         if self.filesystem.exists(self.guard_lock_file):
49             self.filesystem.unlink(self.guard_lock_file)
50         lock_list = self.filesystem.glob(self.lock_file_path_prefix + '*')
51         for file_name in lock_list:
52             self.filesystem.unlink(file_name)
53
54     def assertEqual(self, first, second):
55         if first != second:
56             self.clean_all_lockfile()
57         unittest.TestCase.assertEqual(self, first, second)
58
59     def _check_lock_file(self):
60         if self.filesystem.exists(self.lock_file_name):
61             pid = os.getpid()
62             lock_file_pid = self.filesystem.read_text_file(self.lock_file_name)
63             self.assertEqual(pid, int(lock_file_pid))
64             return True
65         return False
66
67     def test_lock_lifecycle(self):
68         self.http_lock._create_lock_file()
69
70         self.assertEqual(True, self._check_lock_file())
71         self.assertEqual(1, self.http_lock._next_lock_number())
72
73         self.http_lock.cleanup_http_lock()
74
75         self.assertEqual(False, self._check_lock_file())
76         self.assertEqual(0, self.http_lock._next_lock_number())
77
78
79 class HttpLockTest(unittest.TestCase):
80     def setUp(self):
81         self.filesystem = MockFileSystem()
82         self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock", filesystem=self.filesystem, executive=MockExecutive())
83         # FIXME: Shouldn't we be able to get these values from the http_lock object directly?
84         self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_path, self.http_lock._lock_file_prefix)
85         self.lock_file_name = self.lock_file_path_prefix + "0"
86
87     def test_current_lock_pid(self):
88         # FIXME: Once Executive wraps getpid, we can mock this and not use a real pid.
89         current_pid = os.getpid()
90         self.http_lock._filesystem.write_text_file(self.lock_file_name, str(current_pid))
91         self.assertEquals(self.http_lock._current_lock_pid(), current_pid)
92
93     def test_extract_lock_number(self):
94         lock_file_list = (
95             self.lock_file_path_prefix + "00",
96             self.lock_file_path_prefix + "9",
97             self.lock_file_path_prefix + "001",
98             self.lock_file_path_prefix + "021",
99         )
100
101         expected_number_list = (0, 9, 1, 21)
102
103         for lock_file, expected in zip(lock_file_list, expected_number_list):
104             self.assertEqual(self.http_lock._extract_lock_number(lock_file), expected)
105
106     def test_lock_file_list(self):
107         self.http_lock._filesystem = MockFileSystem({
108             self.lock_file_path_prefix + "6": "",
109             self.lock_file_path_prefix + "1": "",
110             self.lock_file_path_prefix + "4": "",
111             self.lock_file_path_prefix + "3": "",
112         })
113
114         expected_file_list = [
115             self.lock_file_path_prefix + "1",
116             self.lock_file_path_prefix + "3",
117             self.lock_file_path_prefix + "4",
118             self.lock_file_path_prefix + "6",
119         ]
120
121         self.assertEqual(self.http_lock._lock_file_list(), expected_file_list)