1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fix cmd/tools/vtest.v build

This PR enables building them again with v2, by making vlib/sync/pool.v
single threaded for now, and by removing the use of generics till they
are fixed in v2.
This commit is contained in:
Delyan Angelov
2020-04-02 16:52:23 +03:00
committed by GitHub
parent 4ada412a05
commit ad9848d983
2 changed files with 89 additions and 38 deletions

View File

@ -1,10 +1,5 @@
import sync
import time
import rand
struct SResult {
s string
}
fn worker_s(p &sync.PoolProcessor, idx int, worker_id int) voidptr {
// TODO: this works, but confuses vfmt. It should be used instead of
@ -12,12 +7,8 @@ fn worker_s(p &sync.PoolProcessor, idx int, worker_id int) voidptr {
// item := p.get_item<string>(idx)
item := p.get_string_item(idx)
println('worker_s worker_id: $worker_id | idx: $idx | item: ${item}')
time.sleep_ms(rand.next(3))
return &SResult{item + item}
}
struct IResult {
i int
time.sleep_ms(3)
return voidptr( &sync.SResult{ '${item} ${item}' } )
}
fn worker_i(p &sync.PoolProcessor, idx int, worker_id int) voidptr {
@ -25,33 +16,47 @@ fn worker_i(p &sync.PoolProcessor, idx int, worker_id int) voidptr {
// item := p.get_item<int>(idx)
item := p.get_int_item(idx)
println('worker_i worker_id: $worker_id | idx: $idx | item: ${item}')
time.sleep_ms(rand.next(5))
return &IResult{item * 1000}
time.sleep_ms(5)
return voidptr( &sync.IResult{ item * 1000 } )
}
fn test_work_on_strings() {
rand.seed(0)
mut pool_s := sync.new_pool_processor({
callback: worker_s
maxjobs: 8
})
pool_s.work_on_items(['a','b','c','d','e','f','g','h','i','j'])
for x in pool_s.get_results<SResult>() {
// TODO: uncomment this when generics work again
//pool_s.work_on_items(['a','b','c','d','e','f','g','h','i','j'])
//for x in pool_s.get_results<SResult>() {
// println( x.s )
// assert x.s.len > 1
//}
pool_s.work_on_items_s(['a','b','c','d','e','f','g','h','i','j'])
for x in pool_s.get_results_s() {
println( x.s )
assert x.s.len > 1
}
}
fn test_work_on_ints() {
rand.seed(0)
// NB: since maxjobs is left empty here,
// the pool processor will use njobs = runtime.nr_jobs so that
// it will work optimally without overloading the system
mut pool_i := sync.new_pool_processor({
callback: worker_i
})
pool_i.work_on_items([1,2,3,4,5,6,7,8])
for x in pool_i.get_results<IResult>() {
// TODO: uncomment this when generics work again
//pool_i.work_on_items([1,2,3,4,5,6,7,8])
//for x in pool_i.get_results<IResult>() {
// println( x.i )
// assert x.i > 100
//}
pool_i.work_on_items_i([1,2,3,4,5,6,7,8])
for x in pool_i.get_results_i() {
println( x.i )
assert x.i > 100
}