-
Notifications
You must be signed in to change notification settings - Fork 2k
remove some of thread.sleeps in test cases #1786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.Semaphore; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
@@ -82,7 +83,7 @@ public static InputStream makeStream(byte[] prefix, byte[] data) { | |
return new ByteArrayInputStream(out); | ||
} | ||
|
||
public static Thread asyncCopy(final InputStream is, final OutputStream os) { | ||
public static Thread asyncCopy(final InputStream is, final OutputStream os, Semaphore sem) { | ||
Thread t = | ||
new Thread( | ||
new Runnable() { | ||
|
@@ -91,6 +92,8 @@ public void run() { | |
Streams.copy(is, os); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} finally { | ||
sem.release(); | ||
} | ||
} | ||
}); | ||
|
@@ -110,14 +113,17 @@ public void testExecProcess() throws IOException, InterruptedException { | |
|
||
final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); | ||
final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); | ||
|
||
Thread t1 = asyncCopy(process.getInputStream(), stdout); | ||
Thread t2 = asyncCopy(process.getErrorStream(), stderr); | ||
final Semaphore sem = new Semaphore(2); | ||
sem.acquire(2); | ||
Thread t1 = asyncCopy(process.getInputStream(), stdout, sem); | ||
Thread t2 = asyncCopy(process.getErrorStream(), stderr, sem); | ||
|
||
process.getHandler().bytesMessage(makeStream(3, OUTPUT_EXIT0.getBytes(StandardCharsets.UTF_8))); | ||
// TODO: Fix this asap! | ||
Thread.sleep(1000); | ||
|
||
while (!sem.tryAcquire(2)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, why not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wanted to print message on why we are waiting.. |
||
System.out.println( | ||
"waiting for async Copy task to be completed in ExecTest::testExecProcess"); | ||
} | ||
process.destroy(); | ||
|
||
assertEquals(msgData, stdout.toString()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for this loop? I would remove all this code and just do:
sem.acquire()
like you did above.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wanted to print message on why we are waiting..
but i have changed semaphores to countdown latch, as i thought it would be better than sempahore in these situations