Revision a95c53ea test/ganeti.locking_unittest.py

b/test/ganeti.locking_unittest.py
1780 1780
    self.assertRaises(Queue.Empty, self.done.get_nowait)
1781 1781
    self.assertRaises(Queue.Empty, done_two.get_nowait)
1782 1782

  
1783
  def testNamesWithOpportunisticAndTimeout(self):
1784
    self.assertRaises(AssertionError, self.ls.acquire,
1785
                      ["one", "two"], timeout=1.0, opportunistic=True)
1786

  
1787
  def testOpportunisticWithUnknownName(self):
1788
    name = "unknown"
1789
    self.assertFalse(name in self.ls._names())
1790
    result = self.ls.acquire(name, opportunistic=True)
1791
    self.assertFalse(result)
1792
    self.assertFalse(self.ls.list_owned())
1793

  
1794
    result = self.ls.acquire(["two", name], opportunistic=True)
1795
    self.assertEqual(result, set(["two"]))
1796
    self.assertEqual(self.ls.list_owned(), set(["two"]))
1797

  
1798
    self.ls.release()
1799

  
1800
  def testSimpleOpportunisticAcquisition(self):
1801
    self.assertEquals(self.ls._names(), set(["one", "two", "three"]))
1802

  
1803
    # Hold a lock in main thread
1804
    self.assertEqual(self.ls.acquire("two", shared=0), set(["two"]))
1805

  
1806
    def fn():
1807
      # The lock "two" is held by the main thread
1808
      result = self.ls.acquire(["one", "two"], shared=0, opportunistic=True)
1809
      self.assertEqual(result, set(["one"]))
1810
      self.assertEqual(self.ls.list_owned(), set(["one"]))
1811
      self.assertFalse(self.ls._get_lock().is_owned())
1812

  
1813
      self.ls.release()
1814
      self.assertFalse(self.ls.list_owned())
1815

  
1816
      # Try to acquire the lock held by the main thread
1817
      result = self.ls.acquire(["two"], shared=0, opportunistic=True)
1818
      self.assertFalse(self.ls._get_lock().is_owned())
1819
      self.assertFalse(result)
1820
      self.assertFalse(self.ls.list_owned())
1821

  
1822
      # Try to acquire all locks
1823
      result = self.ls.acquire(locking.ALL_SET, shared=0, opportunistic=True)
1824
      self.assertTrue(self.ls._get_lock().is_owned(),
1825
                      msg="Internal lock is not owned")
1826
      self.assertEqual(result, set(["one", "three"]))
1827
      self.assertEqual(self.ls.list_owned(), set(["one", "three"]))
1828

  
1829
      self.ls.release()
1830

  
1831
      self.assertFalse(self.ls.list_owned())
1832

  
1833
      self.done.put(True)
1834

  
1835
    self._addThread(target=fn)
1836

  
1837
    # Wait for threads to finish
1838
    self._waitThreads()
1839

  
1840
    self.assertEqual(self.ls.list_owned(), set(["two"]))
1841

  
1842
    self.ls.release()
1843
    self.assertFalse(self.ls.list_owned())
1844
    self.assertFalse(self.ls._get_lock().is_owned())
1845

  
1846
    self.assertTrue(self.done.get_nowait())
1847
    self.assertRaises(Queue.Empty, self.done.get_nowait)
1848

  
1849
  def testOpportunisticAcquisitionWithoutNamesExpires(self):
1850
    self.assertEquals(self.ls._names(), set(["one", "two", "three"]))
1851

  
1852
    # Hold all locks in main thread
1853
    self.ls.acquire(locking.ALL_SET, shared=0)
1854
    self.assertTrue(self.ls._get_lock().is_owned())
1855

  
1856
    def fn():
1857
      # Try to acquire all locks in separate thread
1858
      result = self.ls.acquire(locking.ALL_SET, shared=0, opportunistic=True,
1859
                               timeout=0.1)
1860
      self.assertFalse(result)
1861
      self.assertFalse(self.ls._get_lock().is_owned())
1862
      self.assertFalse(self.ls.list_owned())
1863

  
1864
      # Try once more without a timeout
1865
      self.assertFalse(self.ls.acquire("one", shared=0, opportunistic=True))
1866

  
1867
      self.done.put(True)
1868

  
1869
    self._addThread(target=fn)
1870

  
1871
    # Wait for threads to finish
1872
    self._waitThreads()
1873

  
1874
    self.assertEqual(self.ls.list_owned(), set(["one", "two", "three"]))
1875

  
1876
    self.ls.release()
1877
    self.assertFalse(self.ls.list_owned())
1878
    self.assertFalse(self.ls._get_lock().is_owned(shared=0))
1879

  
1880
    self.assertTrue(self.done.get_nowait())
1881
    self.assertRaises(Queue.Empty, self.done.get_nowait)
1882

  
1883
  def testSharedOpportunisticAcquisitionWithoutNames(self):
1884
    self.assertEquals(self.ls._names(), set(["one", "two", "three"]))
1885

  
1886
    # Hold all locks in main thread
1887
    self.ls.acquire(locking.ALL_SET, shared=1)
1888
    self.assertTrue(self.ls._get_lock().is_owned(shared=1))
1889

  
1890
    def fn():
1891
      # Try to acquire all locks in separate thread in shared mode
1892
      result = self.ls.acquire(locking.ALL_SET, shared=1, opportunistic=True,
1893
                               timeout=0.1)
