@BeforeClass methods won't run in parallel

330 views
Skip to first unread message

aidan.short

unread,
Aug 28, 2009, 8:16:11 PM8/28/09
to testng-dev
I posted this on the opensymphony forum, but didn't get a reply.

I've found that @BeforeClass methods won't run in parallel, even when
they're running in separate threads.

I created the following:

public class TestA {
@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
System.out.println(System.currentTimeMillis() + " -
TestA.BeforeClass in " + Thread.currentThread().getId());
Thread.sleep(5000);
}
@Test
public void execute() {
System.out.println(System.currentTimeMillis() + " - Running TestA in
" + Thread.currentThread().getId());
}
}

public class TestB {

@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
System.out.println(System.currentTimeMillis() + " -
TestB.BeforeClass in " + Thread.currentThread().getId());
Thread.sleep(5000);
}

@Test
public void execute() {
System.out.println(System.currentTimeMillis() + " - Running TestB in
" + Thread.currentThread().getId());
}
}



Even if I specify parallel="methods" in my testng.xml, the
@BeforeClass methods seem to be executing in sequence, rather than in
parallel. I get output like this: (note that the BeforeClass methods
are about 5 seconds removed)

1251215403843 - TestA.BeforeClass in 8
1251215408844 - TestB.BeforeClass in 9
1251215408859 - Running TestA in 8
1251215413859 - Running TestB in 9



Whereas, if I move the Thread.sleep(5000) into the @Test methods, they
run in parallel, and I get output like this: (note that the
BeforeClass methods kick off within milliseconds of each other, and
the Test methods kick off immediately afterward, at the same time).

1251215753393 - TestA.BeforeClass in 8
1251215753409 - TestB.BeforeClass in 9
1251215753424 - Running TestA in 8
1251215753424 - Running TestB in 9

Is this a bug? Is it possible to get the @BeforeClass methods to run
in parallel?

Cedric Beust

unread,
Aug 28, 2009, 8:28:46 PM8/28/09
to testng-dev
This trace shows that @BeforeClass methods *are* running in parallel
(they
are in different threads), so I'm not sure what the problem is?

--
Cedric

Ajit

unread,
Aug 30, 2009, 9:52:39 PM8/30/09
to testng-dev
Yes, although @BeforeClass methods are running in different threads, i
guess the issue seems to be TestB.BeforeClass seems to be waiting for
TestA.BeforeClass to finish. From the example Aidan gave i noted that

1251215408844 - TestB.BeforeClass in 9 started 5 seconds after
1251215403843 - TestA.BeforeClass in 8

In my views, problem is that even if @BeforeClass are in different
threads, these seems to be dependency between threads for
@BeforeClass, TestB.BeforeClass seems to have started only after
TestA.BeforeClass finished execution.

Aidan Short

unread,
Aug 31, 2009, 7:05:20 PM8/31/09
to testng-dev
I did some debugging, and it seems that the @BeforeClass method in
TestB really won't start running until the @BeforeClass method in
TestA is complete. The reason for this is in
org.testng.internal.TestMethodWorker.invokeBeforeClassMethods() - the
actual BeforeClass method invocation is within a synchronized block.
So thread 9 can't run TestB.setup() until thread 8 relinquishes
control of the synchronized object, which it doesn't until TestA.setup
() is finished.

On Aug 28, 5:28 pm, Cedric Beust <cbe...@gmail.com> wrote:

Cédric Beust ♔

unread,
Sep 1, 2009, 1:47:55 PM9/1/09
to testn...@googlegroups.com, aidan...@gmail.com, ajit.za...@gmail.com
On Mon, Aug 31, 2009 at 4:05 PM, Aidan Short <aidan...@gmail.com> wrote:

I did some debugging, and it seems that the @BeforeClass method in
TestB really won't start running until the @BeforeClass method in
TestA is complete. The reason for this is in
org.testng.internal.TestMethodWorker.invokeBeforeClassMethods() - the
actual BeforeClass method invocation is within a synchronized block.
So thread 9 can't run TestB.setup() until thread 8 relinquishes
control of the synchronized object, which it doesn't until TestA.setup
() is finished.

Good catch!

I think I fixed it, can you download the beta at http://testng.org/beta and let me know if this problem is now fixed?

-- 
Cédric

Aidan Short

unread,
Sep 1, 2009, 6:55:24 PM9/1/09
to Cédric Beust ♔, Ajit Zadgaonkar, testn...@googlegroups.com
Fixed! Runs exactly as expected now.

Thanks a lot for your help, and thanks for creating and maintaining
such a great project.

2009/9/1 Cédric Beust ♔ <cbe...@google.com>:

Cédric Beust ♔

unread,
Sep 1, 2009, 6:56:54 PM9/1/09
to Aidan Short, Ajit Zadgaonkar, testn...@googlegroups.com
You're welcome, and thanks for helping track this down!

--
Cédric

Ajit Zadgaonkar

unread,
Sep 1, 2009, 10:58:31 PM9/1/09
to Cédric Beust ♔, Aidan Short, testn...@googlegroups.com
Thank you both of you. TestNG is a wonderful product and empower great
deal of creative testing. I also got a chance to verify the fix in
5.11 beta, it works now. Do you have an idea about 5.11 final release
timelines?

Ajit

2009/9/1 Cédric Beust ♔ <cbe...@google.com>:

Reply all
Reply to author
Forward
0 new messages