Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: query.map() and query.map_async() hanging with empty result set. #230

Merged
merged 3 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion google/cloud/ndb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,11 @@ def map_async(self, callback, **kwargs):
mapped = future
futures.append(mapped)

mapped_results = yield futures
if futures:
mapped_results = yield futures
else:
mapped_results = []

raise tasklets.Return(mapped_results)

@_query_options
Expand Down
12 changes: 12 additions & 0 deletions tests/system/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,18 @@ def get_other_foo(thing):
assert query.map(get_other_foo) == foos


@pytest.mark.usefixtures("client_context")
def test_map_empty_result_set(dispose_of):
class SomeKind(ndb.Model):
foo = ndb.StringProperty()

def somefunc(x):
raise Exception("Shouldn't be called.")

query = SomeKind.query()
assert query.map(somefunc) == []


@pytest.mark.usefixtures("client_context")
def test_gql(ds_entity):
for i in range(5):
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,25 @@ def callback(result):
query = query_module.Query()
assert query.map(callback) == (1, 2, 3, 4, 5)

@staticmethod
@pytest.mark.usefixtures("in_context")
@mock.patch("google.cloud.ndb._datastore_query")
def test_map_empty_result_set(_datastore_query):
class DummyQueryIterator:
def __init__(self, items):
self.items = list(items)

def has_next_async(self):
return utils.future_result(bool(self.items))

_datastore_query.iterate.return_value = DummyQueryIterator(())

def callback(result): # pragma: NO COVER
raise Exception("Shouldn't get called.")

query = query_module.Query()
assert query.map(callback) == []

@staticmethod
@pytest.mark.usefixtures("in_context")
@mock.patch("google.cloud.ndb._datastore_query")
Expand Down