dymokomi/luce-prism
Source of release 0.6.0
## Owner listen + Store.connect. The client never opens the journal.
from luce_prism import prism
import interop
import memory
var db_path: str
var socket_path: str
func ipc_db() -> str:
return db_path
func ipc_sock() -> str:
return socket_path
func run_owner(dbpath: str) -> !:
let store = try prism.Store.open(dbpath, token = "secret")
defer store.release()
try store.value().listen()
print("READY")
try store.value().serve()
func run_client() -> !:
var denied = false
let rejected: interop.Reference[prism.Store]? = prism.Store.connect(ipc_sock(), "wrong") catch failure:
assert(failure.code == prism.invalid)
denied = true
recover none
if let extra = rejected:
extra.release()
assert(false)
assert(denied)
let store = try prism.Store.connect(ipc_sock(), "secret")
defer store.release()
let tx = try store.value().begin()
defer tx.release()
assert(not try tx.value().contains("/notes"))
try tx.value().add("/notes", "file")
assert(try tx.value().contains("/notes"))
let text = try prism.Value.text("hello")
defer text.release()
try tx.value().set("/notes", "text", text)
_ = try tx.value().commit()
_ = try store.value().bake()
let snap = try store.value().snapshot()
defer snap.release()
assert(try snap.value().contains("/notes"))
assert(not try snap.value().contains("/missing"))
let got = try snap.value().get("/notes", "text")
defer got.release()
assert(try got.value().text_at() == "hello")
# A remote failure's message outlives the reply that carried it.
var reported = false
let absent: interop.Reference[prism.Value]? = snap.value().get("/notes", "absent") catch failure:
assert(failure.code == prism.missing)
# Reuse freed memory of every small size class first, as the next request on a
# busy server would, before reading the message.
var intact = true
var size: usize = 16
while size <= 4096:
let scrub = try new u8[size] --- in memory.heap
for i in 0..<scrub.length: scrub[i] = '?'
if failure.message != "Prism property does not exist": intact = false
free(scrub) in memory.heap
size *= 2
assert(intact)
reported = true
recover none
if let extra = absent:
extra.release()
assert(false)
assert(reported)
let kids = try snap.value().children("/")
defer kids.release()
assert(kids.value().count() == 1)
let look = try store.value().lookup("/notes")
defer look.release()
assert(look.value().path() == "/notes")
assert(look.value().kind() == prism.LookKind.file)
var collided = false
let second: interop.Reference[prism.Store]? = prism.Store.open(ipc_db()) catch failure:
assert(failure.code == prism.busy)
collided = true
recover none
if let extra = second:
extra.release()
assert(false)
assert(collided)
pub func main(arguments: str[]) -> i32!:
assert(arguments.length == 4)
db_path = arguments[2]
socket_path = arguments[3]
if arguments[1] == "owner":
try run_owner(db_path)
else:
assert(arguments[1] == "client")
try run_client()
print("PASS ipc: owner listen, client begin/set/commit/lookup, second open fails the process lock")
return 0