Skip to content

Commit

Permalink
Stop Datastore emulator properly (#1911)
Browse files Browse the repository at this point in the history
Killing the main datastore process leave its child processes running and
orphaned.
Should've tested this properly before 🙃

Used the `/shutdown` endpoint that [apparently
exists](https://github.com/googleapis/java-datastore/blob/1849e0be44e94fbfa48211484de4af602ca6b614/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java#L323)
(and works!).
I have no idea if there's proper documentation for this...
  • Loading branch information
michaelkedar authored Jan 10, 2024
1 parent 6587285 commit 666a43e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docker/alias/alias_computation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,4 @@ def test_to_vulnerability(self):
context.set_cache_policy(False)
unittest.main()
finally:
ds_emulator.kill()
tests.stop_emulator()
2 changes: 1 addition & 1 deletion docker/importer/importer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,4 @@ def test_import_override(self, unused_mock_time: mock.MagicMock,
context.set_cache_policy(False)
unittest.main()
finally:
ds_emulator.kill()
tests.stop_emulator()
2 changes: 1 addition & 1 deletion docker/worker/worker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,4 +1425,4 @@ def test_last_affected_git(self):
context.set_cache_policy(False)
unittest.main()
finally:
ds_emulator.kill()
tests.stop_emulator()
2 changes: 1 addition & 1 deletion osv/impact_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
cls._ds_emulator.kill()
tests.stop_emulator()
cls._ndb_context.__exit__(None, None, None) # pylint: disable=unnecessary-dunder-call

def test_update_single_page(self):
Expand Down
13 changes: 13 additions & 0 deletions osv/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ def reset_emulator():
resp.raise_for_status()


def stop_emulator():
"""Stops emulator."""
try:
port = os.environ.get('DATASTORE_EMULATOR_PORT', _DATASTORE_EMULATOR_PORT)
resp = requests.post(
'http://localhost:{}/shutdown'.format(port), timeout=_EMULATOR_TIMEOUT)
resp.raise_for_status()
except Exception as e:
# Something went wrong, manually kill all datastore processes instead.
os.system('pkill -f datastore')
raise e


def mock_datetime(test):
"""Mocks datetime."""
for to_mock in ('osv.models.utcnow', 'osv.utcnow'):
Expand Down

0 comments on commit 666a43e

Please sign in to comment.