1 package test.timeout;
2 
3 import org.testng.annotations.DataProvider;
4 import org.testng.annotations.Test;
5 import org.testng.xml.XmlSuite;
6 
7 import java.util.Arrays;
8 import java.util.Iterator;
9 
10 import test.BaseTest;
11 
12 public class TimeOutTest extends BaseTest {
13   private final long m_id;
14 
TimeOutTest()15   public TimeOutTest() {
16     m_id = System.currentTimeMillis();
17   }
18 
privateTimeOutTest(XmlSuite.ParallelMode parallel)19   private void privateTimeOutTest(XmlSuite.ParallelMode parallel) {
20     addClass(TimeOutSampleTest.class);
21     if (parallel != null) {
22       setParallel(parallel);
23     }
24     run();
25 
26     verifyPassedTests("timeoutShouldPass");
27     verifyFailedTests("timeoutShouldFailByException", "timeoutShouldFailByTimeOut");
28   }
29 
30   @DataProvider(name = "parallelModes")
createData()31   public Iterator<Object[]> createData() {
32     final Iterator<XmlSuite.ParallelMode> parallelModes = Arrays.asList(XmlSuite.ParallelMode.values()).iterator();
33     return new Iterator<Object[]>() {
34       @Override
35       public boolean hasNext() {
36         return parallelModes.hasNext();
37       }
38 
39       @Override
40       public Object[] next() {
41         return new Object[]{ parallelModes.next() };
42       }
43 
44       @Override
45       public void remove() {
46         throw new UnsupportedOperationException("remove");
47       }
48     };
49   }
50 
51 
52   @Test(dataProvider = "parallelModes")
53   public void timeOutInParallel(XmlSuite.ParallelMode parallelMode) {
54     privateTimeOutTest(parallelMode);
55   }
56 
57   @Test
58   public void timeOutInNonParallel() {
59     privateTimeOutTest(null);
60   }
61 
62   @Test
63   public void verifyInvocationTimeOut() {
64     addClass(InvocationTimeOutSampleTest.class);
65     run();
66     verifyPassedTests("shouldPass");
67     verifyFailedTests("shouldFail");
68   }
69 
70   @Override
71   public Long getId() {
72     return m_id;
73   }
74 }
75