mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: time.wait() => time.sleep()
This commit is contained in:
@@ -7,7 +7,7 @@ fn test_stopwatch_works_as_intended() {
|
||||
mut sw := time.new_stopwatch({})
|
||||
// sample code that you want to measure:
|
||||
println('Hello world')
|
||||
time.wait(1 * time.millisecond)
|
||||
time.sleep(1 * time.millisecond)
|
||||
//
|
||||
println('Greeting the world took: ${sw.elapsed().nanoseconds()}ns')
|
||||
assert sw.elapsed().nanoseconds() > 0
|
||||
@@ -16,18 +16,18 @@ fn test_stopwatch_works_as_intended() {
|
||||
fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed() {
|
||||
println('Testing pause function')
|
||||
mut sw := time.new_stopwatch({})
|
||||
time.wait(10 * time.millisecond) // A
|
||||
time.sleep(10 * time.millisecond) // A
|
||||
eprintln('Elapsed after 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
||||
assert sw.elapsed().milliseconds() >= 8 // sometimes it sleeps for 9ms on windows..
|
||||
sw.pause()
|
||||
time.wait(10 * time.millisecond)
|
||||
time.sleep(10 * time.millisecond)
|
||||
eprintln('Elapsed after pause and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
||||
assert sw.elapsed().milliseconds() >= 8
|
||||
$if stopwatch ? {
|
||||
assert sw.elapsed().milliseconds() < 20
|
||||
}
|
||||
sw.start()
|
||||
time.wait(10 * time.millisecond) // B
|
||||
time.sleep(10 * time.millisecond) // B
|
||||
eprintln('Elapsed after resume and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
||||
assert sw.elapsed().milliseconds() >= 18
|
||||
$if stopwatch ? {
|
||||
|
||||
@@ -324,20 +324,22 @@ pub fn ticks() i64 {
|
||||
// # return (double)(* (uint64_t *) &elapsedNano) / 1000000;
|
||||
}
|
||||
|
||||
/*
|
||||
// sleep makes the calling thread sleep for a given number of seconds.
|
||||
[deprecated: 'call time.wait(n * time.second)']
|
||||
[deprecated: 'call time.sleep(n * time.second)']
|
||||
pub fn sleep(seconds int) {
|
||||
wait(seconds * time.second)
|
||||
}
|
||||
*/
|
||||
|
||||
// sleep_ms makes the calling thread sleep for a given number of milliseconds.
|
||||
[deprecated: 'call time.wait(n * time.millisecond)']
|
||||
[deprecated: 'call time.sleep(n * time.millisecond)']
|
||||
pub fn sleep_ms(milliseconds int) {
|
||||
wait(milliseconds * time.millisecond)
|
||||
}
|
||||
|
||||
// usleep makes the calling thread sleep for a given number of microseconds.
|
||||
[deprecated: 'call time.wait(n * time.microsecond)']
|
||||
[deprecated: 'call time.sleep(n * time.microsecond)']
|
||||
pub fn usleep(microseconds int) {
|
||||
wait(microseconds * time.microsecond)
|
||||
}
|
||||
|
||||
@@ -131,7 +131,14 @@ pub fn zero_timespec() C.timespec {
|
||||
}
|
||||
|
||||
// wait makes the calling thread sleep for a given duration (in nanoseconds).
|
||||
[deprecated: 'call time.sleep(n * time.second)']
|
||||
pub fn wait(duration Duration) {
|
||||
ts := &C.timespec{duration / second, duration % second}
|
||||
C.nanosleep(ts, C.NULL)
|
||||
}
|
||||
|
||||
// sleep makes the calling thread sleep for a given duration (in nanoseconds).
|
||||
pub fn sleep(duration Duration) {
|
||||
ts := &C.timespec{duration / second, duration % second}
|
||||
C.nanosleep(ts, C.NULL)
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ fn test_utc() {
|
||||
|
||||
fn test_unix_time() {
|
||||
t1 := time.utc()
|
||||
time.wait(50 * time.millisecond)
|
||||
time.sleep(50 * time.millisecond)
|
||||
t2 := time.utc()
|
||||
ut1 := t1.unix_time()
|
||||
ut2 := t2.unix_time()
|
||||
|
||||
Reference in New Issue
Block a user