/
Base.java
59 lines (50 loc) · 1.42 KB
/
Base.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package testng.parallelOrdering;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class Base
{
private static final AtomicInteger resources = new AtomicInteger();
// This simulates resource allocation
@BeforeClass
public void setupAllocateResources()
{
int concurrentResources = resources.incrementAndGet();
if (concurrentResources > 2) {
throw new IllegalStateException("Tests execute in two threads, so there should be at most 2 resources allocated, got: " + concurrentResources);
}
}
// This simulates freeing resources
@AfterClass(alwaysRun = true)
public void tearDownReleaseResources()
{
resources.decrementAndGet();
}
@Test
public void test1()
{
sleepShortly();
}
@Test
public void test2()
{
sleepShortly();
}
@Test
public void test3()
{
sleepShortly();
}
// Sleep random time to let tests interleave. Keep sleep short not to extend tests duration too much.
private void sleepShortly()
{
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(3));
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}