summaryrefslogtreecommitdiff
path: root/common/device-side/util-axt/tests/src/com/android/compatibility/common/util/TimeoutTest.java
blob: 8992d181556fd13172990d4f5cdcc2e9df34204f (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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.android.compatibility.common.util;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.when;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.expectThrows;

import android.os.SystemClock;

import com.android.compatibility.common.util.Timeout.Sleeper;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.concurrent.Callable;

@RunWith(MockitoJUnitRunner.class)
public class TimeoutTest {

    private static final String NAME = "TIME, Y U NO OUT?";
    private static final String DESC = "something";

    @Mock
    private Callable<Object> mJob;

    private final MySleeper mSleeper = new MySleeper();

    @Test
    public void testInvalidConstructor() {
        // Invalid name
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(null, 1, 2, 2));
        assertThrows(IllegalArgumentException.class, ()-> new Timeout("", 1, 2, 2));
        // Invalid initial value
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, -1, 2, 2));
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 0, 2, 2));
        // Invalid multiplier
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, -1, 2));
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 0, 2));
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 1, 2));
        // Invalid max value
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 2, -1));
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 2, 0));
        // Max value cannot be less than initial
        assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 2, 2, 1));
    }

    @Test
    public void testGetters() {
        final Timeout timeout = new Timeout(NAME, 1, 2, 5);
        assertThat(timeout.ms()).isEqualTo(1);
        assertFloat(timeout.getMultiplier(), 2);
        assertThat(timeout.getMaxValue()).isEqualTo(5);
        assertThat(timeout.getName()).isEqualTo(NAME);
    }

    @Test
    public void testIncrease() {
        final Timeout timeout = new Timeout(NAME, 1, 2, 5);
        // Pre-maximum
        assertThat(timeout.increase()).isEqualTo(1);
        assertThat(timeout.ms()).isEqualTo(2);
        assertThat(timeout.increase()).isEqualTo(2);
        assertThat(timeout.ms()).isEqualTo(4);
        // Post-maximum
        assertThat(timeout.increase()).isEqualTo(4);
        assertThat(timeout.ms()).isEqualTo(5);
        assertThat(timeout.increase()).isEqualTo(5);
        assertThat(timeout.ms()).isEqualTo(5);
    }

    @Test
    public void testRun_invalidArgs() {
        final Timeout timeout = new Timeout(NAME, 1, 2, 5);
        // Invalid description
        assertThrows(IllegalArgumentException.class, ()-> timeout.run(null, mJob));
        assertThrows(IllegalArgumentException.class, ()-> timeout.run("", mJob));
        // Invalid max attempts
        assertThrows(IllegalArgumentException.class, ()-> timeout.run(DESC, -1, mJob));
        assertThrows(IllegalArgumentException.class, ()-> timeout.run(DESC, 0, mJob));
        // Invalid job
        assertThrows(IllegalArgumentException.class, ()-> timeout.run(DESC, null));
    }

    @Test
    public void testRun_successOnFirstAttempt() throws Exception {
        final Timeout timeout = new Timeout(mSleeper, NAME, 100, 2, 500);
        final Object result = new Object();
        when(mJob.call()).thenReturn(result);
        assertThat(timeout.run(DESC, 1, mJob)).isSameAs(result);
        assertThat(mSleeper.totalSleepingTime).isEqualTo(0);
    }

    @Test
    public void testRun_successOnSecondAttempt() throws Exception {
        final Timeout timeout = new Timeout(mSleeper, NAME, 100, 2, 500);
        final Object result = new Object();
        when(mJob.call()).thenReturn((Object) null, result);
        assertThat(timeout.run(DESC, 10, mJob)).isSameAs(result);
        assertThat(mSleeper.totalSleepingTime).isEqualTo(10);
    }

    @Test
    public void testRun_allAttemptsFailed() throws Exception {
        final Timeout timeout = new Timeout(mSleeper, NAME, 100, 2, 500);
        final RetryableException e = expectThrows(RetryableException.class,
                () -> timeout.run(DESC, 10, mJob));
        assertThat(e.getMessage()).contains(DESC);
        assertThat(e.getTimeout()).isSameAs(timeout);
        assertThat(mSleeper.totalSleepingTime).isEqualTo(100);
    }

    private static final class MySleeper implements Sleeper {
        public long totalSleepingTime;

        @Override
        public void sleep(long napTimeMs) {
            // We still need to sleep, as the retry is based on ellapsed time. We could use a
            // Mockito spy, but let's keep it simple
            SystemClock.sleep(napTimeMs);
            totalSleepingTime += napTimeMs;
        }
    }

    public static void assertFloat(float actualValue, float expectedValue) {
        assertThat(actualValue).isWithin(1.0e-10f).of(expectedValue);
    }
}