Add explicit check for holes in test

This commit is contained in:
Kovid Goyal 2024-07-15 09:05:44 +05:30
parent a02fc7194d
commit aaf7808328
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 22 additions and 0 deletions

View file

@ -733,6 +733,23 @@ clear(PyObject *self, PyObject *args UNUSED) {
Py_RETURN_NONE;
}
static PyObject*
holes(PyObject *self_, PyObject *args UNUSED) {
DiskCache *self = (DiskCache*)self_;
mutex(lock);
RAII_PyObject(ans, PyTuple_New(self->holes.count));
if (ans) {
for (size_t i = 0; i < self->holes.count; i++) {
PyObject *t = Py_BuildValue("LL", (long long)self->holes.items[i].pos, (long long)self->holes.items[i].size);
if (!t) return NULL;
PyTuple_SetItem(ans, i, t);
}
}
mutex(unlock);
Py_INCREF(ans);
return ans;
}
static PyObject*
add(PyObject *self, PyObject *args) {
@ -816,6 +833,7 @@ static PyMethodDef methods[] = {
{"wait_for_write", wait_for_write, METH_VARARGS, NULL},
{"size_on_disk", size_on_disk, METH_NOARGS, NULL},
{"clear", clear, METH_NOARGS, NULL},
{"holes", holes, METH_NOARGS, NULL},
{NULL} /* Sentinel */
};

View file

@ -254,11 +254,15 @@ def reset(small_hole_threshold=0):
self.assertRaises(KeyError, dc.get, key_as_bytes(x))
self.assertEqual(sz, dc.size_on_disk())
self.assertEqual(sz, dc.size_on_disk())
holes = {2, 4, 6, 8}
self.assertEqual(holes, {x[1] for x in dc.holes()})
# fill holes largest first to ensure small one doesnt go into large accidentally causing fragmentation
for x in sorted(('xy', 'C'*4, 'B'*6, 'A'*8), key=len, reverse=True):
add(x, x)
self.assertTrue(dc.wait_for_write())
check_data()
holes.discard(len(x))
self.assertEqual(holes, {x[1] for x in dc.holes()})
self.assertEqual(sz, dc.size_on_disk(), f'Disk cache has unexpectedly grown from {sz} to {dc.size_on_disk} with data: {x!r}')
check_data()
dc.clear()