Repository: incubator-unomi Updated Branches: refs/heads/master b62a84c5f -> b7357f922
UNOMI-199 : Avoid endless loop Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/b7357f92 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/b7357f92 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/b7357f92 Branch: refs/heads/master Commit: b7357f922dcb1d227b46f433e290abfa32a1de96 Parents: b62a84c Author: tdraier <[email protected]> Authored: Tue Sep 25 14:53:24 2018 +0200 Committer: tdraier <[email protected]> Committed: Tue Sep 25 14:53:24 2018 +0200 ---------------------------------------------------------------------- .../test/java/org/apache/unomi/itests/BaseIT.java | 17 +++++++++++++++++ .../org/apache/unomi/itests/ProfileExportIT.java | 14 ++++---------- .../unomi/itests/ProfileImportActorsIT.java | 6 +----- .../apache/unomi/itests/ProfileImportBasicIT.java | 6 +----- .../unomi/itests/ProfileImportRankingIT.java | 6 +----- .../unomi/itests/ProfileImportSurfersIT.java | 18 +++--------------- 6 files changed, 27 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b7357f92/itests/src/test/java/org/apache/unomi/itests/BaseIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/BaseIT.java b/itests/src/test/java/org/apache/unomi/itests/BaseIT.java index 635c52c..82e1c57 100644 --- a/itests/src/test/java/org/apache/unomi/itests/BaseIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/BaseIT.java @@ -17,6 +17,7 @@ package org.apache.unomi.itests; +import org.junit.Assert; import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.CoreOptions; import org.ops4j.pax.exam.Option; @@ -25,6 +26,9 @@ import org.ops4j.pax.exam.options.MavenArtifactUrlReference; import org.ops4j.pax.exam.options.MavenUrlReference; import java.io.File; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; import static org.ops4j.pax.exam.CoreOptions.maven; import static org.ops4j.pax.exam.CoreOptions.systemProperty; @@ -132,4 +136,17 @@ public abstract class BaseIT { CoreOptions.frameworkStartLevel(100) }; } + + protected <T> T keepTrying(Supplier<T> call, Predicate<T> predicate, int timeout, int retries) throws InterruptedException { + int count = 0; + T value = null; + while (value == null || !predicate.test(value)) { + if (count++ > retries) { + Assert.fail(); + } + Thread.sleep(timeout); + value = call.get(); + } + return value; + } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b7357f92/itests/src/test/java/org/apache/unomi/itests/ProfileExportIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileExportIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileExportIT.java index 13893e9..1fca3bf 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileExportIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileExportIT.java @@ -80,11 +80,7 @@ public class ProfileExportIT extends BaseIT { profile3.setSegments(segments); profileService.save(profile3); - PartialList<Profile> profiles = null; - while (profiles == null || profiles.getTotalSize() != 3) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("segments", "exportItSeg", 0, 10, null); - } + keepTrying(()-> profileService.findProfilesByPropertyValue("segments", "exportItSeg", 0, 10, null), (p)->p.getTotalSize() == 3, 1000, 20); /*** Export Test ***/ String itemId = "export-test"; @@ -109,11 +105,9 @@ public class ProfileExportIT extends BaseIT { exportConfigurationService.save(exportConfiguration, true); - File exportResult = new File("data/tmp/profiles-export.csv"); - while (!exportResult.exists()) { - Thread.sleep(1000); - exportResult = new File("data/tmp/profiles-export.csv"); - } + final File exportResult = new File("data/tmp/profiles-export.csv"); + keepTrying(()-> exportResult, File::exists, 1000, 20); + logger.info("PATH : {}", exportResult.getAbsolutePath()); Assert.assertTrue(exportResult.exists()); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b7357f92/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java index a6bfa20..86b31fc 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportActorsIT.java @@ -101,11 +101,7 @@ public class ProfileImportActorsIT extends BaseIT { importConfigurationService.save(importConfigActors, true); //Wait for data to be processed - PartialList<Profile> profiles = null; - while (profiles == null || profiles.getTotalSize() != 6) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("properties.city", "hollywood", 0, 10, null); - } + keepTrying(()-> profileService.findProfilesByPropertyValue("properties.city", "hollywood", 0, 10, null), (p)->p.getTotalSize() == 6, 1000, 100); List<ImportConfiguration> importConfigurations = importConfigurationService.getAll(); Assert.assertEquals(1, importConfigurations.size()); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b7357f92/itests/src/test/java/org/apache/unomi/itests/ProfileImportBasicIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportBasicIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportBasicIT.java index 4523bee..3503284 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportBasicIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportBasicIT.java @@ -88,11 +88,7 @@ public class ProfileImportBasicIT extends BaseIT { Files.copy(basicFile.toPath(), new File("data/tmp/unomi_oneshot_import_configs/1-basic-test.csv").toPath(), StandardCopyOption.REPLACE_EXISTING); //Wait for the csv to be processed - PartialList<Profile> profiles = null; - while (profiles == null || profiles.getTotalSize() != 3) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("properties.city", "oneShotImportCity", 0, 10, null); - } + PartialList<Profile> profiles = keepTrying(()->profileService.findProfilesByPropertyValue("properties.city", "oneShotImportCity", 0, 10, null), (p)->p.getTotalSize() == 3, 1000, 100); Assert.assertEquals(3, profiles.getList().size()); checkProfiles(1); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b7357f92/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java index 4ae3c42..78924d7 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportRankingIT.java @@ -106,11 +106,7 @@ public class ProfileImportRankingIT extends BaseIT { //Wait for data to be processed - PartialList<Profile> profiles = null; - while (profiles == null || profiles.getTotalSize() != 25) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("properties.city", "rankingCity", 0, 50, null); - } + keepTrying(()->profileService.findProfilesByPropertyValue("properties.city", "rankingCity", 0, 50, null), (p)->p.getTotalSize() == 25, 1000, 100); List<ImportConfiguration> importConfigurations = importConfigurationService.getAll(); Assert.assertEquals(1, importConfigurations.size()); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b7357f92/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java index 4129edc..58b91dd 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ProfileImportSurfersIT.java @@ -105,11 +105,7 @@ public class ProfileImportSurfersIT extends BaseIT { logger.info("ProfileImportSurfersIT setup successfully."); //Wait for data to be processed - PartialList<Profile> profiles = null; - while (profiles == null || profiles.getTotalSize() != 34) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("properties.city", "surfersCity", 0, 50, null); - } + keepTrying(()->profileService.findProfilesByPropertyValue("properties.city", "surfersCity", 0, 50, null), (p)->p.getTotalSize() == 34, 1000, 20); List<ImportConfiguration> importConfigurations = importConfigurationService.getAll(); Assert.assertEquals(1, importConfigurations.size()); @@ -154,11 +150,7 @@ public class ProfileImportSurfersIT extends BaseIT { logger.info("ProfileImportSurfersOverwriteIT setup successfully."); //Wait for data to be processed - profiles = null; - while (profiles == null || profiles.getTotalSize() != 36) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("properties.city", "surfersCity", 0, 50, null); - } + keepTrying(()->profileService.findProfilesByPropertyValue("properties.city", "surfersCity", 0, 50, null), (p)->p.getTotalSize() == 36, 1000, 20); importConfigurations = importConfigurationService.getAll(); Assert.assertEquals(1, importConfigurations.size()); @@ -197,11 +189,7 @@ public class ProfileImportSurfersIT extends BaseIT { logger.info("ProfileImportSurfersDeleteIT setup successfully."); //Wait for data to be processed - profiles = null; - while (profiles == null || profiles.getTotalSize() != 0) { - Thread.sleep(1000); - profiles = profileService.findProfilesByPropertyValue("properties.city", "surfersCity", 0, 50, null); - } + keepTrying(()->profileService.findProfilesByPropertyValue("properties.city", "surfersCity", 0, 50, null), (p)->p.getTotalSize() == 0, 1000, 20); importConfigurations = importConfigurationService.getAll(); Assert.assertEquals(1, importConfigurations.size());
