Add simple chat test

This commit is contained in:
Emery Hemingway 2021-09-21 19:10:13 +02:00
parent b0c92ea5de
commit 2636fa8f0a
1 changed files with 50 additions and 0 deletions

50
tests/chat.nim Normal file
View File

@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, asyncfile, posix, random, strutils]
import preserves
import syndicate, syndicate/protocols/schemas/simpleChatProtocol, syndicate/sturdy
randomize()
syndicate chat:
let me = "user_" & $rand(range[10..1000])
spawn "debug":
onAsserted(?s) do (s: Preserve):
echo " asserted ", s
onRetracted(?s) do (s: Preserve):
echo " retracted ", s
onMessage(?s) do (s: Preserve):
echo " message ", s
spawn "log":
during(present(?who)) do (who: string):
echo who, " joined"
onStop:
echo who, " left"
onMessage(says(?who, ?what)) do (who: string; what: string):
echo who, " says ", what
spawn "chat":
asserting present(me)
during (present(me)):
let
inputFacet = getCurrentFacet()
af = newAsyncFile(AsyncFD STDIN_FILENO)
inputFacet.beginExternalTask()
proc readStdin() =
readline(af).addCallback do (f: Future[string]):
if f.failed:
inputFacet.endExternalTask()
else:
callSoon:
readStdin()
let line = read f
if line.len > 0:
let a = says(me, strip line)
send a
readStdin()
waitFor chat()