0

I am running a series of tests with testng and am running them at the parallel="classes" level. The problem is I want 3-4 classes to always be ran together on the same thread (single threaded). All other classes can be ran on whatever thread, I do not care. Is there a way to do this?

Example: A B and C need to be ran one-at-a-time. All other classes will be denoted by numbers. 
 Threads running at one time:    
    A 3 7 2 - A would run while 3 other classes run
    C 8 4 1 - B would run once A is done, (other threads dont matter)
    B 5 9 6 - C would run once B is done.

For more information, I have 3-4 classes that can not be ran in parallel because of what they do, I basically just want these to be ran one at a time, and want to guarantee only one of these classes is ever running at once. All my other classes can be run at any given time in any order with no problems.

I know I can set parallel to the group level, but then I have to go add random pointless groups to all my other classes so that they run in the meantime. Perhaps there is a way to single thread one group and parallel another?

I currently am running 4 threads and have about 50 classes.

Any ideas?

-- Added requirements/thoughts based on comments --

The main reason I am trying to do it this way is I am testing a server API. Some of the calls take a long time on the server side, so while I am waiting for a response, I might as well send some other calls as well, hence the multi threading. I converted all my tests to run in multithreading in order to shave a very significant time off of the overall test suite. It works very well, however there are these four classes which can not be thrown at the server at the same time or this will cause test failures. Note: I am aware that one way would be to "fix these classes so that they can work in parallel" and I have tried that, and will continue to if possible, but that’s a different longer story, lets just now assume that is not possible.

Hence, its practically all about the time. Solutions which would in essence run single threaded by sleeping all other threads I am not interested in as that would defeat the whole purpose.

Nick Humrich
  • 14,905
  • 8
  • 62
  • 85

2 Answers2

1

You can bind them all to the same cpu. This would mean they cannot be running at the time time.

The problem is that just having one at a timer running is rarely enough. Instead you should use locking between these threads so that they can hand over execution at known points and in a way you can reason about.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Binding would not work, because lets say I have 2 cores and 4 threads, (or more then 4 threads) I could still get a situation where these classes are parallel, they just wont be parallel on the cpu, which isnt the only issue. If that were the case I would just use syncronize blocks on the hot spots. I am not sure how locking would work from class to class, but I am willing to listen. So far the problem with locking as I can think of it is that I might have all four classes at one time, If i use a lock, then 3 threads are doing nothing except waiting for an unlock – Nick Humrich Jul 30 '13 at 18:41
  • @Humdinger Having one thread running and 3 thread not running is what only having one thread running at a time means to me. Perhaps you can rephrase what you are trying to achieve. As your requirement is not clear, I cannot see why you would want multiple threads which do not run in parallel. I would just have one thread. – Peter Lawrey Jul 30 '13 at 18:44
  • From a normal standpoint single threaded would be the way to go. I added some information at the end of my question to clarify what exactly I am doing and why I prefer multi-threading. – Nick Humrich Jul 30 '13 at 18:52
  • 1
    I would use one shared lock for all the requests which cannot be used concurrently. I assume you have other requests you could be sending in additional threads. Since you will be waiting for the server most of the time, you won't be using even one thread most of the time. – Peter Lawrey Jul 30 '13 at 18:58
  • Its a solution, but not necessarily one I would prefer because I would prefer the classes themselves to be one at a time rather then the calls. I will upvote your comment but leave unanswered in order to leave the question open. I thought about locking them at a beforeClass and AfterClass level, but then that could possibly result in possibly sleeping all threads. – Nick Humrich Jul 30 '13 at 19:10
  • An alternative is to select requests from a pool where it will not give you multiple requests which cannot be run at once. This way you don't have to have 8 threads just because 3 might be blocked. – Peter Lawrey Jul 30 '13 at 19:13
  • Can you explain how I might do this? – Nick Humrich Jul 30 '13 at 19:47
  • 1
    http://msdn.microsoft.com/en-us/library/3dasc8as.aspx - Alternativly i would create a list array that holds what all the user clicked on to have executed, then run a check against that list as you come out of the thread and run , or don't run something. – Don Thomas Boyle Jul 30 '13 at 20:15
  • @DonThomasBoyle I dont understand how this relates. You might want to go into more detail. – Nick Humrich Aug 08 '13 at 18:14
1

After many painstaking attempts of trying to find a workaround for this, I was reading the TestNG spec more clearly and found the solution. Its actually quite simple. The <suite> has a parallel level and so does the <test> That brought me to the following

<suite name="testSuite" parallel="tests" thread-count="4">
   <test name="foo" parallel="classes" thread-count="3">
     .. (classes you want to include)
   </test>
   <test name="bar">  <!-- this runs single threaded because there is no parallel tag>
     .. (classes you want to run single threaded)
   </test>
</suite>
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
  • Do you know how to do this with annotations? All of my tests are within @Test(groups = {"group1}. I have some classes where running each method 1 by 1 is normal, and another class where I want all the tests to execute after each other. It would be nice to have your xml answer, but in code or the annotations. – Will Jan 04 '17 at 22:44