def test_file(self): aset = AddressSet(self.TABLE_LEN) addr = "".join(chr(b) for b in xrange(20)) aset.add(addr) dbfile = tempfile.TemporaryFile() aset.tofile(dbfile) dbfile.seek(0) aset = AddressSet.fromfile(dbfile) self.assertTrue(dbfile.closed) # should be closed by AddressSet in read-only mode self.assertIn(addr, aset) self.assertEqual(len(aset), 1)
def test_pickle_mmap(self): aset = AddressSet(self.TABLE_LEN) addr = "".join(chr(b) for b in xrange(20)) aset.add(addr) dbfile = tempfile.NamedTemporaryFile(delete=False) try: aset.tofile(dbfile) dbfile.seek(0) aset = AddressSet.fromfile(dbfile) # now it's an mmap pickled = pickle.dumps(aset, protocol=pickle.HIGHEST_PROTOCOL) aset.close() # also closes the file aset = pickle.loads(pickled) self.assertIn(addr, aset) self.assertEqual(len(aset), 1) finally: aset.close() dbfile.close() os.remove(dbfile.name)
def test_file_update(self): aset = AddressSet(self.TABLE_LEN) dbfile = tempfile.NamedTemporaryFile(delete=False) try: aset.tofile(dbfile) dbfile.seek(0) aset = AddressSet.fromfile(dbfile, mmap_access=mmap.ACCESS_WRITE) addr = "".join(chr(b) for b in xrange(20)) aset.add(addr) aset.close() self.assertTrue(dbfile.closed) dbfile = open(dbfile.name, "rb") aset = AddressSet.fromfile(dbfile) self.assertIn(addr, aset) self.assertEqual(len(aset), 1) finally: aset.close() dbfile.close() os.remove(dbfile.name)