Skip to content

Commit

Permalink
Adding test for handling error on query (#18082)
Browse files Browse the repository at this point in the history
HDQuery was not handling thrown Errors correctly.
The Error is not catched and therefore not delegated back to the
caller. A caller in that case hangs indefinitely.
fixes #18052
  • Loading branch information
sancar committed Jan 25, 2021
1 parent 20167a2 commit e2ee942
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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 com.hazelcast.map.impl.query;

import com.hazelcast.query.Predicate;

import java.io.Serializable;
import java.util.Map;

public class ErrorThrowingPredicate implements Predicate, Serializable {
@Override
public boolean apply(Map.Entry mapEntry) {
throw new NoClassDefFoundError();
}
}
Expand Up @@ -40,8 +40,10 @@
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import java.io.IOException;
Expand All @@ -60,6 +62,9 @@
@Category({QuickTest.class, ParallelTest.class})
public class QueryAdvancedTest extends HazelcastTestSupport {

@Rule
public ExpectedException expected = ExpectedException.none();

@Test
public void testQueryOperationAreNotSentToLiteMembers() {
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
Expand Down Expand Up @@ -141,8 +146,8 @@ public void entryEvicted(EntryEvent event) {
// check the query result before eviction
Collection values = map.values(new SqlPredicate("active"));
assertEquals(String.format("Expected %s results but got %s. Number of evicted entries: %s.",
activeEmployees, values.size(), allEmployees - latch.getCount()),
activeEmployees, values.size());
activeEmployees, values.size(), allEmployees - latch.getCount()),
activeEmployees, values.size());

// wait until eviction is completed
assertOpenEventually(latch);
Expand Down Expand Up @@ -522,8 +527,8 @@ public Portable create(int classId) {
}
});
config.getMapConfig(mapName)
.addMapIndexConfig(new MapIndexConfig("notExist", false))
.addMapIndexConfig(new MapIndexConfig("n", false));
.addMapIndexConfig(new MapIndexConfig("notExist", false))
.addMapIndexConfig(new MapIndexConfig("n", false));

HazelcastInstance hazelcastInstance = createHazelcastInstance(config);

Expand All @@ -535,4 +540,19 @@ public Portable create(int classId) {
Collection values = map.values(new SqlPredicate("n = name_2 OR notExist = name_0"));
assertEquals(1, values.size());
}

@Test
public void testClassNotFoundErrorDelegatedToCallerOnQuery() {
Config config = getConfig();
HazelcastInstance hazelcastInstance = createHazelcastInstance(config);

IMap<Integer, Integer> map = hazelcastInstance.getMap("map");

map.put(1, 1);
//A remote predicate can throw Error in case of Usercodeployment and missing sub classes
//See the issue for actual problem https://github.com/hazelcast/hazelcast/issues/18052
//We are throwing error to see if the error is delegated to the caller
expected.expect(NoClassDefFoundError.class);
map.values(new ErrorThrowingPredicate());
}
}

0 comments on commit e2ee942

Please sign in to comment.