Skip to content

Commit bcf38a0

Browse files
rvansasebersole
authored andcommitted
HHH-9800 Numerous hibernate-infinispan tests continue to fail transiently
The original cause was JGRP-1931, but this replaces fixed delays with eventual asserts (with 10 seconds timeouts).
1 parent 8e2d1a1 commit bcf38a0

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import java.util.Properties;
1010
import java.util.Set;
11+
import java.util.concurrent.Callable;
12+
import java.util.concurrent.TimeUnit;
1113

1214
import org.hibernate.boot.registry.StandardServiceRegistry;
1315
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@@ -19,9 +21,9 @@
1921
import org.infinispan.AdvancedCache;
2022
import org.infinispan.transaction.tm.BatchModeTransactionManager;
2123
import org.jboss.logging.Logger;
22-
import org.junit.Ignore;
2324
import org.junit.Test;
2425

26+
import static org.hibernate.test.cache.infinispan.util.CacheTestUtil.assertEqualsEventually;
2527
import static org.junit.Assert.assertEquals;
2628
import static org.junit.Assert.assertNull;
2729

@@ -59,7 +61,6 @@ protected void removeFromRegion(Region region, Object key) {
5961
}
6062

6163
@Test
62-
@Ignore // currently ignored because of HHH-9800
6364
public void testEvict() throws Exception {
6465
evictOrRemoveTest();
6566
}
@@ -81,7 +82,7 @@ private void evictOrRemoveTest() throws Exception {
8182
// Sleep a bit to avoid concurrent FLUSH problem
8283
avoidConcurrentFlush();
8384

84-
GeneralDataRegion localRegion = (GeneralDataRegion) createRegion(
85+
final GeneralDataRegion localRegion = (GeneralDataRegion) createRegion(
8586
regionFactory,
8687
getStandardRegionName( REGION_PREFIX ),
8788
properties,
@@ -93,7 +94,7 @@ private void evictOrRemoveTest() throws Exception {
9394
getCacheTestSupport()
9495
);
9596

96-
GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(
97+
final GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(
9798
regionFactory,
9899
getStandardRegionName( REGION_PREFIX ),
99100
properties,
@@ -103,22 +104,29 @@ private void evictOrRemoveTest() throws Exception {
103104
assertNull( "remote is clean", remoteRegion.get( KEY ) );
104105

105106
regionPut( localRegion );
106-
sleep( 250 );
107-
assertEquals( VALUE1, localRegion.get( KEY ) );
108107

109-
// allow async propagation
110-
sleep( 250 );
108+
Callable<Object> getFromLocalRegion = new Callable<Object>() {
109+
@Override
110+
public Object call() throws Exception {
111+
return localRegion.get(KEY);
112+
}
113+
};
114+
Callable<Object> getFromRemoteRegion = new Callable<Object>() {
115+
@Override
116+
public Object call() throws Exception {
117+
return remoteRegion.get(KEY);
118+
}
119+
};
120+
121+
assertEqualsEventually(VALUE1, getFromLocalRegion, 10, TimeUnit.SECONDS);
111122
Object expected = invalidation ? null : VALUE1;
112-
assertEquals( expected, remoteRegion.get( KEY ) );
123+
assertEqualsEventually(expected, getFromRemoteRegion, 10, TimeUnit.SECONDS);
113124

114-
regionEvict( localRegion );
125+
regionEvict(localRegion);
115126

116-
// allow async propagation
117-
sleep( 250 );
118-
assertEquals( null, localRegion.get( KEY ) );
119-
assertEquals( null, remoteRegion.get( KEY ) );
120-
}
121-
finally {
127+
assertEqualsEventually(null, getFromLocalRegion, 10, TimeUnit.SECONDS);
128+
assertEqualsEventually(null, getFromRemoteRegion, 10, TimeUnit.SECONDS);
129+
} finally {
122130
StandardServiceRegistryBuilder.destroy( registry1 );
123131
StandardServiceRegistryBuilder.destroy( registry2 );
124132
}

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/util/CacheTestUtil.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.util.HashMap;
1010
import java.util.Map;
1111
import java.util.Properties;
12+
import java.util.concurrent.Callable;
13+
import java.util.concurrent.TimeUnit;
14+
import java.util.concurrent.TimeoutException;
1215

1316
import org.hibernate.boot.internal.SessionFactoryBuilderImpl;
1417
import org.hibernate.boot.internal.SessionFactoryOptionsImpl;
@@ -18,8 +21,8 @@
1821
import org.hibernate.cfg.AvailableSettings;
1922
import org.hibernate.engine.config.spi.ConfigurationService;
2023
import org.hibernate.engine.config.spi.StandardConverters;
24+
import org.hibernate.internal.util.compare.EqualsHelper;
2125
import org.hibernate.service.ServiceRegistry;
22-
2326
import org.hibernate.test.cache.infinispan.functional.SingleNodeTestCase;
2427

2528
/**
@@ -134,10 +137,42 @@ public static Properties toProperties(Map map) {
134137
return properties;
135138
}
136139

140+
/**
141+
* Executes {@link #assertEqualsEventually(Object, Callable, long, TimeUnit)} without time limit.
142+
* @param expected
143+
* @param callable
144+
* @param <T>
145+
*/
146+
public static <T> void assertEqualsEventually(T expected, Callable<T> callable) throws Exception {
147+
assertEqualsEventually(expected, callable, -1, TimeUnit.SECONDS);
148+
}
149+
150+
/**
151+
* Periodically calls callable and compares returned value with expected value. If the value matches to expected,
152+
* the method returns. If callable throws an exception, this is propagated. If the returned value does not match to
153+
* expected before timeout, {@link TimeoutException} is thrown.
154+
* @param expected
155+
* @param callable
156+
* @param timeout If non-positive, there is no limit.
157+
* @param timeUnit
158+
* @param <T>
159+
*/
160+
public static <T> void assertEqualsEventually(T expected, Callable<T> callable, long timeout, TimeUnit timeUnit) throws Exception {
161+
long now, deadline = timeout <= 0 ? Long.MAX_VALUE : System.currentTimeMillis() + timeUnit.toMillis(timeout);
162+
for (;;) {
163+
T value = callable.call();
164+
if (EqualsHelper.equals(value, expected)) return;
165+
now = System.currentTimeMillis();
166+
if (now < deadline) {
167+
Thread.sleep(Math.min(100, deadline - now));
168+
} else break;
169+
}
170+
throw new TimeoutException();
171+
}
172+
137173
/**
138174
* Prevent instantiation.
139175
*/
140176
private CacheTestUtil() {
141177
}
142-
143178
}

0 commit comments

Comments
 (0)