From 3a2d696facedaa1f179d6196a9db25966ea50818 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 27 Feb 2021 20:41:06 +0300 Subject: [PATCH] time: time.wait() => time.sleep() --- cmd/tools/fast/fast_job.v | 2 +- cmd/tools/test_os_process.v | 6 +++--- cmd/tools/vtest-parser.v | 6 +++--- examples/buf_reader.v | 2 +- examples/concurrency/concurrency.v | 2 +- examples/flappylearning/game.v | 2 +- examples/game_of_life/life.v | 2 +- examples/hot_reload/bounce.v | 2 +- examples/hot_reload/message.v | 2 +- examples/lander.v | 2 +- examples/sokol/sounds/simple_sin_tones.v | 2 +- examples/sokol/sounds/wav_player.v | 2 +- examples/tetris/tetris.v | 2 +- examples/vweb/server_sent_events/server.v | 2 +- examples/websocket/ping.v | 4 ++-- vlib/clipboard/clipboard_windows.c.v | 2 +- vlib/clipboard/x11/clipboard.c.v | 4 ++-- vlib/os/process_test.v | 2 +- vlib/rand/random_identifiers_test.v | 4 ++-- vlib/sync/channel_select_2_test.v | 2 +- vlib/sync/pool/pool_test.v | 4 ++-- vlib/term/ui/input_windows.c.v | 2 +- vlib/term/ui/termios_nix.c.v | 2 +- vlib/time/stopwatch_test.v | 8 ++++---- vlib/time/time.v | 8 +++++--- vlib/time/time_nix.c.v | 7 +++++++ vlib/time/time_test.v | 2 +- vlib/v/live/executable/reloader.v | 2 +- vlib/v/live/live_test.v | 8 ++++---- vlib/v/parser/parser.v | 2 +- vlib/v/tests/autolock_array1_test.v | 2 +- vlib/v/tests/go_wait_2_test.v | 2 +- vlib/v/tests/shared_array_test.v | 2 +- vlib/v/tests/shared_lock_2_test.v | 2 +- vlib/v/tests/shared_lock_3_test.v | 8 ++++---- vlib/v/tests/shared_lock_4_test.v | 8 ++++---- vlib/v/tests/shared_lock_test.v | 2 +- vlib/v/util/util.v | 2 +- vlib/vweb/tests/vweb_test.v | 4 ++-- vlib/vweb/tests/vweb_test_server.v | 4 ++-- vlib/x/websocket/websocket_server.v | 2 +- vlib/x/websocket/websocket_test.v | 6 +++--- 42 files changed, 76 insertions(+), 67 deletions(-) diff --git a/cmd/tools/fast/fast_job.v b/cmd/tools/fast/fast_job.v index 26707aa47..ed72a7431 100644 --- a/cmd/tools/fast/fast_job.v +++ b/cmd/tools/fast/fast_job.v @@ -36,6 +36,6 @@ fn main() { os.system('git push origin gh-pages') os.chdir('..') } - time.wait(60 * time.second) + time.sleep(60 * time.second) } } diff --git a/cmd/tools/test_os_process.v b/cmd/tools/test_os_process.v index 44cd362a0..31de3e055 100644 --- a/cmd/tools/test_os_process.v +++ b/cmd/tools/test_os_process.v @@ -44,7 +44,7 @@ fn (mut ctx Context) println(s string) { fn do_timeout(c &Context) { mut ctx := c - time.wait(ctx.timeout_ms * time.millisecond) + time.sleep(ctx.timeout_ms * time.millisecond) exit(ctx.exitcode) } @@ -76,7 +76,7 @@ fn main() { go do_timeout(&ctx) for i := 1; true; i++ { ctx.println('$i') - time.wait(ctx.period_ms * time.millisecond) + time.sleep(ctx.period_ms * time.millisecond) } - time.wait(100 * time.second) + time.sleep(100 * time.second) } diff --git a/cmd/tools/vtest-parser.v b/cmd/tools/vtest-parser.v index a492a8813..b3d56e9b7 100644 --- a/cmd/tools/vtest-parser.v +++ b/cmd/tools/vtest-parser.v @@ -60,7 +60,7 @@ fn main() { source = source[..context.cut_index] go fn (ms int) { - time.wait(ms * time.millisecond) + time.sleep(ms * time.millisecond) exit(ecode_timeout) }(context.timeout_ms) _ := parser.parse_text(source, context.path, context.table, .skip_comments, context.pref, @@ -259,7 +259,7 @@ fn (mut context Context) start_printing() { fn (mut context Context) stop_printing() { context.stop_print = true - time.wait(time.millisecond * context.period_ms / 5) + time.sleep(time.millisecond * context.period_ms / 5) } fn (mut context Context) print_status() { @@ -284,7 +284,7 @@ fn (mut context Context) print_periodic_status() { for !context.stop_print { context.print_status() for i := 0; i < 10 && !context.stop_print; i++ { - time.wait(time.millisecond * context.period_ms / 10) + time.sleep(time.millisecond * context.period_ms / 10) if context.cut_index > 50 && !printed_at_least_once { context.print_status() printed_at_least_once = true diff --git a/examples/buf_reader.v b/examples/buf_reader.v index 8eca29afa..693a3ec02 100644 --- a/examples/buf_reader.v +++ b/examples/buf_reader.v @@ -14,6 +14,6 @@ fn main() { l := r.read_line() or { break } println('$l') // Make it nice and obvious that we are doing this line by line - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } } diff --git a/examples/concurrency/concurrency.v b/examples/concurrency/concurrency.v index e681fc7fd..92a5517e8 100644 --- a/examples/concurrency/concurrency.v +++ b/examples/concurrency/concurrency.v @@ -3,7 +3,7 @@ import time // Simulate expensive computing using sleep function fn expensive_computing(id int, duration int) { println('Executing expensive computing task ($id)...') - time.wait(duration * time.millisecond) + time.sleep(duration * time.millisecond) println('Finish task $id on $duration ms') } diff --git a/examples/flappylearning/game.v b/examples/flappylearning/game.v index a35265d71..5b1ef094d 100644 --- a/examples/flappylearning/game.v +++ b/examples/flappylearning/game.v @@ -206,7 +206,7 @@ fn main() { fn (mut app App) run() { for { app.update() - time.wait(app.timer_period_ms * time.millisecond) + time.sleep(app.timer_period_ms * time.millisecond) } } diff --git a/examples/game_of_life/life.v b/examples/game_of_life/life.v index c0958d5df..0f3b7c7ef 100644 --- a/examples/game_of_life/life.v +++ b/examples/game_of_life/life.v @@ -20,6 +20,6 @@ fn main() { for { a.update() print_automaton(a) - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } } diff --git a/examples/hot_reload/bounce.v b/examples/hot_reload/bounce.v index 9737bc54d..e9d17a0fa 100644 --- a/examples/hot_reload/bounce.v +++ b/examples/hot_reload/bounce.v @@ -80,6 +80,6 @@ fn (mut game Game) update_model() { fn (mut game Game) run() { for { game.update_model() - time.wait(16 * time.millisecond) // 60fps + time.sleep(16 * time.millisecond) // 60fps } } diff --git a/examples/hot_reload/message.v b/examples/hot_reload/message.v index 8f214013b..1f9ec2a32 100644 --- a/examples/hot_reload/message.v +++ b/examples/hot_reload/message.v @@ -13,6 +13,6 @@ fn print_message() { fn main() { for { print_message() - time.wait(500 * time.millisecond) + time.sleep(500 * time.millisecond) } } diff --git a/examples/lander.v b/examples/lander.v index 91dbd0b0a..4c8e0f42c 100644 --- a/examples/lander.v +++ b/examples/lander.v @@ -30,7 +30,7 @@ fn (l Lander) open_parachutes(n int) { fn wait() { println('waiting...') - time.wait(1 * time.second) + time.sleep(1 * time.second) } fn (l Lander) land(w World) { diff --git a/examples/sokol/sounds/simple_sin_tones.v b/examples/sokol/sounds/simple_sin_tones.v index 7cacc545b..cf545142a 100644 --- a/examples/sokol/sounds/simple_sin_tones.v +++ b/examples/sokol/sounds/simple_sin_tones.v @@ -37,6 +37,6 @@ fn main() { audio.setup( stream_cb: my_audio_stream_callback ) - time.wait(2000 * time.millisecond) + time.sleep(2000 * time.millisecond) audio.shutdown() } diff --git a/examples/sokol/sounds/wav_player.v b/examples/sokol/sounds/wav_player.v index 7bb748f8b..5ea90aec7 100644 --- a/examples/sokol/sounds/wav_player.v +++ b/examples/sokol/sounds/wav_player.v @@ -72,7 +72,7 @@ fn (mut p Player) play_wav_file(fpath string) ? { p.samples << samples p.finished = false for !p.finished { - time.wait(16 * time.millisecond) + time.sleep(16 * time.millisecond) } p.free() } diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index abfacec9a..9c3775eed 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -218,7 +218,7 @@ fn (mut g Game) run() { g.delete_completed_lines() } // glfw.post_empty_event() // force window redraw - time.wait(timer_period * time.millisecond) + time.sleep(timer_period * time.millisecond) } } diff --git a/examples/vweb/server_sent_events/server.v b/examples/vweb/server_sent_events/server.v index 2a9083ba3..3fe1be1e8 100644 --- a/examples/vweb/server_sent_events/server.v +++ b/examples/vweb/server_sent_events/server.v @@ -32,7 +32,7 @@ fn (mut app App) sse() vweb.Result { data := '{"time": "$time.now().str()", "random_id": "$rand.ulid()"}' session.send_message(event: 'ping', data: data) or { return app.server_error(501) } println('> sent event: $data') - time.wait(1 * time.second) + time.sleep(1 * time.second) } return app.server_error(501) } diff --git a/examples/websocket/ping.v b/examples/websocket/ping.v index ba7a55486..a68fa73c5 100644 --- a/examples/websocket/ping.v +++ b/examples/websocket/ping.v @@ -7,7 +7,7 @@ import x.websocket fn main() { println('press enter to quit...\n') go start_server() - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) go start_client() os.get_line() } @@ -76,7 +76,7 @@ fn write_echo(mut ws websocket.Client) ? { for i := 0; i <= 10; i++ { // Server will send pings every 30 seconds ws.write_str(message) or { println('panicing writing $err') } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } ws.close(1000, 'normal') or { println('panicing $err') } } diff --git a/vlib/clipboard/clipboard_windows.c.v b/vlib/clipboard/clipboard_windows.c.v index bc43507ca..35a17423c 100644 --- a/vlib/clipboard/clipboard_windows.c.v +++ b/vlib/clipboard/clipboard_windows.c.v @@ -73,7 +73,7 @@ fn (cb &Clipboard) get_clipboard_lock() bool { } else if last_error != u32(C.ERROR_ACCESS_DENIED) { return false } - time.wait(cb.retry_delay * time.second) + time.sleep(cb.retry_delay * time.second) } C.SetLastError(last_error) return false diff --git a/vlib/clipboard/x11/clipboard.c.v b/vlib/clipboard/x11/clipboard.c.v index 1a2b94c9e..c8a439d0a 100644 --- a/vlib/clipboard/x11/clipboard.c.v +++ b/vlib/clipboard/x11/clipboard.c.v @@ -239,7 +239,7 @@ pub fn (mut cb Clipboard) set_text(text string) bool { C.XFlush(cb.display) cb.mutex.unlock() // sleep a little bit - time.wait(1 * time.millisecond) + time.sleep(1 * time.millisecond) return cb.is_owner } @@ -262,7 +262,7 @@ fn (mut cb Clipboard) get_text() string { if cb.got_text || retries == 0 { break } - time.wait(50 * time.millisecond) + time.sleep(50 * time.millisecond) retries-- } return cb.text diff --git a/vlib/os/process_test.v b/vlib/os/process_test.v index e49308288..50efeb326 100644 --- a/vlib/os/process_test.v +++ b/vlib/os/process_test.v @@ -39,7 +39,7 @@ fn test_run() { $if trace_process_output ? { os.system('ps -opid= -oppid= -ouser= -onice= -of= -ovsz= -orss= -otime= -oargs= -p $p.pid') } - time.wait(50 * time.millisecond) + time.sleep(50 * time.millisecond) i++ } p.wait() diff --git a/vlib/rand/random_identifiers_test.v b/vlib/rand/random_identifiers_test.v index 39f4b27ed..3daee4d46 100644 --- a/vlib/rand/random_identifiers_test.v +++ b/vlib/rand/random_identifiers_test.v @@ -53,9 +53,9 @@ fn test_ulids_generated_in_the_same_millisecond_have_the_same_prefix() { fn test_ulids_should_be_lexicographically_ordered_when_not_in_same_millisecond() { ulid1 := rand.ulid() - time.wait(1 * time.millisecond) + time.sleep(1 * time.millisecond) ulid2 := rand.ulid() - time.wait(1 * time.millisecond) + time.sleep(1 * time.millisecond) ulid3 := rand.ulid() mut all := [ulid3, ulid2, ulid1] // eprintln('all before: $all') diff --git a/vlib/sync/channel_select_2_test.v b/vlib/sync/channel_select_2_test.v index d30fb33af..189aaf6be 100644 --- a/vlib/sync/channel_select_2_test.v +++ b/vlib/sync/channel_select_2_test.v @@ -58,5 +58,5 @@ fn test_select() { // the 3rd contribution is `byte` and must be seen modulo 256 expected_sum := 2 * (300 * (300 - 1) / 2) + 256 * (256 - 1) / 2 + 44 * (44 - 1) / 2 assert sum == expected_sum - time.wait(20 * time.millisecond) // to give assert in coroutine enough time + time.sleep(20 * time.millisecond) // to give assert in coroutine enough time } diff --git a/vlib/sync/pool/pool_test.v b/vlib/sync/pool/pool_test.v index 3aebf1a9a..629b5244b 100644 --- a/vlib/sync/pool/pool_test.v +++ b/vlib/sync/pool/pool_test.v @@ -12,14 +12,14 @@ pub struct IResult { fn worker_s(p &pool.PoolProcessor, idx int, worker_id int) &SResult { item := p.get_item(idx) println('worker_s worker_id: $worker_id | idx: $idx | item: $item') - time.wait(3 * time.millisecond) + time.sleep(3 * time.millisecond) return &SResult{'$item $item'} } fn worker_i(p &pool.PoolProcessor, idx int, worker_id int) &IResult { item := p.get_item(idx) println('worker_i worker_id: $worker_id | idx: $idx | item: $item') - time.wait(5 * time.millisecond) + time.sleep(5 * time.millisecond) return &IResult{item * 1000} } diff --git a/vlib/term/ui/input_windows.c.v b/vlib/term/ui/input_windows.c.v index 406ca85ae..87ecf6cef 100644 --- a/vlib/term/ui/input_windows.c.v +++ b/vlib/term/ui/input_windows.c.v @@ -107,7 +107,7 @@ pub fn (mut ctx Context) run() ? { init_called = true } if sleep_len > 0 { - time.wait(sleep_len * time.microsecond) + time.sleep(sleep_len * time.microsecond) } if !ctx.paused { sw.restart() diff --git a/vlib/term/ui/termios_nix.c.v b/vlib/term/ui/termios_nix.c.v index e324313b4..b2c0357e0 100644 --- a/vlib/term/ui/termios_nix.c.v +++ b/vlib/term/ui/termios_nix.c.v @@ -223,7 +223,7 @@ fn (mut ctx Context) termios_loop() { } // println('SLEEPING: $sleep_len') if sleep_len > 0 { - time.wait(sleep_len * time.microsecond) + time.sleep(sleep_len * time.microsecond) } if !ctx.paused { sw.restart() diff --git a/vlib/time/stopwatch_test.v b/vlib/time/stopwatch_test.v index f0eb926e3..16eadbabf 100644 --- a/vlib/time/stopwatch_test.v +++ b/vlib/time/stopwatch_test.v @@ -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 ? { diff --git a/vlib/time/time.v b/vlib/time/time.v index adddf54e3..b79c9a695 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -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) } diff --git a/vlib/time/time_nix.c.v b/vlib/time/time_nix.c.v index 76e285593..47a17f60d 100644 --- a/vlib/time/time_nix.c.v +++ b/vlib/time/time_nix.c.v @@ -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) +} diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index a0608b328..a498dab67 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -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() diff --git a/vlib/v/live/executable/reloader.v b/vlib/v/live/executable/reloader.v index b04d4fc3e..b18609495 100644 --- a/vlib/v/live/executable/reloader.v +++ b/vlib/v/live/executable/reloader.v @@ -160,7 +160,7 @@ fn reloader(mut r live.LiveReloadInfo) { } } if r.recheck_period_ms > 0 { - time.wait(r.recheck_period_ms * time.millisecond) + time.sleep(r.recheck_period_ms * time.millisecond) } } } diff --git a/vlib/v/live/live_test.v b/vlib/v/live/live_test.v index acca77db4..816d44ee0 100644 --- a/vlib/v/live/live_test.v +++ b/vlib/v/live/live_test.v @@ -110,14 +110,14 @@ fn testsuite_end() { } fn change_source(new string) { - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) vprintln('> change ORIGINAL to: $new') atomic_write_source(live_program_source.replace('ORIGINAL', new)) wait_for_file(new) } fn wait_for_file(new string) { - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) expected_file := os.join_path(os.temp_dir(), new + '.txt') eprintln('waiting for $expected_file ...') max_wait_cycles := edefault('WAIT_CYCLES', '1').int() @@ -128,10 +128,10 @@ fn wait_for_file(new string) { if os.exists(expected_file) { assert true vprintln('> done.') - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) break } - time.wait(5 * time.millisecond) + time.sleep(5 * time.millisecond) } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 0fa59f425..709644f25 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -318,7 +318,7 @@ pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, g for _ in 0 .. nr_cpus - 1 { go q.run() } - time.wait(time.second) + time.sleep(time.second) println('all done') return q.parsed_ast_files } diff --git a/vlib/v/tests/autolock_array1_test.v b/vlib/v/tests/autolock_array1_test.v index bc10956ec..b4c337fea 100644 --- a/vlib/v/tests/autolock_array1_test.v +++ b/vlib/v/tests/autolock_array1_test.v @@ -30,7 +30,7 @@ fn test_autolocked_array() { if finished_threads == 2 { break } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } // create histogram of results mut result := [0, 0, 0, 0] diff --git a/vlib/v/tests/go_wait_2_test.v b/vlib/v/tests/go_wait_2_test.v index fd909d9ab..b27477325 100644 --- a/vlib/v/tests/go_wait_2_test.v +++ b/vlib/v/tests/go_wait_2_test.v @@ -6,7 +6,7 @@ mut: } fn f(x int, y f64, shared s St) { - time.wait(50 * time.millisecond) + time.sleep(50 * time.millisecond) lock s { s.x = x * y } diff --git a/vlib/v/tests/shared_array_test.v b/vlib/v/tests/shared_array_test.v index 5ba6fc0ce..453396c0b 100644 --- a/vlib/v/tests/shared_array_test.v +++ b/vlib/v/tests/shared_array_test.v @@ -31,7 +31,7 @@ fn test_shared_array() { if finished_threads == 4 { break } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } rlock foo { f0 := foo[0] diff --git a/vlib/v/tests/shared_lock_2_test.v b/vlib/v/tests/shared_lock_2_test.v index b2d9f66b4..5070d1b93 100644 --- a/vlib/v/tests/shared_lock_2_test.v +++ b/vlib/v/tests/shared_lock_2_test.v @@ -44,7 +44,7 @@ fn test_shared_receiver_lock() { if finished { break } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } rlock x, y { assert x.a == 7 && y.a == 5 diff --git a/vlib/v/tests/shared_lock_3_test.v b/vlib/v/tests/shared_lock_3_test.v index 5d1f70fa1..b5ca960be 100644 --- a/vlib/v/tests/shared_lock_3_test.v +++ b/vlib/v/tests/shared_lock_3_test.v @@ -8,7 +8,7 @@ mut: fn f(shared x St, shared z St) { for _ in 0 .. reads_per_thread { rlock x { // other instances may read at the same time - time.wait(time.millisecond) + time.sleep(time.millisecond) assert x.a == 7 || x.a == 5 } } @@ -37,10 +37,10 @@ fn test_shared_lock() { for i in 0 .. writes { lock x { // wait for ongoing reads to finish, don't start new ones x.a = 17 // this should never be read - time.wait(50 * time.millisecond) + time.sleep(50 * time.millisecond) x.a = if (i & 1) == 0 { 7 } else { 5 } } // now new reads are possible again - time.wait(20 * time.millisecond) + time.sleep(20 * time.millisecond) } // wait until all read threads are finished for finished := false; true; { @@ -52,6 +52,6 @@ fn test_shared_lock() { if finished { break } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } } diff --git a/vlib/v/tests/shared_lock_4_test.v b/vlib/v/tests/shared_lock_4_test.v index ca0c4c736..bfe1e333d 100644 --- a/vlib/v/tests/shared_lock_4_test.v +++ b/vlib/v/tests/shared_lock_4_test.v @@ -8,7 +8,7 @@ mut: fn (shared x St) f(shared z St) { for _ in 0 .. reads_per_thread { rlock x { // other instances may read at the same time - time.wait(time.millisecond) + time.sleep(time.millisecond) assert x.a == 7 || x.a == 5 } } @@ -37,10 +37,10 @@ fn test_shared_lock() { for i in 0 .. writes { lock x { // wait for ongoing reads to finish, don't start new ones x.a = 17 // this value should never be read - time.wait(50 * time.millisecond) + time.sleep(50 * time.millisecond) x.a = if (i & 1) == 0 { 7 } else { 5 } } // now new reads are possible again - time.wait(20 * time.millisecond) + time.sleep(20 * time.millisecond) } // wait until all read threads are finished for finished := false; true; { @@ -52,6 +52,6 @@ fn test_shared_lock() { if finished { break } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } } diff --git a/vlib/v/tests/shared_lock_test.v b/vlib/v/tests/shared_lock_test.v index 0293fe148..84177e5b0 100644 --- a/vlib/v/tests/shared_lock_test.v +++ b/vlib/v/tests/shared_lock_test.v @@ -44,7 +44,7 @@ fn test_shared_lock() { if finished { break } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } rlock x, y { assert x.a == 7 && y.a == 5 diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 8ed09ed01..28d1c11a1 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -511,7 +511,7 @@ pub fn prepare_tool_when_needed(source_name string) { stool := os.join_path(vroot, 'cmd', 'tools', source_name) tool_name, tool_exe := tool_source2name_and_exe(stool) if should_recompile_tool(vexe, stool, tool_name, tool_exe) { - time.wait(1001 * time.millisecond) // TODO: remove this when we can get mtime with a better resolution + time.sleep(1001 * time.millisecond) // TODO: remove this when we can get mtime with a better resolution recompile_file(vexe, stool) } } diff --git a/vlib/vweb/tests/vweb_test.v b/vlib/vweb/tests/vweb_test.v index 439a4fa54..6fc4c5e6d 100644 --- a/vlib/vweb/tests/vweb_test.v +++ b/vlib/vweb/tests/vweb_test.v @@ -50,7 +50,7 @@ fn test_a_simple_vweb_app_runs_in_the_background() { res := os.system(server_exec_cmd) assert res == 0 } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } // web client tests follow @@ -238,7 +238,7 @@ fn simple_tcp_client(config SimpleTcpClientConfig) ?string { if tries > config.retries { return error(err) } - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) continue } break diff --git a/vlib/vweb/tests/vweb_test_server.v b/vlib/vweb/tests/vweb_test_server.v index 84aeb4c11..c3f04dc64 100644 --- a/vlib/vweb/tests/vweb_test_server.v +++ b/vlib/vweb/tests/vweb_test_server.v @@ -15,7 +15,7 @@ struct App { } fn exit_after_timeout(timeout_in_ms int) { - time.wait(timeout_in_ms * time.millisecond) + time.sleep(timeout_in_ms * time.millisecond) // eprintln('webserver is exiting ...') exit(0) } @@ -105,6 +105,6 @@ pub fn (mut app App) shutdown() vweb.Result { fn (mut app App) gracefull_exit() { eprintln('>> webserver: gracefull_exit') - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) exit(0) } diff --git a/vlib/x/websocket/websocket_server.v b/vlib/x/websocket/websocket_server.v index b67f9ee8c..3475d4ce8 100644 --- a/vlib/x/websocket/websocket_server.v +++ b/vlib/x/websocket/websocket_server.v @@ -72,7 +72,7 @@ fn (mut s Server) close() { fn (mut s Server) handle_ping() { mut clients_to_remove := []string{} for s.state == .open { - time.wait(s.ping_interval * time.second) + time.sleep(s.ping_interval * time.second) for i, _ in s.clients { mut c := s.clients[i] if c.client.state == .open { diff --git a/vlib/x/websocket/websocket_test.v b/vlib/x/websocket/websocket_test.v index 0bff4c8b3..7e222f8e5 100644 --- a/vlib/x/websocket/websocket_test.v +++ b/vlib/x/websocket/websocket_test.v @@ -20,7 +20,7 @@ fn test_ws() { } port := 30000 + rand.intn(1024) go start_server(port) - time.wait(500 * time.millisecond) + time.sleep(500 * time.millisecond) ws_test('ws://localhost:$port') or { assert false } } @@ -92,10 +92,10 @@ fn ws_test(uri string) ? { for msg in text { ws.write(msg.bytes(), .text_frame) or { panic('fail to write to websocket') } // sleep to give time to recieve response before send a new one - time.wait(100 * time.millisecond) + time.sleep(100 * time.millisecond) } // sleep to give time to recieve response before asserts - time.wait(1500 * time.millisecond) + time.sleep(1500 * time.millisecond) // We expect at least 2 pongs, one sent directly and one indirectly assert test_results.nr_pong_received >= 2 assert test_results.nr_messages == 2 -- 2.30.2