Java Code Examples for org.rapidoid.u.U#sleep()

The following examples show how to use org.rapidoid.u.U#sleep() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Jobs.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static void executeAndWait(Runnable job) {
	Opt<ThreadPoolExecutor> executor = jobs.executor();

	if (executor.exists()) {
		ContextPreservingJobWrapper jobWrapper = wrap(job);

		try {
			executor.get().execute(jobWrapper);
		} catch (RejectedExecutionException e) {
			Log.warn("Job execution was rejected!", "job", job);
		}

		while (!jobWrapper.isDone()) {
			U.sleep(10);
		}
	}
}
 
Example 2
Source File: FutureImpl.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Override
public T get(long timeoutMs, long sleepingIntervalMs) throws TimeoutException {
	long waitingSince = U.time();

	while (!isDone()) {
		if (U.timedOut(waitingSince, timeoutMs)) {
			throw new TimeoutException();
		}

		U.sleep(sleepingIntervalMs);
	}

	if (getError() != null) {
		throw U.rte("Cannot get the result, there was an error!", error);
	}

	return result;
}
 
Example 3
Source File: MultiWatchTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private void exerciseMultiWatch(String dir) {
	final Set<Integer> seen = Coll.synchronizedSet();

	int total = 50;

	for (int i = 0; i < total; i++) {
		final int seenBy = i;
		Msc.watchForChanges(dir, filename -> seen.add(seenBy)
		);
	}

	giveItTimeToRefresh();

	IO.save(Msc.path(dir, "a.txt"), "ABC-" + U.time());

	while (seen.size() < total) {
		U.sleep(200);
	}

	eq(seen.size(), total);

	Watch.cancelAll();
}
 
Example 4
Source File: HttpLoginTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@TestCtx
private void randomUserLogin() {
	while (!ready) U.sleep(100); // wait

	switch (Rnd.rnd(4)) {
		case 0:
			loginFlow("foo", "bar", U.list());
			break;
		case 1:
			loginFlow("abc", "abc", U.list("guest"));
			break;
		case 2:
			loginFlow("chuck", "chuck", U.list("moderator", "restarter"));
			break;
		case 3:
			loginFlow("niko", "easy", U.list("owner", "administrator", "moderator"));
			break;
		default:
			throw Err.notExpected();
	}
}
 
Example 5
Source File: JPAUtil.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private static EntityManagerFactory createEMF(Properties props, CustomHibernatePersistenceProvider provider) {
	while (true) {
		try {
			EntityManagerFactory emf = provider.createEMF(props);
			U.must(emf != null, "Failed to initialize EntityManagerFactory with Hibernate!");
			return emf;

		} catch (Exception e) {
			if (Msc.rootCause(e) instanceof ConnectException) {
				Log.warn("Couldn't connect, will retry again in 3 seconds...");
				U.sleep(3000); // FIXME improve back-off
			} else {
				throw U.rte("Failed to create EMF!", e);
			}
		}
	}
}
 
Example 6
Source File: TcpClientTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
public void testTCPClientWithDefaultAndCustomConnections() {
	Log.setLogLevel(LogLevel.DEBUG);

	TCPClient client = TCP.client().host("127.0.0.1").port(8888).connections(3).protocol(HI_CLIENT).build().start();
	client.connect("localhost", 9090, HI_CLIENT, 2, false, null);

	// let the clients wait
	U.sleep(3000);

	Server server1 = TCP.server().port(8888).protocol(UPPERCASE_SERVER).build().start();
	Server server2 = TCP.server().port(9090).protocol(UPPERCASE_SERVER).build().start();

	// let the servers serve the clients
	U.sleep(3000);

	eq(client.info().messagesProcessed(), 5);
	eq(server1.info().messagesProcessed(), 3);
	eq(server2.info().messagesProcessed(), 2);

	client.shutdown();
	server1.shutdown();
	server2.shutdown();
}
 
Example 7
Source File: TcpClientTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
public void testTCPClientWithCustomConnections() {
	Log.setLogLevel(LogLevel.DEBUG);

	TCPClient client = TCP.client().build().start();
	client.connect("localhost", 8888, HI_CLIENT, 10, false, null);

	// let the clients wait
	U.sleep(3000);

	Server server = TCP.server().port(8888).protocol(UPPERCASE_SERVER).build().start();

	// let the server serve the clients
	U.sleep(3000);

	eq(client.info().messagesProcessed(), 10);
	eq(server.info().messagesProcessed(), 10);

	client.shutdown();
	server.shutdown();
}
 
