@@ -58,14 +58,31 @@ def _create(conn):
5858 return self .get_success (self .db_pool .runWithConnection (_create ))
5959
6060 def _insert_rows (self , instance_name : str , number : int ):
61+ """Insert N rows as the given instance, inserting with stream IDs pulled
62+ from the postgres sequence.
63+ """
64+
6165 def _insert (txn ):
6266 for _ in range (number ):
6367 txn .execute (
6468 "INSERT INTO foobar VALUES (nextval('foobar_seq'), ?)" ,
6569 (instance_name ,),
6670 )
6771
68- self .get_success (self .db_pool .runInteraction ("test_single_instance" , _insert ))
72+ self .get_success (self .db_pool .runInteraction ("_insert_rows" , _insert ))
73+
74+ def _insert_row_with_id (self , instance_name : str , stream_id : int ):
75+ """Insert one row as the given instance with given stream_id, updating
76+ the postgres sequence position to match.
77+ """
78+
79+ def _insert (txn ):
80+ txn .execute (
81+ "INSERT INTO foobar VALUES (?, ?)" , (stream_id , instance_name ,),
82+ )
83+ txn .execute ("SELECT setval('foobar_seq', ?)" , (stream_id ,))
84+
85+ self .get_success (self .db_pool .runInteraction ("_insert_row_with_id" , _insert ))
6986
7087 def test_empty (self ):
7188 """Test an ID generator against an empty database gives sensible
@@ -188,11 +205,17 @@ def test_get_persisted_upto_position(self):
188205 positions.
189206 """
190207
191- self ._insert_rows ("first" , 3 )
192- self ._insert_rows ("second" , 5 )
208+ # The following tests are a bit cheeky in that we notify about new
209+ # positions via `advance` without *actually* advancing the postgres
210+ # sequence.
211+
212+ self ._insert_row_with_id ("first" , 3 )
213+ self ._insert_row_with_id ("second" , 5 )
193214
194215 id_gen = self ._create_id_generator ("first" )
195216
217+ self .assertEqual (id_gen .get_positions (), {"first" : 3 , "second" : 5 })
218+
196219 # Min is 3 and there is a gap between 5, so we expect it to be 3.
197220 self .assertEqual (id_gen .get_persisted_upto_position (), 3 )
198221
@@ -218,3 +241,26 @@ def test_get_persisted_upto_position(self):
218241 id_gen .advance ("first" , 11 )
219242 id_gen .advance ("second" , 15 )
220243 self .assertEqual (id_gen .get_persisted_upto_position (), 11 )
244+
245+ def test_get_persisted_upto_position_get_next (self ):
246+ """Test that `get_persisted_upto_position` correctly tracks updates to
247+ positions when `get_next` is called.
248+ """
249+
250+ self ._insert_row_with_id ("first" , 3 )
251+ self ._insert_row_with_id ("second" , 5 )
252+
253+ id_gen = self ._create_id_generator ("first" )
254+
255+ self .assertEqual (id_gen .get_positions (), {"first" : 3 , "second" : 5 })
256+
257+ self .assertEqual (id_gen .get_persisted_upto_position (), 3 )
258+ with self .get_success (id_gen .get_next ()) as stream_id :
259+ self .assertEqual (stream_id , 6 )
260+ self .assertEqual (id_gen .get_persisted_upto_position (), 3 )
261+
262+ self .assertEqual (id_gen .get_persisted_upto_position (), 6 )
263+
264+ # We assume that so long as `get_next` does correctly advance the
265+ # `persisted_upto_position` in this case, then it will be correct in the
266+ # other cases that are tested above (since they'll hit the same code).
0 commit comments