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

Commit 73171eb

Browse files
committed
run unit tests in emulator mode by default
1 parent 3b5b033 commit 73171eb

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

.github/workflows/unittest.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ jobs:
2323
- name: Run unit tests
2424
env:
2525
COVERAGE_FILE: .coverage-${{ matrix.python }}
26-
# use emulator mode to disable automatic credential detection
27-
BIGTABLE_EMULATOR_HOST: "none"
2826
run: |
2927
nox -s unit-${{ matrix.python }}
3028
- name: Upload coverage results

tests/unit/data/_async/test_client.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ def _get_target_class(self):
5151

5252
return BigtableDataClientAsync
5353

54-
def _make_one(self, *args, **kwargs):
55-
return self._get_target_class()(*args, **kwargs)
54+
def _make_one(self, *args, use_emulator=True, **kwargs):
55+
import os
56+
env_mask = {}
57+
# by default, use emulator mode to avoid auth issues in CI
58+
# emulator mode must be disabled by tests that check channel pooling/refresh background tasks
59+
if use_emulator:
60+
env_mask["BIGTABLE_EMULATOR_HOST"] = "localhost"
61+
with mock.patch.dict(os.environ, env_mask):
62+
return self._get_target_class()(*args, **kwargs)
5663

5764
@pytest.mark.asyncio
5865
async def test_ctor(self):
@@ -63,8 +70,9 @@ async def test_ctor(self):
6370
project="project-id",
6471
pool_size=expected_pool_size,
6572
credentials=expected_credentials,
73+
use_emulator=False,
6674
)
67-
await asyncio.sleep(0.1)
75+
await asyncio.sleep(0)
6876
assert client.project == expected_project
6977
assert len(client.transport._grpc_channel._pool) == expected_pool_size
7078
assert not client._active_instances
@@ -98,6 +106,7 @@ async def test_ctor_super_inits(self):
98106
pool_size=pool_size,
99107
credentials=credentials,
100108
client_options=options_parsed,
109+
use_emulator=False,
101110
)
102111
except AttributeError:
103112
pass
@@ -135,7 +144,7 @@ async def test_ctor_dict_options(self):
135144
with mock.patch.object(
136145
self._get_target_class(), "_start_background_channel_refresh"
137146
) as start_background_refresh:
138-
client = self._make_one(client_options=client_options)
147+
client = self._make_one(client_options=client_options, use_emulator=False)
139148
start_background_refresh.assert_called_once()
140149
await client.close()
141150

@@ -235,14 +244,15 @@ async def test_channel_pool_replace(self):
235244
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
236245
def test__start_background_channel_refresh_sync(self):
237246
# should raise RuntimeError if called in a sync context
238-
client = self._make_one(project="project-id")
247+
client = self._make_one(project="project-id", use_emulator=False)
239248
with pytest.raises(RuntimeError):
240249
client._start_background_channel_refresh()
241250

242251
@pytest.mark.asyncio
243252
async def test__start_background_channel_refresh_tasks_exist(self):
244253
# if tasks exist, should do nothing
245-
client = self._make_one(project="project-id")
254+
client = self._make_one(project="project-id", use_emulator=False)
255+
assert len(client._channel_refresh_tasks) > 0
246256
with mock.patch.object(asyncio, "create_task") as create_task:
247257
client._start_background_channel_refresh()
248258
create_task.assert_not_called()
@@ -252,7 +262,7 @@ async def test__start_background_channel_refresh_tasks_exist(self):
252262
@pytest.mark.parametrize("pool_size", [1, 3, 7])
253263
async def test__start_background_channel_refresh(self, pool_size):
254264
# should create background tasks for each channel
255-
client = self._make_one(project="project-id", pool_size=pool_size)
265+
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
256266
ping_and_warm = AsyncMock()
257267
client._ping_and_warm_instances = ping_and_warm
258268
client._start_background_channel_refresh()
@@ -272,7 +282,7 @@ async def test__start_background_channel_refresh(self, pool_size):
272282
async def test__start_background_channel_refresh_tasks_names(self):
273283
# if tasks exist, should do nothing
274284
pool_size = 3
275-
client = self._make_one(project="project-id", pool_size=pool_size)
285+
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
276286
for i in range(pool_size):
277287
name = client._channel_refresh_tasks[i].get_name()
278288
assert str(i) in name
@@ -537,7 +547,7 @@ async def test__manage_channel_refresh(self, num_cycles):
537547
grpc_helpers_async, "create_channel"
538548
) as create_channel:
539549
create_channel.return_value = new_channel
540-
client = self._make_one(project="project-id")
550+
client = self._make_one(project="project-id", use_emulator=False)
541551
create_channel.reset_mock()
542552
try:
543553
await client._manage_channel(
@@ -919,9 +929,9 @@ async def test_multiple_pool_sizes(self):
919929
# should be able to create multiple clients with different pool sizes without issue
920930
pool_sizes = [1, 2, 4, 8, 16, 32, 64, 128, 256]
921931
for pool_size in pool_sizes:
922-
client = self._make_one(project="project-id", pool_size=pool_size)
932+
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
923933
assert len(client._channel_refresh_tasks) == pool_size
924-
client_duplicate = self._make_one(project="project-id", pool_size=pool_size)
934+
client_duplicate = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
925935
assert len(client_duplicate._channel_refresh_tasks) == pool_size
926936
assert str(pool_size) in str(client.transport)
927937
await client.close()
@@ -934,7 +944,7 @@ async def test_close(self):
934944
)
935945

936946
pool_size = 7
937-
client = self._make_one(project="project-id", pool_size=pool_size)
947+
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
938948
assert len(client._channel_refresh_tasks) == pool_size
939949
tasks_list = list(client._channel_refresh_tasks)
940950
for task in client._channel_refresh_tasks:

tests/unit/data/test_read_rows_acceptance.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ def cancel(self):
119119
return mock_stream(chunk_list)
120120

121121
try:
122-
client = BigtableDataClientAsync()
122+
with mock.patch.dict(os.environ, {"BIGTABLE_EMULATOR_HOST": "localhost"}):
123+
# use emulator mode to avoid auth issues in CI
124+
client = BigtableDataClientAsync()
123125
table = client.get_table("instance", "table")
124126
results = []
125127
with mock.patch.object(table.client._gapic_client, "read_rows") as read_rows:

0 commit comments

Comments
 (0)