http_lock_unittest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Szeged
  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. from http_lock import HttpLock
  26. import os # Used for os.getpid()
  27. import unittest2 as unittest
  28. from webkitpy.common.system.filesystem_mock import MockFileSystem
  29. from webkitpy.common.system.executive_mock import MockExecutive
  30. # FIXME: These tests all touch the real disk, but could be written to a MockFileSystem instead.
  31. class HttpLockTestWithRealFileSystem(unittest.TestCase):
  32. # FIXME: Unit tests do not use an __init__ method, but rather setUp and tearDown methods.
  33. def __init__(self, testFunc):
  34. self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock")
  35. self.filesystem = self.http_lock._filesystem # FIXME: We should be passing in a MockFileSystem instead.
  36. self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_path, self.http_lock._lock_file_prefix)
  37. self.lock_file_name = self.lock_file_path_prefix + "0"
  38. self.guard_lock_file = self.http_lock._guard_lock_file
  39. self.clean_all_lockfile()
  40. unittest.TestCase.__init__(self, testFunc)
  41. def clean_all_lockfile(self):
  42. if self.filesystem.exists(self.guard_lock_file):
  43. self.filesystem.remove(self.guard_lock_file)
  44. lock_list = self.filesystem.glob(self.lock_file_path_prefix + '*')
  45. for file_name in lock_list:
  46. self.filesystem.remove(file_name)
  47. def assertEqual(self, first, second):
  48. if first != second:
  49. self.clean_all_lockfile()
  50. unittest.TestCase.assertEqual(self, first, second)
  51. def _check_lock_file(self):
  52. if self.filesystem.exists(self.lock_file_name):
  53. pid = os.getpid()
  54. lock_file_pid = self.filesystem.read_text_file(self.lock_file_name)
  55. self.assertEqual(pid, int(lock_file_pid))
  56. return True
  57. return False
  58. def test_lock_lifecycle(self):
  59. self.http_lock._create_lock_file()
  60. self.assertEqual(True, self._check_lock_file())
  61. self.assertEqual(1, self.http_lock._next_lock_number())
  62. self.http_lock.cleanup_http_lock()
  63. self.assertEqual(False, self._check_lock_file())
  64. self.assertEqual(0, self.http_lock._next_lock_number())
  65. class HttpLockTest(unittest.TestCase):
  66. def setUp(self):
  67. self.filesystem = MockFileSystem()
  68. self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock", filesystem=self.filesystem, executive=MockExecutive())
  69. # FIXME: Shouldn't we be able to get these values from the http_lock object directly?
  70. self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_path, self.http_lock._lock_file_prefix)
  71. self.lock_file_name = self.lock_file_path_prefix + "0"
  72. def test_current_lock_pid(self):
  73. # FIXME: Once Executive wraps getpid, we can mock this and not use a real pid.
  74. current_pid = os.getpid()
  75. self.http_lock._filesystem.write_text_file(self.lock_file_name, str(current_pid))
  76. self.assertEqual(self.http_lock._current_lock_pid(), current_pid)
  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. expected_number_list = (0, 9, 1, 21)
  85. for lock_file, expected in zip(lock_file_list, expected_number_list):
  86. self.assertEqual(self.http_lock._extract_lock_number(lock_file), expected)
  87. def test_lock_file_list(self):
  88. self.http_lock._filesystem = MockFileSystem({
  89. self.lock_file_path_prefix + "6": "",
  90. self.lock_file_path_prefix + "1": "",
  91. self.lock_file_path_prefix + "4": "",
  92. self.lock_file_path_prefix + "3": "",
  93. })
  94. expected_file_list = [
  95. self.lock_file_path_prefix + "1",
  96. self.lock_file_path_prefix + "3",
  97. self.lock_file_path_prefix + "4",
  98. self.lock_file_path_prefix + "6",
  99. ]
  100. self.assertEqual(self.http_lock._lock_file_list(), expected_file_list)