L
ling-wallet
↓ 0 downloadsLing Wallet: a post-quantum privacy-coin wallet, full node and HyperTorus miner for LNG
Keywords wallet, cryptocurrency, lng, node, miner, ling
lingfu install ling-walletCode / Files (116)
ling/tests/threads/t_basic.ling
# t_basic.ling — isolates + channels (LING_API.md §1).
# ling run --interp t_basic.ling
# One PASS/FAIL line per check, then a summary.
fn check(label, cond) {
if cond {
st_set("pass", st_get("pass", 0) + 1)
print(format("PASS {}", label))
} else {
st_set("fail", st_get("fail", 0) + 1)
print(format("FAIL {}", label))
}
return cond
}
fn is_ok(r) {
match r {
ok(v) => { return true }
bad(e) => { return false }
}
}
fn is_bad(r) {
match r {
ok(v) => { return false }
bad(e) => { return true }
}
}
fn val(r) {
match r {
ok(v) => { return v }
bad(e) => { return format("ERR:{}", e) }
}
}
fn err(r) {
match r {
ok(v) => { return "" }
bad(e) => { return e }
}
}
fn spawn(f, args, caps) { return val(thread_spawn("worker.ling", f, args, caps)) }
fn run(f, args, caps) { return thread_join(spawn(f, args, caps)) }
bind start = do {
st_set("pass", 0)
st_set("fail", 0)
print("-- spawn / join")
check("cpus >= 1", thread_cpus() >= 1)
check("main caps are all", len(thread_caps()) == 7)
check("number", val(run("add", [2, 3], [])) == 5)
check("string", val(run("echo", ["灵 ling"], [])) == "灵 ling")
bind l = val(run("echo", [[1, [2, "x"], true, ()]], []))
check("nested list", format("{}", l) == "[1, [2, x], true, ()]")
bind p = val(run("mkpoint", [3, 4], []))
check("struct", p.x == 3 && p.y == 4)
bind pin = val(run("echo", [p], []))
check("struct as arg", pin.y == 4)
check("child returns bad", err(val(run("ret_bad", [], []))) == "nope")
check("child returns ok", format("{}", val(run("ret_ok", [], []))) == "Ok([1, two])")
check("returning a fn is an error", is_bad(run("ret_fn", [], [])))
check("fn arg rejected", is_bad(thread_spawn("worker.ling", "echo", [|x| x], [])))
check("missing file", is_bad(thread_spawn("nope_nope.ling", "add", [], [])))
check("missing function", is_bad(run("no_such_fn", [], [])))
bind h = spawn("add", [1, 1], [])
check("join twice", val(thread_join(h)) == 2 && val(thread_join(h)) == 2)
check("not alive after join", !thread_alive(h))
bind ch = spawn("me", [], [])
check("thread_self in child is its handle", val(thread_join(ch)) == ch)
check("thread_self main differs", thread_self() != ch && len(thread_self()) == 32)
check("nested spawn", val(val(run("nested_add", [], ["fs"]))) == 42)
print("-- parallelism")
bind t0 = time_now()
bind hs = [spawn("busy", [400], []), spawn("busy", [400], []), spawn("busy", [400], []), spawn("busy", [400], [])]
for w in hs { thread_join(w) }
bind dt = (time_now() - t0) * 1000
print(format(" 4 x 400ms busy workers took {} ms", floor(dt)))
check("4 workers overlap (< 1200 ms)", dt < 1200)
bind t1 = time_now()
bind n = 0
while n < 20 {
thread_join(spawn("add", [1, 2], []))
bind n = n + 1
}
print(format(" spawn+join: {} ms each", floor((time_now() - t1) * 1000 / 20 * 100) / 100))
print("-- channels")
bind a = chan_new(1)
bind b = chan_new(1)
bind pg = spawn("pong", [list_get(a, 1), list_get(b, 0), 1000], [])
bind t2 = time_now()
bind k = 0
bind good = true
while k < 1000 {
chan_send(list_get(a, 0), k)
if val(chan_recv(list_get(b, 1))) != k + 1 { bind good = false }
bind k = k + 1
}
print(format(" ping-pong round trip: {} us", floor((time_now() - t2) * 1000000 / 1000)))
check("ping-pong 1000", good && val(thread_join(pg)) == 1000)
bind c = chan_new(2)
bind ctx = list_get(c, 0)
bind crx = list_get(c, 1)
check("try_send 1", is_ok(chan_try_send(ctx, 1)))
check("try_send 2", is_ok(chan_try_send(ctx, 2)))
check("try_send full", err(chan_try_send(ctx, 3)) == "full")
check("len 2", chan_len(ctx) == 2 && chan_len(crx) == 2)
check("try_recv", val(chan_try_recv(crx)) == 1)
check("fifo", val(chan_recv(crx)) == 2)
check("try_recv empty", err(chan_try_recv(crx)) == "empty")
bind t3 = time_now()
check("recv_timeout", err(chan_recv_timeout(crx, 50)) == "timeout")
check("timeout waited ~50ms", (time_now() - t3) * 1000 >= 45)
chan_send(ctx, "last")
check("close send side", chan_close(ctx))
check("close again false", !chan_close(ctx))
check("send after close", err(chan_send(ctx, 1)) == "closed")
check("drain after close", val(chan_recv(crx)) == "last")
check("recv closed", err(chan_recv(crx)) == "closed")
bind d = chan_new(4)
chan_send(list_get(d, 0), 1)
check("close recv side", chan_close(list_get(d, 1)))
check("send to closed receiver", err(chan_try_send(list_get(d, 0), 2)) == "closed")
bind pc = chan_new(8)
bind ph = spawn("producer", [list_get(pc, 0), 100], [])
bind sum = 0
bind going = true
while going {
bind r = chan_recv(list_get(pc, 1))
if is_ok(r) { bind sum = sum + val(r) } else { bind going = false }
}
check("producer/consumer + close", sum == 4950 && val(thread_join(ph)) == 100)
bind s1 = chan_new(4)
bind s2 = chan_new(4)
bind s3 = chan_new(4)
bind rxs = [list_get(s1, 1), list_get(s2, 1), list_get(s3, 1)]
chan_send(list_get(s3, 0), "three")
bind sel = val(chan_select(rxs, 100))
check("select picks ready channel", list_get(sel, 0) == 2 && list_get(sel, 1) == "three")
chan_send(list_get(s2, 0), "two")
check("select index 1", list_get(val(chan_select(rxs, 100)), 0) == 1)
check("select timeout", err(chan_select(rxs, 30)) == "timeout")
bind sw = chan_new(1)
bind swh = spawn("producer", [list_get(sw, 0), 1], [])
bind sel2 = val(chan_select([list_get(s1, 1), list_get(sw, 1)], 2000))
check("select wakes on child send", list_get(sel2, 0) == 1 && list_get(sel2, 1) == 0)
thread_join(swh)
chan_close(list_get(s1, 0))
chan_close(list_get(s2, 0))
chan_close(list_get(s3, 0))
check("select all closed", err(chan_select(rxs, 100)) == "closed")
check("capacity 0 rejected", is_bad(run("bad_cap", [], [])))
check("capacity 65537 rejected", is_bad(run("big_cap", [], [])))
bind sc = chan_new_secure(2)
bind sch = spawn("pong", [list_get(sc, 1), list_get(b, 0), 1], [])
chan_send(list_get(sc, 0), 41)
check("secure channel delivers", val(chan_recv(list_get(b, 1))) == 42 && val(thread_join(sch)) == 1)
chan_send(list_get(sc, 0), "secret-string")
check("secure channel string", val(chan_recv(list_get(sc, 1))) == "secret-string")
print("-- handles")
bind hc = chan_new(1)
check("handle is 32 hex", len(list_get(hc, 0)) == 32)
check("forged handle in child errors", is_bad(run("forge", ["0123456789abcdef0123456789abcdef"], [])))
check("handle passed as spawn arg works", is_ok(val(run("forge", [list_get(hc, 0)], []))))
check("value arrived via arg handle", val(chan_try_recv(list_get(hc, 1))) == 1)
bind m = chan_new(1)
bind reply = chan_new(1)
bind vm = spawn("via_msg", [list_get(m, 1)], [])
chan_send(list_get(m, 0), [list_get(reply, 0)])
check("handle sent in a message works", val(chan_recv(list_get(reply, 1))) == "hi" && val(thread_join(vm)) == "sent")
check("handle in shared store is plain text", is_ok(shared_put("t_basic_handle", list_get(hc, 0))))
check("child can't use handle read from shared store", is_bad(run("forge_shared", ["t_basic_handle"], [])))
print("-- capabilities")
check("child without fs can't write", err(run("try_write", [], [])) == "capability \"fs\" not granted")
check("child caps listed", format("{}", val(run("caps", [], ["db", "fs"]))) == "[db, fs]")
check("child w/o net can't grant net", err(val(run("try_net_grant", [], []))) == "capability \"net\" not granted")
check("child with fs can grant fs", is_ok(val(run("try_fs_grant", [], ["fs"]))))
check("unknown cap", is_bad(thread_spawn("worker.ling", "add", [1, 2], ["root"])))
check("gui in isolate fails", is_bad(run("gui", [], ["fs", "net"])))
print("-- failures")
check("runtime error -> bad", is_bad(run("boom", [], [])))
bind pe = err(run("do_panic", [], []))
print(format(" panic join: {}", pe))
check("panic -> bad(panic: ..)", pe == "panic: kaboom")
bind fv = spawn("forever", [], [])
check("alive while looping", thread_alive(fv))
check("timeout join", err(thread_join_timeout(fv, 50)) == "timeout")
check("cancel returns true", thread_cancel(fv))
check("cancel stops infinite loop", err(thread_join_timeout(fv, 2000)) == "cancelled")
bind sl = spawn("sleepy", [], [])
thread_cancel(sl)
check("cancel wakes sleep", err(thread_join_timeout(sl, 2000)) == "cancelled")
bind br = chan_new(1)
bind bh = spawn("blocked_recv", [list_get(br, 1)], [])
thread_cancel(bh)
check("cancel wakes chan_recv", err(thread_join_timeout(bh, 2000)) == "cancelled")
print("-- shared store")
check("shared_put", is_ok(shared_put("cfg", [1, "a"])))
check("shared_put write-once", err(shared_put("cfg", 2)) == "exists")
check("shared_get main", format("{}", shared_get("cfg", 0)) == "[1, a]")
check("shared_get child", format("{}", val(run("read_shared", ["cfg"], []))) == "[1, a]")
check("shared_get default", val(run("read_shared", ["nokey"], [])) == "missing")
print(format("-- {} passed, {} failed", st_get("pass", 0), st_get("fail", 0)))
}