下面列出了org.junit.jupiter.api.Assertions#assertTimeout() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Disabled
@Test
public void performanceTestNuget() {
Assertions.assertTimeout(Duration.ofSeconds(120), () -> {
final String dependencyGraphFile = FunctionalTestFiles.asString("/nuget/dwCheckApi_inspection.json");
final NugetInspectorParser packager = new NugetInspectorParser(gson, externalIdFactory);
final NugetParseResult result = packager.createCodeLocation(dependencyGraphFile);
final CodeLocation codeLocation = result.getCodeLocations().get(0);
final BdioPropertyHelper bdioPropertyHelper = new BdioPropertyHelper();
final BdioNodeFactory bdioNodeFactory = new BdioNodeFactory(bdioPropertyHelper);
final DependencyGraphTransformer dependencyGraphTransformer = new DependencyGraphTransformer(bdioPropertyHelper, bdioNodeFactory);
final BdioProject bdioNode = bdioNodeFactory.createProject("test", "1.0.0", BdioId.createFromPieces("bdioId"), externalIdFactory.createMavenExternalId("group", "name", "version"));
final List<BdioComponent> components = dependencyGraphTransformer
.transformDependencyGraph(codeLocation.getDependencyGraph(), bdioNode, codeLocation.getDependencyGraph().getRootDependencies(), new HashMap<ExternalId, BdioNode>());
assertEquals(211, components.size());
});
}
@Test
public void fixedDelay() {
final Delay fixed = Constant.of().delay(Duration.ofMillis(50)).timeout(Duration.ofMillis(5 * 50)).build();
int[] attempt = { 1 };
final Executable fn = () -> {
Duration next = Duration.ZERO;
while ((next = fixed.nextDelay(attempt[0])) != Duration.ZERO) {
attempt[0]++;
TimeUnit.MILLISECONDS.sleep(next.toMillis());
}
};
//
// Small 5ms jitter to ensure we complete within time
//
Assertions.assertTimeout(Duration.ofMillis(6 * 50), fn);
assertThat(6).isEqualTo(attempt[0]);
assertThat(fixed.nextDelay(8) == Duration.ZERO).isEqualTo(true);
assertThat(fixed.nextDelay(10) == Duration.ZERO).isEqualTo(true);
assertThat(fixed.nextDelay(15) == Duration.ZERO).isEqualTo(true);
}
@Test
void runtime() {
final Lifecycle lifecycle = mock(Lifecycle.class);
final ShutdownEnabler se = mock(ShutdownEnabler.class);
final DaemonShutdownHook hook = new DaemonShutdownHook(lifecycle, se);
hook.start();
se.get()
.ifPresent(c -> c.accept(ReturnCode.OK));
Assertions.assertTimeout(Duration.ofSeconds(5), () -> {
while (hook.isAlive()) { // wait until the hook to terminate
Thread.sleep(1);
}
verify(se).waitUntilTriggered();
verify(lifecycle).resumeToShutdown();
});
}
@Test
public void testConcurrentMapAccess() {
final AtomicInteger counter = new AtomicInteger();
final Map<Object, Object> map = autoToStr(counter);
final int k = 1000;
Assertions.assertTimeout(Duration.ofSeconds(60), () -> {
Msc.benchmarkMT(200, "gets", 100000000, () -> {
int rnd = Rnd.rnd(k);
eq(map.get(rnd), rnd + "");
});
});
// it is highly unlikely to be less than K, for a small value of K
eq(counter.get(), k);
}
@Test
public void shouldBeFast() {
// typically 1M reads should take less than a second
Assertions.assertTimeout(Duration.ofSeconds(5), () -> {
for (int i = 0; i < 900; i++) {
// fill-in the cache (with non-existing resources)
Res.from("abc", "x-location");
}
// should be fast
multiThreaded(100, 1000000, () -> {
Res file = Res.from("abc.txt", "");
notNull(file.getBytes());
});
});
}
@Test
public void createCodeLocationDWService() {
Assertions.assertTimeout(Duration.ofMillis(5000L), () -> {
final String dependencyNodeFile = FunctionalTestFiles.asString("/nuget/dwCheckApi_inspection_martin.json");
final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
final NugetInspectorParser packager = new NugetInspectorParser(gson, externalIdFactory);
final NugetParseResult result = packager.createCodeLocation(dependencyNodeFile);
for (final CodeLocation codeLocation : result.getCodeLocations()) {
final BdioPropertyHelper bdioPropertyHelper = new BdioPropertyHelper();
final BdioNodeFactory bdioNodeFactory = new BdioNodeFactory(bdioPropertyHelper);
final DependencyGraphTransformer dependencyNodeTransformer = new DependencyGraphTransformer(bdioPropertyHelper, bdioNodeFactory);
final BdioExternalIdentifier projectId = bdioPropertyHelper.createExternalIdentifier(codeLocation.getExternalId().get());
final BdioProject project = bdioNodeFactory.createProject(result.getProjectName(), result.getProjectVersion(), BdioId.createFromPieces(Forge.NUGET.toString()), projectId);
final Map<ExternalId, BdioNode> components = new HashMap<>();
components.put(codeLocation.getExternalId().get(), project);
final List<BdioComponent> bdioComponents = dependencyNodeTransformer.transformDependencyGraph(codeLocation.getDependencyGraph(), project, codeLocation.getDependencyGraph().getRootDependencies(), components);
assertEquals(bdioComponents.size(), bdioComponents.size());
}
});
}
@Test
void canCancel() {
final RandomSourceFunction function = new RandomSourceFunction(Integer.MAX_VALUE);
function.cancel();
env.addSource(function).addSink(new CollectSink());
Assertions.assertTimeout(Duration.ofSeconds(2), () -> env.execute());
assertThat(CollectSink.values).isEmpty();
}
@Test
public void firingUnusedEventsMustBeFast() {
Assertions.assertTimeout(Duration.ofSeconds(1), () -> {
for (int i = 0; i < 100 * 1000 * 1000; i++) {
Fire.event(Events.LOG_TRACE);
}
});
}
@Test
public void testJobsExecution() {
int total = 100000;
final AtomicInteger counter = new AtomicInteger();
Assertions.assertTimeout(Duration.ofSeconds(30), () -> {
multiThreaded(1000, total, () -> {
Ctxs.open("test-job");
final UserInfo user = new UserInfo(TestRnd.rndStr(50), U.set("role1"));
Ctxs.required().setUser(user);
ensureProperContext(user);
ScheduledFuture<?> future = Jobs.after(100, TimeUnit.MILLISECONDS).run(() -> {
ensureProperContext(user);
counter.incrementAndGet();
});
try {
future.get();
} catch (Exception e) {
e.printStackTrace();
fail("The job throwed an exception!");
}
Ctxs.close();
});
});
eq(counter.get(), total);
}
@Test
public void shouldSupportMultipleWatchCalls() {
String dir = TestIO.createTempDir("watch-service-test");
Assertions.assertTimeout(Duration.ofSeconds(60), () -> {
if (!TestCommons.RAPIDOID_CI) {
for (int i = 0; i < 10; i++) {
exerciseMultiWatch(dir);
}
}
});
}
@Test
public void timeout() {
Assertions.assertTimeout(Duration.ofMillis(10), () -> Thread.sleep(5));
}
@Test
void createNotoSansScaler() throws Exception {
Assertions.assertTimeout(
Duration.of(250, ChronoUnit.MILLIS),
NotoSans::createScaler);
}
@Test
@Disabled
public void shouldFailBecauseTimeout() throws InterruptedException {
Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
}
@Test
@Disabled
public void shouldFailBecauseTimeout() throws InterruptedException {
Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
}