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 testFetchResultWithToken() {
        ResultFetcher fetcher =
                buildResultFetcher(Collections.singletonList(data.iterator()), data.size());
        Long nextToken = 0L;
        List<RowData> actual = new ArrayList<>();
        ResultSet resultSetBefore = null;
        while (nextToken != null) {
            if (resultSetBefore != null) {
                assertEquals(resultSetBefore, fetcher.fetchResults(nextToken - 1, data.size()));
            }

            ResultSet resultSet = fetcher.fetchResults(nextToken, data.size());
            ResultSet resultSetWithSameToken = fetcher.fetchResults(nextToken, data.size());

            assertEquals(resultSet, resultSetWithSameToken);
            if (resultSet.getResultType() == ResultSet.ResultType.EOS) {
                break;
            }
            resultSetBefore = resultSet;

            actual.addAll(checkNotNull(resultSet.getData()));
            nextToken = resultSet.getNextToken();
        }

        assertEquals(data, actual);
    }