1
0
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:
Alexander Medvednikov 2021-02-27 20:41:06 +03:00
parent be4a2e17d3
commit 3a2d696fac
42 changed files with 76 additions and 67 deletions

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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')
}

View File

@ -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)
}
}

View File

@ -20,6 +20,6 @@ fn main() {
for {
a.update()
print_automaton(a)
time.wait(100 * time.millisecond)
time.sleep(100 * time.millisecond)
}
}

View File

@ -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
}
}

View File

@ -13,6 +13,6 @@ fn print_message() {
fn main() {
for {
print_message()
time.wait(500 * time.millisecond)
time.sleep(500 * time.millisecond)
}
}

View File

@ -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) {

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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') }
}

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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')

View File

@ -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
}

View File

@ -12,14 +12,14 @@ pub struct IResult {
fn worker_s(p &pool.PoolProcessor, idx int, worker_id int) &SResult {
item := p.get_item<string>(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<int>(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}
}

View File

@ -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()

View File

@ -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()

View File

@ -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 ? {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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]

View File

@ -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
}

View File

@ -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]

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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

View File

@ -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)
}

View File

@ -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 {

View File

@ -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