ChrisSamo632 commented on code in PR #7537: URL: https://github.com/apache/nifi/pull/7537#discussion_r1305782117
########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/java/org/apache/nifi/processors/elasticsearch/SearchElasticsearchTest.java: ########## @@ -0,0 +1,217 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License") you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.elasticsearch; + +import org.apache.nifi.components.state.Scope; +import org.apache.nifi.processors.elasticsearch.api.PaginationType; +import org.apache.nifi.processors.elasticsearch.api.ResultOutputStrategy; +import org.apache.nifi.state.MockStateManager; +import org.apache.nifi.util.TestRunner; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.Instant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +public class SearchElasticsearchTest extends AbstractPaginatedJsonQueryElasticsearchTest { + @BeforeAll + public static void setUpBeforeClass() throws Exception { + AbstractPaginatedJsonQueryElasticsearchTest.setUpBeforeClass(); + } + + public AbstractPaginatedJsonQueryElasticsearch getProcessor() { + return new SearchElasticsearch(); + } + + public boolean isStateUsed() { + return true; + } + + public boolean isInput() { + return false; + } + + @Test + public void testScrollError() { + final TestRunner runner = createRunner(false); + final TestElasticsearchClientService service = AbstractJsonQueryElasticsearchTest.getService(runner); + service.setMaxPages(2); + service.setThrowErrorInSearch(false); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, PaginationType.SCROLL.getValue()); + runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, matchAllWithSortByMsgWithSizeQuery); + + // initialize search + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.clearTransferState(); + + // scroll (error) + service.setThrowErrorInSearch(true); + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 0, 0, 0); + assertTrue(runner.getLogger().getErrorMessages().stream().anyMatch(logMessage -> + logMessage.getMsg().contains("Could not query documents") && logMessage.getThrowable().getMessage().contains("Simulated IOException - scroll"))); + } + + @Test + public void testScrollExpiration() throws Exception { + testPaginationExpiration(PaginationType.SCROLL); + } + + @Test + public void testPitExpiration() throws Exception { + testPaginationExpiration(PaginationType.POINT_IN_TIME); + } + + @Test + public void testSearchAfterExpiration() throws Exception { + testPaginationExpiration(PaginationType.SEARCH_AFTER); + } + + private void testPaginationExpiration(final PaginationType paginationType) throws Exception{ + // test flowfile per page + final TestRunner runner = createRunner(false); + final TestElasticsearchClientService service = AbstractJsonQueryElasticsearchTest.getService(runner); + service.setMaxPages(2); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue()); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_KEEP_ALIVE, "1 sec"); + runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, matchAllWithSortByMsgWithSizeQuery); + + // first page + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1"); + assertState(runner.getStateManager(), paginationType, 10, 1); + + // wait for expiration + final Instant expiration = Instant.ofEpochMilli(Long.parseLong(runner.getStateManager().getState(Scope.LOCAL).get(SearchElasticsearch.STATE_PAGE_EXPIRATION_TIMESTAMP))); + while (expiration.isAfter(Instant.now())) { + Thread.sleep(10); + } + + if ("true".equalsIgnoreCase(System.getenv("CI"))) { + // allow extra time if running in CI Pipeline to prevent intermittent timing-issue failures + Thread.sleep(1000); + } + + service.resetPageCount(); + runner.clearTransferState(); + + // first page again (new query after first query expired) + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1"); + assertState(runner.getStateManager(), paginationType, 10, 1); + runner.clearTransferState(); + + // second page + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "2"); + assertState(runner.getStateManager(), paginationType, 20, 2); + runner.clearTransferState(); + } + + @Override + public void testPagination(final PaginationType paginationType) throws Exception { + final TestRunner runner = createRunner(false); + final TestElasticsearchClientService service = AbstractJsonQueryElasticsearchTest.getService(runner); + service.setMaxPages(2); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue()); + runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, matchAllWithSortByMsgWithSizeQuery); + + // Tests flowfile per page, hits splitting and hits combined + for(ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) { + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, resultOutputStrategy.getValue()); + + for(int iteration = 1; iteration < 4; iteration++) { + runOnce(runner); + validatePagination(runner, resultOutputStrategy, paginationType, iteration); + runner.clearTransferState(); + if(ResultOutputStrategy.PER_QUERY.equals(resultOutputStrategy)) { + break; + } + } + reset(runner); + } + } + + private static void validatePagination(final TestRunner runner, final ResultOutputStrategy resultOutputStrategy, final PaginationType paginationType, int iteration) throws IOException { Review Comment: The code I pasted in my suggestion (https://github.com/apache/nifi/pull/7537#discussion_r1304789339) worked for me when running locally last night, which is why I suggested it (don't copy `testPagination` from either of the classes as they are, neither will work fully for the other class - I've combined the two to get them working using the same logic (including some things that had been missed from one or the other test class previously) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
