aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/packages/TestTimeoutTest.java
blob: 9134ce16355c43e81666c2a681f78ebc9aff480d (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2009 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.packages;

import static com.google.devtools.build.lib.packages.TestTimeout.ETERNAL;
import static com.google.devtools.build.lib.packages.TestTimeout.LONG;
import static com.google.devtools.build.lib.packages.TestTimeout.MODERATE;
import static com.google.devtools.build.lib.packages.TestTimeout.SHORT;
import static com.google.devtools.build.lib.packages.TestTimeout.getSuggestedTestTimeout;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests the various methods of {@link TestTimeout}
 */
@RunWith(JUnit4.class)
public class TestTimeoutTest {

  @Test
  public void testBasicConversion() throws Exception {
    assertSame(SHORT, TestTimeout.valueOf("SHORT"));
    assertSame(MODERATE, TestTimeout.valueOf("MODERATE"));
    assertSame(LONG, TestTimeout.valueOf("LONG"));
    assertSame(ETERNAL, TestTimeout.valueOf("ETERNAL"));
  }

  @Test
  public void testSuggestedTestSize() throws Exception {
    assertEquals(SHORT, getSuggestedTestTimeout(0));
    assertEquals(SHORT, getSuggestedTestTimeout(2));
    assertEquals(SHORT, getSuggestedTestTimeout(6));
    assertEquals(SHORT, getSuggestedTestTimeout(59));
    assertEquals(MODERATE, getSuggestedTestTimeout(60));
    assertEquals(MODERATE, getSuggestedTestTimeout(299));
    assertEquals(LONG, getSuggestedTestTimeout(300));
    assertEquals(LONG, getSuggestedTestTimeout(899));
    assertEquals(ETERNAL, getSuggestedTestTimeout(900));
    assertEquals(ETERNAL, getSuggestedTestTimeout(1234567890));
  }

  @Test
  public void testIsInRangeExact() throws Exception {
    assertTrue(SHORT.isInRangeExact(0));
    assertTrue(SHORT.isInRangeExact(1));
    assertFalse(SHORT.isInRangeExact(60));
    assertTrue(MODERATE.isInRangeExact(60));
    assertTrue(MODERATE.isInRangeExact(299));
    assertFalse(MODERATE.isInRangeExact(300));
    assertTrue(LONG.isInRangeExact(300));
    assertTrue(LONG.isInRangeExact(899));
    assertFalse(LONG.isInRangeExact(900));
    assertTrue(ETERNAL.isInRangeExact(900));
    assertFalse(ETERNAL.isInRangeExact(1234567890));
  }

  @Test
  public void testIsInRangeFuzzy() throws Exception {
    assertFuzzyRange(SHORT, 0, 105);
    assertFuzzyRange(MODERATE, 8, 525);
    assertFuzzyRange(LONG, 75, 1575);
    assertFuzzyRange(ETERNAL, 225, Integer.MAX_VALUE);
  }

  private void assertFuzzyRange(TestTimeout timeout, int min, int max) {
    if (min > 0) {
      assertFalse(timeout.isInRangeFuzzy(min - 1));
    }
    assertTrue(timeout.isInRangeFuzzy(min));
    assertTrue(timeout.isInRangeFuzzy(min + 1));
    assertTrue(timeout.isInRangeFuzzy(max - 1));
    assertTrue(timeout.isInRangeFuzzy(max));
    if (max < Integer.MAX_VALUE) {
      assertFalse(timeout.isInRangeFuzzy(max + 1));
    }
  }
}