public synchronized ResultSet fetchResults(FetchOrientation orientation, int maxFetchSize) {
        long token;
        switch (orientation) {
            case FETCH_NEXT:
                token = currentToken;
                break;
            case FETCH_PRIOR:
                token = currentToken - 1;
                break;
            default:
                throw new UnsupportedOperationException(
                        String.format("Unknown fetch orientation: %s.", orientation));
        }

        if (orientation == FetchOrientation.FETCH_NEXT && bufferedResults.isEmpty()) {
            // make sure data is available in the buffer
            resultStore.waitUntilHasData();
        }

        return fetchResults(token, maxFetchSize);
    }
@Test
    void testFetchResultsInWithLimitedBufferSizeInOrientation() {
        int bufferSize = data.size() / 2;
        ResultFetcher fetcher =
                buildResultFetcher(Collections.singletonList(data.iterator()), bufferSize);

        int fetchSize = data.size();
        runFetchMultipleTimes(
                bufferSize,
                fetchSize,
                token -> fetcher.fetchResults(FetchOrientation.FETCH_NEXT, fetchSize));
    }