1894
      self.assertEqual(result, set(["one", "two", "three"]))
1895
      self.assertTrue(self.ls._get_lock().is_owned(shared=1))
1896
      self.ls.release()
1897
      self.assertFalse(self.ls._get_lock().is_owned())
1898

  
1899
      # Try one in exclusive mode
1900
      self.assertFalse(self.ls.acquire("one", shared=0, opportunistic=True))
1901

  
1902
      self.done.put(True)
1903

  
1904
    self._addThread(target=fn)
1905

  
1906
    # Wait for threads to finish
1907
    self._waitThreads()
1908

  
1909
    self.assertEqual(self.ls.list_owned(), set(["one", "two", "three"]))
1910

  
1911
    self.ls.release()
1912
    self.assertFalse(self.ls.list_owned())
1913
    self.assertFalse(self.ls._get_lock().is_owned())
1914

  
1915
    self.assertTrue(self.done.get_nowait())
1916
    self.assertRaises(Queue.Empty, self.done.get_nowait)
1917

  
1918
  def testLockDeleteWithOpportunisticAcquisition(self):
1919
    # This test exercises some code handling LockError on acquisition, that is
1920
    # after all lock names have been gathered. This shouldn't happen in reality
1921
    # as removing locks from the set requires the lockset-internal lock, but
1922
    # the code should handle the situation anyway.
1923
    ready = threading.Event()
1924
    finished = threading.Event()
1925

  
1926
    self.assertEquals(self.ls._names(), set(["one", "two", "three"]))
1927

  
1928
    # Thread function to delete lock
1929
    def fn():
1930
      # Wait for notification
1931
      ready.wait()
1932

  
1933
      # Delete lock named "two" by accessing lockset-internal data
1934
      ld = self.ls._get_lockdict()
1935
      self.assertTrue(ld["two"].delete())
1936

  
1937
      self.done.put("deleted.two")
1938

  
1939
      # Notify helper
1940
      finished.set()
1941

  
1942
    self._addThread(target=fn)
1943

  
1944
    # Notification helper, called when lock already holds internal lock.
1945
    # Therefore only one of the locks not yet locked can be deleted.
1946
    def notify(name):
1947
      self.done.put("notify.%s" % name)
1948

  
1949
      if name == "one":
1950
        # Tell helper thread to delete lock "two"
1951
        ready.set()
1952
        finished.wait()
1953

  
1954
    # Hold all locks in main thread
1955
    self.ls.acquire(locking.ALL_SET, shared=0, test_notify=notify)
1956
    self.assertEqual(self.ls.list_owned(), set(["one", "three"]))
1957

  
1958
    # Wait for threads to finish
1959
    self._waitThreads()
1960

  
1961
    # Release all locks
1962
    self.ls.release()
1963
    self.assertFalse(self.ls.list_owned())
1964
    self.assertFalse(self.ls._get_lock().is_owned())
1965

  
1966
    self.assertEqual(self.done.get_nowait(), "notify.one")
1967
    self.assertEqual(self.done.get_nowait(), "deleted.two")
1968
    self.assertEqual(self.done.get_nowait(), "notify.three")
1969
    self.assertEqual(self.done.get_nowait(), "notify.two")
1970
    self.assertRaises(Queue.Empty, self.done.get_nowait)
1971

  
1972

  
1973
class TestGetLsAcquireModeAndTimeouts(unittest.TestCase):
1974
  def setUp(self):
1975
    self.fn = locking._GetLsAcquireModeAndTimeouts
1976

  
1977
  def testOpportunisticWithoutNames(self):
1978
    (mode, ls_timeout_fn, timeout_fn) = self.fn(False, None, True)
1979
    self.assertEqual(mode, locking._LS_ACQUIRE_OPPORTUNISTIC)
1980
    self.assertTrue(ls_timeout_fn is None)
1981
    self.assertEqual(timeout_fn(), 0)
1982

  
1983
  def testAllInputCombinations(self):
1984
    for want_all in [False, True]:
1985
      for timeout in [None, 0, 100]:
1986
        for opportunistic in [False, True]:
1987
          if (opportunistic and
1988
              not want_all and
1989
              timeout is not None):
1990
            # Can't accept a timeout when acquiring opportunistically
1991
            self.assertRaises(AssertionError, self.fn,
1992
                              want_all, timeout, opportunistic)
1993
          else:
1994
            (mode, ls_timeout_fn, timeout_fn) = \
1995
              self.fn(want_all, timeout, opportunistic)
1996

  
1997
            if opportunistic:
1998
              self.assertEqual(mode, locking._LS_ACQUIRE_OPPORTUNISTIC)
1999
              self.assertEqual(timeout_fn(), 0)
2000
            else:
2001
              self.assertTrue(callable(timeout_fn))
2002
              if want_all:
2003
                self.assertEqual(mode, locking._LS_ACQUIRE_ALL)
2004
              else:
2005
                self.assertEqual(mode, locking._LS_ACQUIRE_EXACT)
2006

  
2007
            if want_all:
2008
              self.assertTrue(callable(ls_timeout_fn))
2009
            else:
2010
              self.assertTrue(ls_timeout_fn is None)
2011

  
1783 2012

  
1784 2013
class TestGanetiLockManager(_ThreadedTestCase):
1785 2014
  def setUp(self):

Also available in: Unified diff