@Test
    void testStatefulConnections() throws Exception {
        configureServer(bootstrap -> bootstrap
                .register("*", new SimpleService()));
        final HttpHost target = startServer();

        final int workerCount = 5;
        final int requestCount = 5;

        final UserTokenHandler userTokenHandler = (route, context) -> {
            final String id = (String) context.getAttribute("user");
            return id;
        };

        configureClient(builder -> builder
                .setUserTokenHandler(userTokenHandler));
        final TestClient client = client();

        final PoolingHttpClientConnectionManager connectionManager = client.getConnectionManager();
        connectionManager.setMaxTotal(workerCount);
        connectionManager.setDefaultMaxPerRoute(workerCount);

        final HttpClientContext[] contexts = new HttpClientContext[workerCount];
        final HttpWorker[] workers = new HttpWorker[workerCount];
        for (int i = 0; i < contexts.length; i++) {
            final HttpClientContext context = HttpClientContext.create();
            contexts[i] = context;
            workers[i] = new HttpWorker(
                    "user" + i,
                    context, requestCount, target, client);
        }

        for (final HttpWorker worker : workers) {
            worker.start();
        }
        for (final HttpWorker worker : workers) {
            worker.join(LONG_TIMEOUT.toMilliseconds());
        }
        for (final HttpWorker worker : workers) {
            final Exception ex = worker.getException();
            if (ex != null) {
                throw ex;
            }
            Assertions.assertEquals(requestCount, worker.getCount());
        }

        for (final HttpContext context : contexts) {
            final String state0 = (String) context.getAttribute("r0");
            Assertions.assertNotNull(state0);
            for (int r = 1; r < requestCount; r++) {
                Assertions.assertEquals(state0, context.getAttribute("r" + r));
            }
        }

    }
Test cases for state-ful connections.
httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestStatefulConnManagement.java