Spaces:
Running
Running
Download python/test_binding.py from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 4.16 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/python/test_binding.py
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/python/test_binding.py
-
curl -L -o test_binding.py https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/python/test_binding.py
4.16 kB
| import gc | |
| import random | |
| import unittest | |
| from concurrent.futures import ThreadPoolExecutor | |
| from threading import Barrier | |
| from sincekit import BatchPlan, Index | |
| class IndexTest(unittest.TestCase): | |
| def test_shared_rows_and_ownership(self): | |
| index = Index(8) | |
| self.assertEqual((index.capacity, index.revision), (8, 0)) | |
| changes = [(2, False), (0, False), (2, True), (3, True), (0, True), (2, False)] | |
| self.assertEqual(index.write_many(iter(changes)), 6) | |
| plan = index.since_many(cursor for cursor in (0, 6, 5, 1, 4)) | |
| self.assertEqual(plan, BatchPlan(6, ((2, 6, False), (0, 5, True), (3, 4, True)), | |
| (3, 0, 1, 3, 2))) | |
| index.write_many([(2, True)]) | |
| del index | |
| gc.collect() | |
| self.assertEqual(plan.rows[:plan.prefix_lengths[4]], ((2, 6, False), (0, 5, True))) | |
| def test_empty(self): | |
| index = Index(0) | |
| self.assertEqual(index.write_many([]), 0) | |
| self.assertEqual(index.since_many([]), BatchPlan(0, (), ())) | |
| self.assertEqual(index.since_many([0, 0]), BatchPlan(0, (), (0, 0))) | |
| with self.assertRaises(ValueError): | |
| index.write_many([(0, False)]) | |
| def test_reject_bad_values_without_partial_publication(self): | |
| for bad_capacity in (-1, 2**32, True, 2.5): | |
| with self.assertRaises((TypeError, OverflowError)): | |
| Index(bad_capacity) | |
| index = Index(3) | |
| index.write_many([(1, True)]) | |
| expected = index.since_many([0]) | |
| for bad in ((0,), (0, 1), (True, False), (3, False), (-1, True), | |
| (2**80, False), "ab", object(), [1, False, 3]): | |
| with self.subTest(bad=bad): | |
| with self.assertRaises((TypeError, ValueError, OverflowError)): | |
| index.write_many([(2, True), bad]) | |
| self.assertEqual(index.since_many([0]), expected) | |
| for bad_cursor in (True, 1.0, "0", 2, 2**65, -1): | |
| with self.assertRaises((TypeError, ValueError, OverflowError)): | |
| index.since_many([0, bad_cursor]) | |
| with self.assertRaises(TypeError): | |
| index.since_many(None) | |
| def failing_iterator(): | |
| yield (0, False) | |
| raise LookupError("broken input") | |
| with self.assertRaises(LookupError): | |
| index.write_many(failing_iterator()) | |
| self.assertEqual(index.since_many([0]), expected) | |
| def test_reference(self): | |
| rng = random.Random(754535) | |
| index = Index(253) | |
| head = 0 | |
| latest = {} | |
| for _ in range(70): | |
| changes = [(rng.randrange(253), rng.random() < .35) for _ in range(rng.randrange(100))] | |
| for slot, deleted in changes: | |
| head += 1 | |
| latest[slot] = (slot, head, deleted) | |
| self.assertEqual(index.write_many(changes), head) | |
| cursors = [rng.randrange(head + 1) for _ in range(90)] | |
| plan = index.since_many(cursors) | |
| oracle = sorted(latest.values(), key=lambda row: row[1], reverse=True) | |
| self.assertEqual(plan.rows, tuple(row for row in oracle if row[1] > min(cursors))) | |
| self.assertEqual(plan.prefix_lengths, | |
| tuple(sum(row[1] > cursor for row in oracle) for cursor in cursors)) | |
| def test_concurrent_publication_has_no_interleaved_batches(self): | |
| writers, batch = 8, 25 | |
| index = Index(writers * batch) | |
| barrier = Barrier(writers) | |
| def write(group): | |
| changes = [(group * batch + slot, False) for slot in range(batch)] | |
| barrier.wait() | |
| return group, index.write_many(changes) | |
| with ThreadPoolExecutor(writers) as pool: | |
| ends = dict(pool.map(write, range(writers))) | |
| self.assertEqual(sorted(ends.values()), list(range(batch, writers * batch + 1, batch))) | |
| plan = index.since_many([0]) | |
| for slot, rev, deleted in plan.rows: | |
| group, within = divmod(slot, batch) | |
| self.assertEqual(rev, ends[group] - batch + 1 + within) | |
| self.assertFalse(deleted) | |
| if __name__ == "__main__": | |
| unittest.main() | |