code: def test_database(test_dir):
      test_trial = get_test_trial()
      testlogger.debug(test_dir)
      db_port = sherpa.core._port_finder(27000, 28000)
      with sherpa.database._Database(test_dir, port=db_port) as db:
          time.sleep(2)
          testlogger.debug("Enqueuing...")
          db.enqueue_trial(test_trial)
  
          testlogger.debug("Starting Client...")
          client = sherpa.Client(
              port=db_port,
              connectTimeoutMS=100,
              serverSelectionTimeoutMS=1000,
          )
  
          testlogger.debug("Getting Trial...")
          os.environ["SHERPA_TRIAL_ID"] = "1"
          t = client.get_trial()
          assert t.id == 1
          assert t.parameters == {"a": 1, "b": 2}
  
          testlogger.debug("Sending Metrics...")
          client.send_metrics(
              trial=t,
              iteration=1,
              objective=0.1,
              context={"other_metric": 0.2},
          )
  
          new_results = db.get_new_results()
          testlogger.debug(new_results)
          assert new_results == [
              {
                  "context": {"other_metric": 0.2},
                  "iteration": 1,
                  "objective": 0.1,
                  "parameters": {"a": 1, "b": 2},
                  "trial_id": 1,
              }
          ]
  
          # test that Sherpa raises correct error if MongoDB exits
          db2 = sherpa.database._Database(test_dir, port=db_port)
          with pytest.raises(OSError):
              db2.start()
  
file_path: sherpa/tests/test_database.py
imports: import os
  import pytest
  import sys
  import sherpa
  import sherpa.core
  import sherpa.schedulers
  import sherpa.database
  import logging
  import time
  import warnings
  from testing_utils import *
label_desc: Use image node_id 1 for calling a global function with example usage: test_database(test_dir) without return types