xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testsleep.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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 
17 #include "time.h"
18 #include "apr_thread_proc.h"
19 #include "apr_errno.h"
20 #include "apr_general.h"
21 #include "apr_lib.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "testutil.h"
25 
26 #define SLEEP_INTERVAL 5
27 
sleep_one(abts_case * tc,void * data)28 static void sleep_one(abts_case *tc, void *data)
29 {
30     time_t pretime = time(NULL);
31     time_t posttime;
32     time_t timediff;
33 
34     apr_sleep(apr_time_from_sec(SLEEP_INTERVAL));
35     posttime = time(NULL);
36 
37     /* normalize the timediff.  We should have slept for SLEEP_INTERVAL, so
38      * we should just subtract that out.
39      */
40     timediff = posttime - pretime - SLEEP_INTERVAL;
41     ABTS_TRUE(tc, timediff >= 0);
42     ABTS_TRUE(tc, timediff <= 1);
43 }
44 
testsleep(abts_suite * suite)45 abts_suite *testsleep(abts_suite *suite)
46 {
47     suite = ADD_SUITE(suite)
48 
49     abts_run_test(suite, sleep_one, NULL);
50 
51     return suite;
52 }
53 
54