def test_nlmsaslice_cache(self): "NLMSASlice sequence caching & removal" # set up sequences dnaseq = testutil.datafile('dnaseq.fasta') db = SequenceFileDB(dnaseq, autoGC=-1) # use pure WeakValueDict... try: gc.collect() assert len( db._weakValueDict) == 0, '_weakValueDict should be empty' seq1, seq2 = db['seq1'], db['seq2'] assert len(db._weakValueDict)==2, \ '_weakValueDict should have 2 seqs' # build referencing NLMSA mymap = NLMSA('test', 'memory', db, pairwiseMode=True) mymap += seq1 mymap[seq1] += seq2 mymap.build() # check: no cache assert not hasattr(db, '_cache'), 'should be no cache yet' seq1, seq2 = db['seq1'], db['seq2'] # re-retrieve # now retrieve a NLMSASlice, forcing entry of seq into cache ival = seq1[5:10] x = mymap[ival] assert len(db._cache.values()) != 0 n1 = len(db._cache) assert n1 == 1, "should be exactly one cache entry, not %d" % \ (n1, ) # ok, now trash referencing arguments & make sure of cleanup del x gc.collect() assert len(db._cache.values()) == 0 n2 = len(db._cache) assert n2 == 0, '%d objects remain; cache memory leak!' % n2 # FAIL because of __dealloc__ error in cnestedlist.NLMSASlice. # Drop our references, the cache should empty. del mymap, ival, seq1, seq2 gc.collect() # check that db._weakValueDict cache is empty assert len( db._weakValueDict) == 0, '_weakValueDict should be empty' finally: db.close()