Revision e4e35357 test/ganeti.locking_unittest.py

b/test/ganeti.locking_unittest.py
28 28
import Queue
29 29
import threading
30 30
import random
31
import gc
31 32
import itertools
32 33

  
33 34
from ganeti import constants
......
1914 1915

  
1915 1916
      self.assertEqual(len(self.lm._locks), 1)
1916 1917

  
1918
  def testDeleteAndRecreate(self):
1919
    lname = "TestLock101923193"
1920

  
1921
    # Create some locks with the same name and keep all references
1922
    locks = [locking.SharedLock(lname, monitor=self.lm)
1923
             for _ in range(5)]
1924

  
1925
    self.assertEqual(len(self.lm._locks), len(locks))
1926

  
1927
    result = self.lm.QueryLocks(["name", "mode", "owner"])
1928
    self.assertEqual(objects.QueryResponse.FromDict(result).data,
1929
                     [[(constants.RS_NORMAL, lname),
1930
                       (constants.RS_NORMAL, None),
1931
                       (constants.RS_NORMAL, None)]] * 5)
1932

  
1933
    locks[2].delete()
1934

  
1935
    # Check information order
1936
    result = self.lm.QueryLocks(["name", "mode", "owner"])
1937
    self.assertEqual(objects.QueryResponse.FromDict(result).data,
1938
                     [[(constants.RS_NORMAL, lname),
1939
                       (constants.RS_NORMAL, None),
1940
                       (constants.RS_NORMAL, None)]] * 2 +
1941
                     [[(constants.RS_NORMAL, lname),
1942
                       (constants.RS_NORMAL, "deleted"),
1943
                       (constants.RS_NORMAL, None)]] +
1944
                     [[(constants.RS_NORMAL, lname),
1945
                       (constants.RS_NORMAL, None),
1946
                       (constants.RS_NORMAL, None)]] * 2)
1947

  
1948
    locks[1].acquire(shared=0)
1949

  
1950
    last_status = [
1951
      [(constants.RS_NORMAL, lname),
1952
       (constants.RS_NORMAL, None),
1953
       (constants.RS_NORMAL, None)],
1954
      [(constants.RS_NORMAL, lname),
1955
       (constants.RS_NORMAL, "exclusive"),
1956
       (constants.RS_NORMAL, [threading.currentThread().getName()])],
1957
      [(constants.RS_NORMAL, lname),
1958
       (constants.RS_NORMAL, "deleted"),
1959
       (constants.RS_NORMAL, None)],
1960
      [(constants.RS_NORMAL, lname),
1961
       (constants.RS_NORMAL, None),
1962
       (constants.RS_NORMAL, None)],
1963
      [(constants.RS_NORMAL, lname),
1964
       (constants.RS_NORMAL, None),
1965
       (constants.RS_NORMAL, None)],
1966
      ]
1967

  
1968
    # Check information order
1969
    result = self.lm.QueryLocks(["name", "mode", "owner"])
1970
    self.assertEqual(objects.QueryResponse.FromDict(result).data, last_status)
1971

  
1972
    self.assertEqual(len(set(self.lm._locks.values())), len(locks))
1973
    self.assertEqual(len(self.lm._locks), len(locks))
1974

  
1975
    # Check lock deletion
1976
    for idx in range(len(locks)):
1977
      del locks[0]
1978
      assert gc.isenabled()
1979
      gc.collect()
1980
      self.assertEqual(len(self.lm._locks), len(locks))
1981
      result = self.lm.QueryLocks(["name", "mode", "owner"])
1982
      self.assertEqual(objects.QueryResponse.FromDict(result).data,
1983
                       last_status[idx + 1:])
1984

  
1985
    # All locks should have been deleted
1986
    assert not locks
1987
    self.assertFalse(self.lm._locks)
1988

  
1989
    result = self.lm.QueryLocks(["name", "mode", "owner"])
1990
    self.assertEqual(objects.QueryResponse.FromDict(result).data, [])
1991

  
1917 1992

  
1918 1993
if __name__ == '__main__':
1919 1994
  testutils.GanetiTestProgram()

Also available in: Unified diff