1
# test tools for the pyxpcom bindings
2
from xpcom import _xpcom
5
# export a "getmemusage()" function that returns a useful "bytes used" count
6
# for the current process. Growth in this when doing the same thing over and
17
# XXX - win32pdh is slow, particularly finding our current process.
18
# A better way would be good.
20
# Our win32pdh specific functions - they can be at the top-level on all
21
# platforms, but will only actually be called if the modules are available.
23
pid_me = win32api.GetCurrentProcessId()
26
items, instances = win32pdh.EnumObjectItems(None,None,object, -1)
27
for instance in instances:
28
# We use 2 counters - "ID Process" and "Working Set"
29
counter = "ID Process"
30
format = win32pdh.PDH_FMT_LONG
32
hq = win32pdh.OpenQuery()
33
path = win32pdh.MakeCounterPath( (None,object,instance, None, -1,"ID Process") )
34
hc1 = win32pdh.AddCounter(hq, path)
35
path = win32pdh.MakeCounterPath( (None,object,instance, None, -1,"Working Set") )
36
hc2 = win32pdh.AddCounter(hq, path)
37
win32pdh.CollectQueryData(hq)
38
type, pid = win32pdh.GetFormattedCounterValue(hc1, format)
40
win32pdh.RemoveCounter(hc1) # not needed any more
42
# Not mine - close the query and try again
43
win32pdh.RemoveCounter(hc1)
44
win32pdh.RemoveCounter(hc2)
45
win32pdh.CloseQuery(hq)
47
raise RuntimeError, "Can't find myself!?"
49
def CloseCounter(hq, hc):
50
win32pdh.RemoveCounter(hc)
51
win32pdh.CloseQuery(hq)
53
def GetCounterValue(hq, hc):
54
win32pdh.CollectQueryData(hq)
55
format = win32pdh.PDH_FMT_LONG
56
type, val = win32pdh.GetFormattedCounterValue(hc, format)
60
# The pdh function that does the work
61
def pdh_getmemusage():
63
if g_pdh_data is None:
64
hq, hc = FindMyCounter()
67
return GetCounterValue(hq, hc)
71
getmemusage = pdh_getmemusage
76
# Test runner utilities, including some support for builtin leak tests.
77
class TestLoader(unittest.TestLoader):
78
def loadTestsFromTestCase(self, testCaseClass):
79
"""Return a suite of all tests cases contained in testCaseClass"""
81
for name in self.getTestCaseNames(testCaseClass):
82
real_test = testCaseClass(name)
83
leak_test = self._getTestWrapper(real_test)
84
leak_tests.append(leak_test)
85
return self.suiteClass(leak_tests)
86
def _getTestWrapper(self, test):
87
# later! see pywin32's win32/test/util.py
89
def loadTestsFromModule(self, mod):
90
if hasattr(mod, "suite"):
93
ret = unittest.TestLoader.loadTestsFromModule(self, mod)
94
assert ret.countTestCases() > 0, "No tests in %r" % (mod,)
96
def loadTestsFromName(self, name, module=None):
97
test = unittest.TestLoader.loadTestsFromName(self, name, module)
98
if isinstance(test, unittest.TestSuite):
99
pass # hmmm? print "Don't wrap suites yet!", test._tests
100
elif isinstance(test, unittest.TestCase):
101
test = self._getTestWrapper(test)
103
print "XXX - what is", test
106
# A base class our tests should derive from (well, one day it will be)
107
TestCase = unittest.TestCase
109
def suite_from_functions(*funcs):
110
suite = unittest.TestSuite()
112
suite.addTest(unittest.FunctionTestCase(func))
115
def testmain(*args, **kw):
117
if not new_kw.has_key('testLoader'):
118
new_kw['testLoader'] = TestLoader()
120
unittest.main(*args, **new_kw)
122
_xpcom.NS_ShutdownXPCOM()
123
ni = _xpcom._GetInterfaceCount()
124
ng = _xpcom._GetGatewayCount()
126
print "********* WARNING - Leaving with %d/%d objects alive" % (ni,ng)