### Question:
MetricTrackingExecutorFactory { public ExecutorService newCachedThreadPool( final String name, final int maxThreads, final int maxQueueSize, final ThreadFactory threadFactory) { final ThreadPoolExecutor executor = new ThreadPoolExecutor( maxThreads, maxThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueSize), threadFactory); executor.allowCoreThreadTimeOut(true); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_queue_size", "Current size of the executor task queue", () -> executor.getQueue().size()); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_pool_size", "Current number of threads in the executor thread pool", executor::getPoolSize); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_active_count", "Current number of threads executing tasks for this executor", executor::getActiveCount); return executor; } MetricTrackingExecutorFactory(final MetricsSystem metricsSystem); ExecutorService newCachedThreadPool(
      final String name,
      final int maxThreads,
      final int maxQueueSize,
      final ThreadFactory threadFactory);  }### Answer:
@Test void shouldQueueTasksOnceAllThreadsAreFull() throws Exception { final Task task1 = new Task(); final Task task2 = new Task(); final Task task3 = new Task(); final ExecutorService executorService = newCachedThreadPool(2, 5); executorService.execute(task1); executorService.execute(task2); executorService.execute(task3); task1.assertStarted(); task2.assertStarted(); assertThat(task3.isStarted()).isFalse(); assertThat(metricSystem.getGauge(TekuMetricCategory.EXECUTOR, "foo_queue_size").getValue()) .isEqualTo(1); assertThat( metricSystem .getGauge(TekuMetricCategory.EXECUTOR, "foo_thread_active_count") .getValue()) .isEqualTo(2); assertThat( metricSystem.getGauge(TekuMetricCategory.EXECUTOR, "foo_thread_pool_size").getValue()) .isEqualTo(2); task1.allowCompletion(); task3.assertStarted(); assertThat(metricSystem.getGauge(TekuMetricCategory.EXECUTOR, "foo_queue_size").getValue()) .isEqualTo(0); task2.allowCompletion(); task3.allowCompletion(); }
@Test void shouldRejectTasksOnceQueueIsFull() { final Task task1 = new Task(); final Task task2 = new Task(); final Task task3 = new Task(); final Task task4 = new Task(); final ExecutorService executorService = newCachedThreadPool(1, 2); executorService.execute(task1); executorService.execute(task2); executorService.execute(task3); assertThatThrownBy(() -> executorService.execute(task4)) .isInstanceOf(RejectedExecutionException.class); }