public LocalDateTime currentTime0() { ThreadUtil.sleep(MathUtil.randomLong(1, 2)); return LocalDateTime.now(); }
/** * Creates new daemon thread factory. */ public static ThreadFactory daemonThreadFactory(final String name) { return daemonThreadFactory(name, Thread.NORM_PRIORITY); } /**
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ThreadUtil.sleep(5000); write(resp, "OK"); }
@Test void testDaemonThreadFactory() { ThreadFactory threadFactory = ThreadUtil.daemonThreadFactory("jodd-thread"); final StringBuilder sb = new StringBuilder(); final Runnable runnable = () -> {sb.append("runnable instance for jodd junit test");}; Thread t1 = threadFactory.newThread(runnable); Thread t2 = threadFactory.newThread(runnable); Thread t3 = threadFactory.newThread(runnable); // asserts // Thread #1 assertNotNull(t1); assertEquals(Thread.NORM_PRIORITY, t1.getPriority()); assertEquals("jodd-thread-1", t1.getName()); assertEquals(true, t1.isDaemon()); // Thread #2 assertNotNull(t2); assertEquals(Thread.NORM_PRIORITY, t2.getPriority()); assertEquals("jodd-thread-2", t2.getName()); assertEquals(true, t2.isDaemon()); // Thread #3 assertNotNull(t3); assertEquals(Thread.NORM_PRIORITY, t3.getPriority()); assertEquals("jodd-thread-3", t3.getName()); assertEquals(true, t3.isDaemon()); // check that no thread has been runned assertEquals(0, sb.length()); }
public LocalDateTime currentTime() { ThreadUtil.sleep(MathUtil.randomLong(1, 2)); return LocalDateTime.now(); }
/** * Creates new daemon thread factory. */ public static ThreadFactory daemonThreadFactory(final String name) { return daemonThreadFactory(name, Thread.NORM_PRIORITY); } /**
@Test void testWithinAsyncFaster() throws ExecutionException, InterruptedException { final CompletableFuture<String> asyncCode = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(1000); return "done"; }); String value = Futures.within(asyncCode, Duration.ofSeconds(5)) .exceptionally(throwable -> null) .get(); if (value == null) { fail("error"); } }
@Test void testWithinBreaksAsyncExecution() throws ExecutionException, InterruptedException { final CompletableFuture<String> asyncCode = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(3000); return "done"; }); String value = Futures.within(asyncCode, Duration.ofSeconds(1)) .exceptionally(throwable -> null) .get(); if (value != null) { fail("Test waited so we failed!"); } }
@Test void testFailAfterOnManyTasks() throws ExecutionException, InterruptedException { MutableInteger execCount = new MutableInteger(); MutableInteger interruptCount = new MutableInteger(); final CompletableFuture<String> asyncCode1 = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(1000); execCount.value++; return "done"; }); final CompletableFuture<String> asyncCode2 = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(2000); execCount.value++; return "done"; }); final CompletableFuture<String> asyncCode3 = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(3000); execCount.value++; return "done"; }); CompletableFuture<String> f1 = Futures.within(asyncCode1, Duration.ofMillis(1500)).exceptionally(throwable -> { interruptCount.value++; return null; }); CompletableFuture<String> f2 = Futures.within(asyncCode2, Duration.ofMillis(1500)).exceptionally(throwable -> { interruptCount.value++; return null; }); CompletableFuture<String> f3 = Futures.within(asyncCode3, Duration.ofMillis(1500)).exceptionally(throwable -> { interruptCount.value++; return null; }); CompletableFuture.allOf(f1, f2, f3).get(); assertEquals(1, execCount.value); assertEquals(2, interruptCount.value); }
@Test void testDirWatcher() throws IOException { DirWatcher dirWatcher = new DirWatcher(dataRoot, "*.md"); final StringBuilder sb = new StringBuilder(); dirWatcher.register(event -> sb.append(event.type().name() + ":" + event.target().getName() + "\n")); dirWatcher.start(100); File destFile = new File(dataRoot, "jodd.md"); FileUtil.writeString(destFile, "#Jodd"); ThreadUtil.sleep(600); FileUtil.writeString(destFile, "#Jodd2"); ThreadUtil.sleep(600); FileUtil.delete(destFile); ThreadUtil.sleep(600); dirWatcher.stop(); assertEquals( DirWatcherEvent.Type.CREATED + ":jodd.md\n" + DirWatcherEvent.Type.MODIFIED + ":jodd.md\n" + DirWatcherEvent.Type.DELETED + ":jodd.md\n", sb.toString()); }
@Test void testDirWatcherWithFile() throws IOException { DirWatcher dirWatcher = new DirWatcher(dataRoot) .monitor("*.md") .useWatchFile("watch.txt"); final StringBuilder sb = new StringBuilder(); dirWatcher.register( event -> sb.append(event.type().name() + ":" + event.target().getName() + "\n")); dirWatcher.start(100); File watchFile = new File(dataRoot, "watch.txt"); File destFile = new File(dataRoot, "jodd.md"); FileUtil.writeString(destFile, "#Jodd"); FileUtil.touch(watchFile); ThreadUtil.sleep(600); FileUtil.writeString(destFile, "#Jodd2"); ThreadUtil.sleep(600); FileUtil.delete(destFile); FileUtil.touch(watchFile); ThreadUtil.sleep(600); dirWatcher.stop(); assertEquals( DirWatcherEvent.Type.CREATED + ":jodd.md\n" + //DirWatcher.Event.MODIFIED + ":jodd.md\n" + DirWatcherEvent.Type.DELETED + ":jodd.md\n", sb.toString()); }
@Test void testBlankStart() throws IOException { DirWatcher dirWatcher = new DirWatcher(dataRoot) .monitor("*.txt") .startBlank(true); final StringBuilder sb = new StringBuilder(); dirWatcher.register(event -> { sb.append(event.type().name() + ":" + event.target().getName() + "\n"); }); dirWatcher.start(100); ThreadUtil.sleep(600); dirWatcher.stop(); assertEquals( DirWatcherEvent.Type.CREATED + ":watch.txt\n", sb.toString()); }
@Override public void run() { JtxTransaction tx = manager.requestTransaction(new JtxTransactionMode(JtxPropagationBehavior.PROPAGATION_REQUIRED, JtxIsolationLevel.ISOLATION_DEFAULT, true, 1)); count[0]++; assertEquals(count[0], manager.totalTransactions()); assertEquals(2, manager.totalTransactions()); assertEquals(1, manager.totalThreadTransactions()); ThreadUtil.sleep(1000); tx.commit(); count[0]--; } }.start();
@Override public void run() { JtxTransaction tx = manager.requestTransaction(new JtxTransactionMode(JtxPropagationBehavior.PROPAGATION_REQUIRED, JtxIsolationLevel.ISOLATION_DEFAULT, true, 1)); count[0]++; assertEquals(count[0], manager.totalTransactions()); assertEquals(1, manager.totalTransactions()); assertEquals(1, manager.totalThreadTransactions()); ThreadUtil.sleep(1000); tx.commit(); count[0]--; } }.start();
ThreadUtil.sleep(500); new Thread() { @Override
@Test void testCacheTime() { Cache<String, String> cache = new FIFOCache<>(3); cache.put("1", "1"); cache.put("2", "2"); cache.put("3", "3", 50); ThreadUtil.sleep(100); cache.put("4", "4"); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); assertNull(cache.get("3")); assertNotNull(cache.get("4")); }
@Test void testCacheTime2() { Cache<String, String> cache = new FIFOCache<>(3, 50); cache.put("1", "1"); cache.put("2", "2"); assertEquals(2, cache.size()); assertEquals(50, cache.timeout()); ThreadUtil.sleep(100); assertEquals(2, cache.prune()); assertEquals(0, cache.size()); assertTrue(cache.isEmpty()); }
@Test void testCacheTime() { Cache<String, String> cache = new LRUCache<>(3); cache.put("3", "3"); cache.put("2", "2"); assertNotNull(cache.get("2")); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertTrue(cache.isFull()); ThreadUtil.sleep(100); assertNull(cache.get("1")); // expired assertFalse(cache.isFull()); }
@Test void testTime() { JtxTransactionManager manager = new JtxTransactionManager(); manager.registerResourceManager(new DbJtxResourceManager(cp)); JtxTransaction tx1 = manager.requestTransaction(new JtxTransactionMode(JtxPropagationBehavior.PROPAGATION_REQUIRED, JtxIsolationLevel.ISOLATION_DEFAULT, true, 1)); DbSession session1 = tx1.requestResource(DbSession.class); assertNotNull(session1); executeCount(session1, "select count(*) from GIRL"); ThreadUtil.sleep(2000); try { DbSession session2 = tx1.requestResource(DbSession.class); assertNotNull(session2); assertSame(session1, session2); executeCount(session1, "select count(*) from GIRL"); fail("error"); } catch (JtxException ignore) { } tx1.rollback(); }
@Test void testCacheTime() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertNotNull(cache.get("1")); // boost usage cache.put("2", "2"); cache.get("2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); ThreadUtil.sleep(100); assertNull(cache.get("1")); // expired assertFalse(cache.isFull()); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertNotNull(cache.get("1")); ThreadUtil.sleep(100); assertTrue(cache.isFull()); cache.put("4", "4"); assertNotNull(cache.get("3")); assertNotNull(cache.get("2")); assertNotNull(cache.get("4")); assertNull(cache.get("1")); }