From aaf78083285ec5bbd3c0aaefb29022582ce905e5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 15 Jul 2024 09:05:44 +0530 Subject: [PATCH] Add explicit check for holes in test --- kitty/disk-cache.c | 18 ++++++++++++++++++ kitty_tests/graphics.py | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/kitty/disk-cache.c b/kitty/disk-cache.c index 9f6993252..cc53e1af3 100644 --- a/kitty/disk-cache.c +++ b/kitty/disk-cache.c @@ -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 */ }; diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 2101b0af1..6f956fe58 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -254,11 +254,15 @@ class TestGraphics(BaseTest): 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()