Issue 12 in thread-weaver: Test clasims the thread will not finish but they should

9 views
Skip to first unread message

thread...@googlecode.com

unread,
Oct 21, 2014, 6:49:49 AM10/21/14
to thread...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 12 by martin.v...@gmail.com: Test clasims the thread will not
finish but they should
https://code.google.com/p/thread-weaver/issues/detail?id=12

What steps will reproduce the problem?
1. create an object with the followings method and field
private ConcurrentMap<Object, CountDownLatch> latchesPerKeys = new
ConcurrentHashMap<Object, CountDownLatch>();

public void testedMethod(Map<K, V> values, Object key) {
CountDownLatch newLatch = new CountDownLatch(1);
CountDownLatch oldLatch = latchesPerKeys.putIfAbsent(key, newLatch);
if (oldLatch == null) { // the first thread
newLatch.countDown();
}
else { // the second thread
try {
boolean succWait = oldLatch.await(5, TimeUnit.SECONDS);
if (!succWait) {
oldLatch.countDown();
}
}
catch (InterruptedException e) {
}
}
}
2. test it with
@ThreadedMain
public void mainThread() {
Map<Integer, Integer> values = new HashMap<Integer, Integer>();
object.testedMethod(values, 1);
}

@ThreadedSecondary
public void secondThread() {
Map<Integer, Integer> values = new HashMap<Integer, Integer>();
object.testedMethod(values, 1);
}
3.

What is the expected output? What do you see instead?
Test should be green but it says that "Thread will not finish".

What version of the product are you using? On what operating system?
ThreadWeaver 0.2 on Windows 7.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

thread...@googlecode.com

unread,
Oct 21, 2014, 7:43:34 PM10/21/14
to thread...@googlegroups.com
Updates:
Status: Invalid

Comment #1 on issue 12 by alasdair.mackintosh: Test clasims the thread will
I reproduced this myself with the default options. However, if you increase
the timeout, it works. You can try:

AnnotatedTestRunner runner = new AnnotatedTestRunner();
runner.setTimeout(10000); // in millis - make this bigger if need be
runner.setDebug(true); // this can be useful to see what's going on.

Please let me know if you still see problems after updating the timeout.

thread...@googlecode.com

unread,
Jan 6, 2015, 7:34:14 AM1/6/15
to thread...@googlegroups.com

Comment #2 on issue 12 by martin.v...@gmail.com: Test clasims the thread
Hello, it works after this change but the test really takes a long time. Is
such a long waiting really necessary?

Alasdair Mackintosh

unread,
Jan 6, 2015, 12:33:09 PM1/6/15
to thread...@googlegroups.com
On Tue, Jan 6, 2015 at 4:34 AM, <thread...@googlecode.com> wrote:
>
> Comment #2 on issue 12 by martin.v...@gmail.com: Test clasims the thread
> will not finish but they should
> https://code.google.com/p/thread-weaver/issues/detail?id=12
>
> Hello, it works after this change but the test really takes a long time. Is
> such a long waiting really necessary?

It looks as though your test has an inbuilt wait() that can take 5
seconds. This is what is slowing down the test.

1 public void testedMethod(Map<K, V> values, Object key) {
2 CountDownLatch newLatch = new CountDownLatch(1);
3 CountDownLatch oldLatch = latchesPerKeys.putIfAbsent(key, newLatch);
4 if (oldLatch == null) { // the first thread
5 newLatch.countDown();
6 }
7 else { // the second thread
8 try {
9 boolean succWait = oldLatch.await(5, TimeUnit.SECONDS);
10 if (!succWait) {
11 oldLatch.countDown();
12 }
13 }
14 catch (InterruptedException e) {
15 }
16 }
17 }

The first thread will pause before executing line 4. The second thread
will then wait for 5 seconds at line 8 before continuing. In the next
iteration, the first thread will pause before line 5, and you'll have
another 5 second delay.
Reply all
Reply to author
Forward
0 new messages