Example 8
Source File: TcpClientTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
public void testTCPClientWithDefaultConnections() {
	Log.setLogLevel(LogLevel.DEBUG);

	TCPClient client = TCP.client().host("localhost").port(8888).connections(5).protocol(HI_CLIENT).build().start();

	// let the clients wait
	U.sleep(3000);

	Server server = TCP.server().port(8888).protocol(UPPERCASE_SERVER).build().start();

	// let the server serve the clients
	U.sleep(3000);

	eq(client.info().messagesProcessed(), 5);
	eq(server.info().messagesProcessed(), 5);

	client.shutdown();
	server.shutdown();
}
 
Example 9
Source File: WatchingRefresherThread.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
protected void loop() {
	boolean found = false;
	String filename;

	while ((filename = created.poll()) != null) {
		createdFilenames.add(filename);
		found = true;
	}
	while ((filename = modified.poll()) != null) {
		modifiedFilenames.add(filename);
		found = true;
	}
	while ((filename = deleted.poll()) != null) {
		deletedFilenames.add(filename);
		found = true;
	}

	// when the changes are complete
	if (!found && (!createdFilenames.isEmpty() || !modifiedFilenames.isEmpty() || !deletedFilenames.isEmpty())) {
		reload();
		createdFilenames.clear();
		modifiedFilenames.clear();
		deletedFilenames.clear();
	}

	U.sleep(100);
}
 
Example 10
Source File: ExpirationCrawlerThread.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	long startedAt = U.time();

	while (!Thread.interrupted()) {

		long timeSpent = U.time() - startedAt;
		U.sleep(Math.max(resolution - timeSpent, 0));
		startedAt = U.time();

		crawl();
	}
}
 
Example 11
Source File: IsolatedIntegrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@AfterEach
public void closeContext() {
	Jobs.shutdownNow();
	U.sleep(200);

	if (Admin.setup().isRunning()) {
		if (Admin.setup().port() == On.setup().port()) {
			Admin.setup().reset();
		} else {
			Admin.setup().shutdown();
		}
	}

	RapidoidIntegrationTest.after(this);
}
 
Example 12
Source File: RapidoidWorkerThread.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public RapidoidWorker getWorker() {
	while (worker == null) {
		U.sleep(50);
	}

	return worker;
}
 
Example 13
Source File: HttpRoutesImpl.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public void waitToStabilize() {
	while (!stable) {
		U.sleep(1);
		if (ready()) {
			synchronized (this) {
				if (!stable) {
					stable = true;
					Log.debug("Stabilized HTTP routes");
				}
			}
		}
	}
}
 
Example 14
Source File: JobChainingTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testJobChaining() {
	Ctx ctx = Ctxs.open("root");
	isFalse(ctx.isClosed());

	asyncLoop(1000);

	ctx.close();

	while (!ctx.isClosed()) {
		U.sleep(1);
	}

	Log.info("Done");
}
 
Example 15
Source File: CancelWatchTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testCancelAll() {
	// expecting a lot of content with frequent changes
	String userHome = System.getProperty("user.home");

	Operation<String> noOp = obj -> {
		// do nothing
	};

	for (int i = 0; i < 30; i++) {
		Watch.dir(userHome, noOp);
		U.sleep(100);
		Watch.cancelAll();
	}
}
 
Example 16
Source File: AbstractLoop.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public void waitForStatusOtherThan(LoopStatus... requiredStatuses) {
	while (Arr.contains(requiredStatuses, status)) {
		U.sleep(5);
	}
}
 
Example 17
Source File: AbstractLoop.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public void waitForStatus(LoopStatus... requiredStatuses) {
	while (!Arr.contains(requiredStatuses, status)) {
		U.sleep(5);
	}
}
 
Example 18
Source File: MultiWatchTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private void giveItTimeToRefresh() {
	U.sleep(3000);
}
 
Example 19
Source File: HttpResponseCodes.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static void init() {
	while (!ready) {
		U.sleep(1);
	}
}
 
Example 20
Source File: Wait.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static void until(AtomicBoolean condition) {
	while (!condition.get()) {
		U.sleep(10);
	}
}