code: def test_worker_trials(storage, monkeypatch):
      """Test number of trials executed is limited based on worker-trials"""
      monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
  
      assert (
          len(
              list(
                  storage.fetch_experiments(
                      {"name": "demo_random_search"}
                  )
              )
          )
          == 0
      )
  
      orion.core.cli.main(
          [
              "hunt",
              "--config",
              "./orion_config_random.yaml",
              "--worker-trials",
              "0",
              "./black_box.py",
              "-x~uniform(-50, 50)",
          ]
      )
  
      exp = list(
          storage.fetch_experiments({"name": "demo_random_search"})
      )
      assert len(exp) == 1
      exp = exp[0]
      assert "_id" in exp
      exp_id = exp["_id"]
  
      def n_completed():
          return len(
              list(
                  storage._fetch_trials(
                      {"experiment": exp_id, "status": "completed"}
                  )
              )
          )
  
      assert n_completed() == 0
  
      # Test only executes 2 trials
      orion.core.cli.main(
          [
              "hunt",
              "--name",
              "demo_random_search",
              "--worker-trials",
              "2",
          ]
      )
  
      assert n_completed() == 2
  
      # Test only executes 3 more trials
      orion.core.cli.main(
          [
              "hunt",
              "--name",
              "demo_random_search",
              "--worker-trials",
              "3",
          ]
      )
  
      assert n_completed() == 5
  
      # Test that max-trials has precedence over worker-trials
      orion.core.cli.main(
          [
              "hunt",
              "--name",
              "demo_random_search",
              "--worker-trials",
              "5",
              "--max-trials",
              "6",
          ]
      )
  
      assert n_completed() == 6
  
imports: import os
  import shutil
  import subprocess
  import sys
  import tempfile
  from collections import defaultdict
  from contextlib import contextmanager
  import numpy
  import pytest
  import yaml
  import orion.core.cli
  import orion.core.io.experiment_builder
  from orion.core.cli.hunt import workon
  from orion.testing import OrionState
label_desc: Use image node_id 18 for calling a global function with example usage: test_worker_trials(storage, monkeypatch) and returns: len