Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit b99695f

Browse files
committed
Add test for the batcher time interval
1 parent d7b09b0 commit b99695f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

google/cloud/bigtable/batcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ def __init__(
145145
self._executor = concurrent.futures.ThreadPoolExecutor()
146146
self._is_open = True
147147
atexit.register(self.close)
148-
threading.Timer(flush_interval, self.flush).start()
148+
self._timer = threading.Timer(flush_interval, self.flush)
149+
self._timer.start()
149150

150151
@property
151152
def flush_count(self):

tests/unit/test_batcher.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515

1616
import mock
17+
import time
18+
1719
import pytest
1820

1921
from google.cloud.bigtable.row import DirectRow
@@ -180,12 +182,29 @@ def test_mutations_batcher_mutate_after_batcher_closed_raise_error():
180182

181183
assert table.mutation_calls == 0
182184
with pytest.raises(BatcherIsClosedError):
183-
mutation_batcher.close()
184185
row = DirectRow(row_key=b"row_key")
185186
row.set_cell("cf1", b"c1", 1)
186187
mutation_batcher.mutate(row)
187188

188189

190+
@mock.patch("google.cloud.bigtable.batcher.MutationsBatcher.flush")
191+
def test_mutations_batcher_flush_interval(mocked_flush):
192+
table = _Table(TABLE_NAME)
193+
flush_interval = 0.5
194+
mutation_batcher = MutationsBatcher(table=table, flush_interval=flush_interval)
195+
196+
assert mutation_batcher._timer.interval == flush_interval
197+
mocked_flush.assert_not_called()
198+
199+
time.sleep(0.4)
200+
mocked_flush.assert_not_called()
201+
202+
time.sleep(0.1)
203+
mocked_flush.assert_called_once_with()
204+
205+
mutation_batcher.close()
206+
207+
189208
class _Instance(object):
190209
def __init__(self, client=None):
191210
self._client = client

0 commit comments

Comments
 (0)