02d0670f5eb79f436c4977cd62d10a0c07391dd9
[folly.git] / folly / io / async / test / Util.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 #pragma once
20
21 #include <folly/io/async/test/TimeUtil.h>
22 #include <folly/test/TestUtils.h>
23 #include <folly/portability/GTest.h>
24
25 /**
26  * Check how long a timeout took to fire.
27  *
28  * This method verifies:
29  * - that the timeout did not fire too early (never less than expectedMS)
30  * - that the timeout fired within a reasonable period of the expected
31  *   duration.  It must fire within the specified tolerance, excluding time
32  *   that this process spent waiting to be scheduled.
33  *
34  * @param start                 A TimePoint object set just before the timeout
35  *                              was scheduled.
36  * @param end                   A TimePoint object set when the timeout fired.
37  * @param expectedMS            The timeout duration, in milliseconds
38  * @param tolerance             The tolerance, in milliseconds.
39  */
40 #define T_CHECK_TIMEOUT(start, end, expectedMS, ...)     \
41   if (!::folly::checkTimeout((start), (end),             \
42                              (expectedMS), false,        \
43                              ##__VA_ARGS__)) {           \
44     SKIP() << "T_CHECK_TIMEOUT lapsed";                  \
45   }
46
47 /**
48  * Verify that an event took less than a specified amount of time.
49  *
50  * This is similar to T_CHECK_TIMEOUT, but does not fail if the event took less
51  * than the allowed time.
52  */
53 #define T_CHECK_TIME_LT(start, end, expectedMS, ...)     \
54   if (!::folly::checkTimeout((start), (end),             \
55                              (expectedMS), true,         \
56                              ##__VA_ARGS__)) {           \
57     SKIP() << "T_CHECK_TIMEOUT_LT lapsed";               \
58   }