Remove unecessary iostream include
[folly.git] / folly / io / async / test / TimeUtil.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17
18 #include <chrono>
19 #include <iosfwd>
20
21 #include <folly/portability/SysTypes.h>
22
23 namespace folly {
24
25 class TimePoint {
26  public:
27   explicit TimePoint(bool set = true)
28     : tid_(0) {
29     if (set) {
30       reset();
31     }
32   }
33
34   void reset();
35
36   bool isUnset() const {
37     return (timeStart_.time_since_epoch().count() == 0 &&
38             timeEnd_.time_since_epoch().count() == 0 &&
39             timeWaiting_.count() == 0);
40   }
41
42   std::chrono::system_clock::time_point getTime() const {
43     return timeStart_;
44   }
45
46   std::chrono::system_clock::time_point getTimeStart() const {
47     return timeStart_;
48   }
49
50   std::chrono::system_clock::time_point getTimeEnd() const {
51     return timeStart_;
52   }
53
54   std::chrono::milliseconds getTimeWaiting() const {
55     return timeWaiting_;
56   }
57
58   pid_t getTid() const {
59     return tid_;
60   }
61
62  private:
63   std::chrono::system_clock::time_point timeStart_;
64   std::chrono::system_clock::time_point timeEnd_;
65   std::chrono::milliseconds timeWaiting_{0};
66   pid_t tid_;
67 };
68
69 std::ostream& operator<<(std::ostream& os, const TimePoint& timePoint);
70
71 bool checkTimeout(const TimePoint& start,
72                   const TimePoint& end,
73                   std::chrono::milliseconds expectedMS,
74                   bool allowSmaller,
75                   std::chrono::milliseconds tolerance =
76                   std::chrono::milliseconds(5));
77
78 }