Skip to content

Commit

Permalink
run unit tests in emulator mode by default
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Jan 19, 2024
1 parent 3b5b033 commit 73171eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ jobs:
- name: Run unit tests
env:
COVERAGE_FILE: .coverage-${{ matrix.python }}
# use emulator mode to disable automatic credential detection
BIGTABLE_EMULATOR_HOST: "none"
run: |
nox -s unit-${{ matrix.python }}
- name: Upload coverage results
Expand Down
34 changes: 22 additions & 12 deletions tests/unit/data/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ def _get_target_class(self):

return BigtableDataClientAsync

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

@pytest.mark.asyncio
async def test_ctor(self):
Expand All @@ -63,8 +70,9 @@ async def test_ctor(self):
project="project-id",
pool_size=expected_pool_size,
credentials=expected_credentials,
use_emulator=False,
)
await asyncio.sleep(0.1)
await asyncio.sleep(0)
assert client.project == expected_project
assert len(client.transport._grpc_channel._pool) == expected_pool_size
assert not client._active_instances
Expand Down Expand Up @@ -98,6 +106,7 @@ async def test_ctor_super_inits(self):
pool_size=pool_size,
credentials=credentials,
client_options=options_parsed,
use_emulator=False,
)
except AttributeError:
pass
Expand Down Expand Up @@ -135,7 +144,7 @@ async def test_ctor_dict_options(self):
with mock.patch.object(
self._get_target_class(), "_start_background_channel_refresh"
) as start_background_refresh:
client = self._make_one(client_options=client_options)
client = self._make_one(client_options=client_options, use_emulator=False)
start_background_refresh.assert_called_once()
await client.close()

Expand Down Expand Up @@ -235,14 +244,15 @@ async def test_channel_pool_replace(self):
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test__start_background_channel_refresh_sync(self):
# should raise RuntimeError if called in a sync context
client = self._make_one(project="project-id")
client = self._make_one(project="project-id", use_emulator=False)
with pytest.raises(RuntimeError):
client._start_background_channel_refresh()

@pytest.mark.asyncio
async def test__start_background_channel_refresh_tasks_exist(self):
# if tasks exist, should do nothing
client = self._make_one(project="project-id")
client = self._make_one(project="project-id", use_emulator=False)
assert len(client._channel_refresh_tasks) > 0
with mock.patch.object(asyncio, "create_task") as create_task:
client._start_background_channel_refresh()
create_task.assert_not_called()
Expand All @@ -252,7 +262,7 @@ async def test__start_background_channel_refresh_tasks_exist(self):
@pytest.mark.parametrize("pool_size", [1, 3, 7])
async def test__start_background_channel_refresh(self, pool_size):
# should create background tasks for each channel
client = self._make_one(project="project-id", pool_size=pool_size)
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
ping_and_warm = AsyncMock()
client._ping_and_warm_instances = ping_and_warm
client._start_background_channel_refresh()
Expand All @@ -272,7 +282,7 @@ async def test__start_background_channel_refresh(self, pool_size):
async def test__start_background_channel_refresh_tasks_names(self):
# if tasks exist, should do nothing
pool_size = 3
client = self._make_one(project="project-id", pool_size=pool_size)
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
for i in range(pool_size):
name = client._channel_refresh_tasks[i].get_name()
assert str(i) in name
Expand Down Expand Up @@ -537,7 +547,7 @@ async def test__manage_channel_refresh(self, num_cycles):
grpc_helpers_async, "create_channel"
) as create_channel:
create_channel.return_value = new_channel
client = self._make_one(project="project-id")
client = self._make_one(project="project-id", use_emulator=False)
create_channel.reset_mock()
try:
await client._manage_channel(
Expand Down Expand Up @@ -919,9 +929,9 @@ async def test_multiple_pool_sizes(self):
# should be able to create multiple clients with different pool sizes without issue
pool_sizes = [1, 2, 4, 8, 16, 32, 64, 128, 256]
for pool_size in pool_sizes:
client = self._make_one(project="project-id", pool_size=pool_size)
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
assert len(client._channel_refresh_tasks) == pool_size
client_duplicate = self._make_one(project="project-id", pool_size=pool_size)
client_duplicate = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
assert len(client_duplicate._channel_refresh_tasks) == pool_size
assert str(pool_size) in str(client.transport)
await client.close()
Expand All @@ -934,7 +944,7 @@ async def test_close(self):
)

pool_size = 7
client = self._make_one(project="project-id", pool_size=pool_size)
client = self._make_one(project="project-id", pool_size=pool_size, use_emulator=False)
assert len(client._channel_refresh_tasks) == pool_size
tasks_list = list(client._channel_refresh_tasks)
for task in client._channel_refresh_tasks:
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/data/test_read_rows_acceptance.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ def cancel(self):
return mock_stream(chunk_list)

try:
client = BigtableDataClientAsync()
with mock.patch.dict(os.environ, {"BIGTABLE_EMULATOR_HOST": "localhost"}):
# use emulator mode to avoid auth issues in CI
client = BigtableDataClientAsync()
table = client.get_table("instance", "table")
results = []
with mock.patch.object(table.client._gapic_client, "read_rows") as read_rows:
Expand Down

0 comments on commit 73171eb

Please sign in to comment.