/build/source/nativelink-util/src/instant_wrapper.rs
Line  | Count  | Source  | 
1  |  | // Copyright 2024 The NativeLink Authors. All rights reserved.  | 
2  |  | //  | 
3  |  | // Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License");  | 
4  |  | // you may not use this file except in compliance with the License.  | 
5  |  | // You may obtain a copy of the License at  | 
6  |  | //  | 
7  |  | //    See LICENSE file for details  | 
8  |  | //  | 
9  |  | // Unless required by applicable law or agreed to in writing, software  | 
10  |  | // distributed under the License is distributed on an "AS IS" BASIS,  | 
11  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
12  |  | // See the License for the specific language governing permissions and  | 
13  |  | // limitations under the License.  | 
14  |  |  | 
15  |  | use core::fmt::Debug;  | 
16  |  | use core::future::Future;  | 
17  |  | use core::time::Duration;  | 
18  |  | use std::time::{SystemTime, UNIX_EPOCH}; | 
19  |  |  | 
20  |  | use mock_instant::thread_local::{Instant as MockInstant, MockClock}; | 
21  |  |  | 
22  |  | /// Wrapper used to abstract away which underlying Instant impl we are using.  | 
23  |  | /// This is needed for testing.  | 
24  |  | pub trait InstantWrapper: Send + Sync + Unpin + Debug + 'static { | 
25  |  |     fn from_secs(secs: u64) -> Self;  | 
26  |  |     fn unix_timestamp(&self) -> u64;  | 
27  |  |     fn now(&self) -> SystemTime;  | 
28  |  |     fn elapsed(&self) -> Duration;  | 
29  |  |     fn sleep(self, duration: Duration) -> impl Future<Output = ()> + Send + Sync + 'static;  | 
30  |  | }  | 
31  |  |  | 
32  |  | impl InstantWrapper for SystemTime { | 
33  | 0  |     fn from_secs(secs: u64) -> Self { | 
34  | 0  |         Self::UNIX_EPOCH  | 
35  | 0  |             .checked_add(Duration::from_secs(secs))  | 
36  | 0  |             .unwrap()  | 
37  | 0  |     }  | 
38  |  |  | 
39  | 32  |     fn unix_timestamp(&self) -> u64 { | 
40  | 32  |         self.duration_since(UNIX_EPOCH).unwrap().as_secs()  | 
41  | 32  |     }  | 
42  |  |  | 
43  | 0  |     fn now(&self) -> SystemTime { | 
44  | 0  |         Self::now()  | 
45  | 0  |     }  | 
46  |  |  | 
47  | 20.9k  |     fn elapsed(&self) -> Duration { | 
48  | 20.9k  |         <Self>::elapsed(self).unwrap()  | 
49  | 20.9k  |     }  | 
50  |  |  | 
51  | 0  |     async fn sleep(self, duration: Duration) { | 
52  | 0  |         tokio::time::sleep(duration).await;  | 
53  | 0  |     }  | 
54  |  | }  | 
55  |  |  | 
56  | 0  | pub fn default_instant_wrapper() -> impl InstantWrapper { | 
57  | 0  |     SystemTime::now()  | 
58  | 0  | }  | 
59  |  |  | 
60  |  | /// Our mocked out instant that we can pass to our `EvictionMap`.  | 
61  |  | #[derive(Debug, Clone, Copy)]  | 
62  |  | pub struct MockInstantWrapped(MockInstant);  | 
63  |  |  | 
64  |  | impl Default for MockInstantWrapped { | 
65  | 1.01k  |     fn default() -> Self { | 
66  | 1.01k  |         Self(MockInstant::now())  | 
67  | 1.01k  |     }  | 
68  |  | }  | 
69  |  |  | 
70  |  | impl InstantWrapper for MockInstantWrapped { | 
71  | 36  |     fn from_secs(_secs: u64) -> Self { | 
72  | 36  |         Self(MockInstant::now())  | 
73  | 36  |     }  | 
74  |  |  | 
75  | 8  |     fn unix_timestamp(&self) -> u64 { | 
76  | 8  |         MockClock::time().as_secs()  | 
77  | 8  |     }  | 
78  |  |  | 
79  | 692  |     fn now(&self) -> SystemTime { | 
80  | 692  |         UNIX_EPOCH + MockClock::time()  | 
81  | 692  |     }  | 
82  |  |  | 
83  | 459  |     fn elapsed(&self) -> Duration { | 
84  | 459  |         self.0.elapsed()  | 
85  | 459  |     }  | 
86  |  |  | 
87  | 226  |     async fn sleep(self, duration: Duration) {180   | 
88  | 180  |         let baseline = self.0.elapsed();  | 
89  |  |         loop { | 
90  | 3.04k  |             tokio::task::yield_now().await;  | 
91  | 2.97k  |             if self.0.elapsed() - baseline >= duration {  Branch (91:16): [Folded - Ignored]
  Branch (91:16): [Folded - Ignored]
  Branch (91:16): [True: 0, False: 1]
  Branch (91:16): [True: 115, False: 2.86k]
  | 
92  | 115  |                 break;  | 
93  | 2.86k  |             }  | 
94  |  |         }  | 
95  | 115  |     }  | 
96  |  | }  |