Coverage for drivers/refcounter : 64%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/python # # Copyright (C) Citrix Systems Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; version 2.1 only. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # Persistent reference counter. This refcounter can maintain two separate # refcounts: one binary (which can have a value of 0 or 1) and one normal. The # parameter "binary" specifies which of the two counters to update, while the # return value is zero IFF both counters are zero # # Synchronization must be done at a higher level, by the users of this module #
"""Persistent local-FS file-based reference counter. The operations are get() and put(), and they are atomic."""
"""Get (inc ref count) 'obj' in namespace 'ns' (optional). Returns new ref count""" else:
"""Put (dec ref count) 'obj' in namespace 'ns' (optional). If ref count was zero already, this operation is a no-op. Returns new ref count""" else:
"""Set normal & binary counts explicitly to the specified values. Returns new ref count"""
"""Get the ref count values for 'obj' in namespace 'ns' (optional)"""
"""Lock-protected access""" lock = Lock(obj, ns) lock.acquire() try: return RefCounter.check(obj, ns) finally: lock.release()
"""Reset ref counts for 'obj' in namespace 'ns' (optional) to 0."""
"""Reset ref counts of 'obj' in namespace 'ns' to 0. If obj is not provided, reset all existing objects in 'ns' to 0. If neither obj nor ns are supplied, do this for all namespaces""" else: except OSError: raise RefCounterException("failed to get namespace list")
"""Add 'delta' to the normal refcount and 'binaryDelta' to the binary refcount of 'obj' in namespace 'ns'. Returns new ref count""" raise RefCounterException("Binary delta = %d outside [-1;1]" % \ binaryDelta)
(ns, obj, count, binaryCount, delta, binaryDelta, newCount, newBinaryCount))
"""Get the ref count values for 'obj' in namespace 'ns'"""
"""Set the ref count values for 'obj' in namespace 'ns'""" (ns, obj, count, binaryCount)) else:
"""Get a name that can be used as a file name"""
except OSError as e: if e.errno != errno.EEXIST: raise RefCounterException("failed to makedirs '%s' (%s)" % \ (nsDir, e))
except OSError: raise RefCounterException("failed to remove '%s'" % objFile)
else: raise RefCounterException("failed to remove '%s'" % nsDir)
return else: except OSError: raise RefCounterException("failed to list '%s'" % ns)
except IOError: raise RefCounterException("failed to read file '%s'" % fn)
raise RefCounterException("failed to write '(%d %d)' to '%s': %s" \ % (count, binaryCount, fn, e))
"Unit tests"
# A print("Error: check = %d != 0 in the beginning" % cnt) return -1
print("Error: count = %d != 1 after first get()" % cnt) return -1 print("Error: check = %d != 1 after first get()" % cnt) return -1
print("Error: count = %d != 0 after get-put" % cnt) return -1 print("Error: check = %d != 0 after get-put" % cnt) return -1
print("Error: count = %d != 1 after get-put-get" % cnt) return -1
print("Error: count = %d != 2 after second get()" % cnt) return -1
print("Error: count = %d != 3 after third get()" % cnt) return -1 print("Error: check = %d != 3 after third get()" % cnt) return -1
print("Error: count = %d != 0 after first put()" % cnt) return -1 print("Error: check = %d != 0 after first put()" % cnt) return -1
print("Error: count = %d != 2 after 3get-1put" % cnt) return -1
print("Error: count = %d != 1 after 3get-2put" % cnt) return -1
print("Error: count = %d != 2 after 4get-2put" % cnt) return -1 print("Error: check = %d != 2 after 4get-2put" % cnt) return -1
print("Error: count = %d != 0 after 4get-3put" % cnt) return -1
print("Error: count = %d != 0 after 4get-4put" % cnt) return -1 print("Error: check = %d != 0 after 4get-4put" % cnt) return -1
# B print("Error: count = %d != 0 after new put()" % cnt) return -1
print("Error: count = %d != 1 after put-get" % cnt) return -1
print("Error: count = %d != 0 after put-get-put" % cnt) return -1 print("Error: check = %d != 0 after put-get-put" % cnt) return -1
print("Error: count = %d != 1 after put-get-put-get" % cnt) return -1 print("Error: check = %d != 1 after put-get-put-get" % cnt) return -1
# set print("Error: count = %d != 0 initially" % cnt) return -1 print("Error: count = (%d,%d) != (2,0) after set(2,0)" % (cnt, bcnt)) return -1 print("Error: count = %d != 1 after set(2)-put" % cnt) return -1 print("Error: count = %d != 2 after set(2)-put-get" % cnt) return -1 print("Error: cnt,bcnt = (%d,%d) != (100,0) after set(100,0)" % \ (cnt, bcnt)) return -1 print("Error: count = %d != 101 after get" % cnt) return -1 print("Error: cnt,bcnt = (%d,%d) != (100,1) after set(100,1)" % \ (cnt, bcnt)) return -1 print("Error: check = %d != 0 after reset" % cnt) return -1
# binary print("Error: count = %d != 1 after get(bin)" % cnt) return -1 print("Error: count = %d != 1 after get(bin)*2" % cnt) return -1 print("Error: count = %d != 0 after get(bin)*2-put(bin)" % cnt) return -1 print("Error: count = %d != 0 after get(bin)*2-put(bin)*2" % cnt) return -1 print("Error: set(0,2) was allowed") return -1 print("Error: count = %d != 1 after get(bin)" % cnt) return -1 print("Error: count = %d != 2 after get(bin)-get" % cnt) return -1 print("Error: count = %d != 3 after get(bin)-get-get" % cnt) return -1 print("Error: count = %d != 3 after get(bin)-get*2-get(bin)" % cnt) return -1 print("Error: count = %d != 2 after get(bin)*2-get*2-put" % cnt) return -1 print("Error: cnt = %d != 1 after get(b)*2-get*2-put-put(b)" % cnt) return -1 print("Error: cnt = %d != 0 after get(b)*2-get*2-put*2-put(b)" % cnt) return -1
# names print("Error: count = %d != 1 after get (no ns 1)" % cnt) return -1
print("Error: count = %d != 1 after get (no ns 2)" % cnt) return -1
print("Error: count = %d != 1 after get (no ns 3)" % cnt) return -1
print("Error: count = %d != 1 after get (no ns 4)" % cnt) return -1
print("Error: count = %d != 2 after get (no ns 1)" % cnt) return -1
print("Error: count = %d != 2 after get (no ns 2)" % cnt) return -1
print("Error: count = %d != 2 after get (no ns 3)" % cnt) return -1
print("Error: count = %d != 2 after get (no ns 4)" % cnt) return -1
# resetAll print("Error: count = %d != 1 after resetAll-get" % cnt) return -1
print("Error: count = %d != 1 after C.get" % cnt) return -1
print("Error: count = %d != 1 after second resetAll-get" % cnt) return -1
print("Error: count = %d != 2 after second C.get" % cnt) return -1
print("Error: count = %d != 0 after resetAll-put" % cnt) return -1
print("Error: count = %d != 0 after C.resetAll-put" % cnt) return -1
print("Running unit tests...") try: if RefCounter._runTests() == 0: print("All done, no errors") except RefCounterException as e: print("FAIL: Got exception: %s" % e) raise |