@Test
public void testQueueDepthStats() throws Throwable {

    setUpHost(false);
    MinimalFactoryTestService factoryService = (MinimalFactoryTestService)
            this.host.startServiceAndWait(new MinimalFactoryTestService(),
                    UUID.randomUUID().toString(), null);
    factoryService.setChildServiceCaps(EnumSet.of(ServiceOption.PERSISTENCE));
    this.host.doFactoryChildServiceStart(null, this.serviceCount, MinimalTestServiceState.class,
            (o) -> {
                MinimalTestServiceState initialState = new MinimalTestServiceState();
                initialState.documentSelfLink = initialState.id = UUID.randomUUID().toString();
                o.setBody(initialState);
            }, factoryService.getUri());
    // Create an executor service which rejects all attempts to create new threads.
    ExecutorService blockingExecutor = Executors.newSingleThreadExecutor((r) -> null);
    ExecutorService queryExecutor = this.indexService.setQueryExecutorService(blockingExecutor);
    queryExecutor.shutdown();
    queryExecutor.awaitTermination(this.host.getTimeoutSeconds(), TimeUnit.SECONDS);
    // Now submit a query and wait for the queue depth stat to be set.
    QueryTask queryTask = QueryTask.Builder.create()
            .setQuery(Query.Builder.create()
                    .addKindFieldClause(MinimalTestServiceState.class)
                    .build())
            .build();
    this.host.createQueryTaskService(queryTask);
    this.host.waitFor("Query queue depth stat was not set", () -> {
        Map<String, ServiceStat> indexStats = this.host.getServiceStats(
                this.host.getDocumentIndexServiceUri());
        ServiceStat queueDepthStat = indexStats.entrySet().stream()
                .filter((entry) -> entry.getKey().startsWith(
                        LuceneDocumentIndexService.STAT_NAME_PREFIX_QUERY_QUEUE_DEPTH))
                .filter((entry) -> entry.getKey().endsWith(
                        ServiceStats.STAT_NAME_SUFFIX_PER_DAY))
                .map(Entry::getValue).findFirst().orElse(null);
        if (queueDepthStat == null) {
            return false;
        }
        assertTrue(queueDepthStat.name.contains(ServiceUriPaths.CORE_AUTHZ_GUEST_USER));
        return queueDepthStat.latestValue == 1;
    });
}