Mercurial > hg
annotate tests/test-rust-revlog.py @ 51251:0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
This will be useful for callers, such as `scmutil` who reuse a
`NodeTree` instance as a cache. They would otherwise get hard
errors if any mutation of the index occurred since instantiation.
This is something the C index does not provide.
author | Georges Racinet <georges.racinet@octobus.net> |
---|---|
date | Mon, 30 Oct 2023 22:36:30 +0100 |
parents | 2966b88d4531 |
children | 96e05f1a99bd |
rev | line source |
---|---|
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
1 import struct |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
2 import unittest |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
3 |
51249
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
4 from mercurial.node import hex |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
5 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
6 try: |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
7 from mercurial import rustext |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
8 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
9 rustext.__name__ # trigger immediate actual import |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
10 except ImportError: |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
11 rustext = None |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
12 else: |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
13 from mercurial.rustext import revlog |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
14 |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
15 # this would fail already without appropriate ancestor.__package__ |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
16 from mercurial.rustext.ancestor import LazyAncestors |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
17 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
18 from mercurial.testing import revlog as revlogtesting |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
19 |
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
20 header = struct.unpack(">I", revlogtesting.data_non_inlined[:4])[0] |
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
21 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
22 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
23 @unittest.skipIf( |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44012
diff
changeset
|
24 rustext is None, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44012
diff
changeset
|
25 "rustext module revlog relies on is not available", |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
26 ) |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
27 class RustRevlogIndexTest(revlogtesting.RevlogBasedTestBase): |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
28 def test_heads(self): |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
29 idx = self.parseindex() |
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
30 rustidx = revlog.MixedIndex(idx, revlogtesting.data_non_inlined, header) |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
31 self.assertEqual(rustidx.headrevs(), idx.headrevs()) |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
32 |
44012
443dc1655923
rust-index: expose a method to retrieve the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
44011
diff
changeset
|
33 def test_get_cindex(self): |
443dc1655923
rust-index: expose a method to retrieve the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
44011
diff
changeset
|
34 # drop me once we no longer need the method for shortest node |
443dc1655923
rust-index: expose a method to retrieve the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
44011
diff
changeset
|
35 idx = self.parseindex() |
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
36 rustidx = revlog.MixedIndex(idx, revlogtesting.data_non_inlined, header) |
44012
443dc1655923
rust-index: expose a method to retrieve the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
44011
diff
changeset
|
37 cidx = rustidx.get_cindex() |
443dc1655923
rust-index: expose a method to retrieve the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
44011
diff
changeset
|
38 self.assertTrue(idx is cidx) |
443dc1655923
rust-index: expose a method to retrieve the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
44011
diff
changeset
|
39 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
40 def test_len(self): |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
41 idx = self.parseindex() |
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
42 rustidx = revlog.MixedIndex(idx, revlogtesting.data_non_inlined, header) |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
43 self.assertEqual(len(rustidx), len(idx)) |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
44 |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
45 def test_ancestors(self): |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
46 idx = self.parseindex() |
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
47 rustidx = revlog.MixedIndex(idx, revlogtesting.data_non_inlined, header) |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
48 lazy = LazyAncestors(rustidx, [3], 0, True) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
49 # we have two more references to the index: |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
50 # - in its inner iterator for __contains__ and __bool__ |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
51 # - in the LazyAncestors instance itself (to spawn new iterators) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
52 self.assertTrue(2 in lazy) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
53 self.assertTrue(bool(lazy)) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
54 self.assertEqual(list(lazy), [3, 2, 1, 0]) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
55 # a second time to validate that we spawn new iterators |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
56 self.assertEqual(list(lazy), [3, 2, 1, 0]) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
57 |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
58 # let's check bool for an empty one |
51239
7eea2e4109ae
rust-index: using the `hg::index::Index` in ancestors iterator and lazy set
Georges Racinet <georges.racinet@octobus.net>
parents:
51191
diff
changeset
|
59 self.assertFalse(LazyAncestors(rustidx, [0], 0, False)) |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
60 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
61 |
51249
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
62 @unittest.skipIf( |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
63 rustext is None, |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
64 "rustext module revlog relies on is not available", |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
65 ) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
66 class RustRevlogNodeTreeClassTest(revlogtesting.RustRevlogBasedTestBase): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
67 def test_standalone_nodetree(self): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
68 idx = self.parserustindex() |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
69 nt = revlog.NodeTree(idx) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
70 for i in range(4): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
71 nt.insert(i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
72 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
73 bin_nodes = [entry[7] for entry in idx] |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
74 hex_nodes = [hex(n) for n in bin_nodes] |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
75 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
76 for i, node in enumerate(hex_nodes): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
77 self.assertEqual(nt.prefix_rev_lookup(node), i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
78 self.assertEqual(nt.prefix_rev_lookup(node[:5]), i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
79 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
80 # all 4 revisions in idx (standard data set) have different |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
81 # first nybbles in their Node IDs, |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
82 # hence `nt.shortest()` should return 1 for them, except when |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
83 # the leading nybble is 0 (ambiguity with NULL_NODE) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
84 for i, (bin_node, hex_node) in enumerate(zip(bin_nodes, hex_nodes)): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
85 shortest = nt.shortest(bin_node) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
86 expected = 2 if hex_node[0] == ord('0') else 1 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
87 self.assertEqual(shortest, expected) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
88 self.assertEqual(nt.prefix_rev_lookup(hex_node[:shortest]), i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
89 |
51251
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
90 # test invalidation (generation poisoning) detection |
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
91 del idx[3] |
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
92 self.assertTrue(nt.is_invalidated()) |
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
93 |
51249
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
94 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
95 if __name__ == '__main__': |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
96 import silenttestrunner |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
97 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
98 silenttestrunner.main(__name__) |