fed-sx-m1: Step 8c-post-publish-srv — gen_server-wrapped nx_kernel (start_link + publish/query/log_tip) + 11 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
-module(nx_kernel).
|
||||
-behaviour(gen_server).
|
||||
-export([new/3, publish/2,
|
||||
actor_id/1, log_state/1, log_tip/1,
|
||||
key_spec/1, actor_state/1, projections/1,
|
||||
next_published/1, with_projections/2]).
|
||||
-export([start_link/3, publish/1, query/0, log_tip/0,
|
||||
with_projections/1, stop/0]).
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
|
||||
|
||||
%% Kernel orchestrator — the long-lived runtime state held by the
|
||||
%% running fed-sx instance. The HTTP layer (Step 8c-post-publish
|
||||
@@ -80,3 +84,56 @@ field(_, []) -> nil.
|
||||
set(K, V, []) -> [{K, V}];
|
||||
set(K, V, [{K, _} | Rest]) -> [{K, V} | Rest];
|
||||
set(K, V, [P | Rest]) -> [P | set(K, V, Rest)].
|
||||
|
||||
%% ── gen_server wrapper ──────────────────────────────────────────
|
||||
%%
|
||||
%% Mirrors the registry / projection gen_server patterns from
|
||||
%% Steps 5b and 7b. Same port quirks: raw Pid return, no `?MODULE`
|
||||
%% macro, spawned processes don't persist across separate
|
||||
%% erlang-eval-ast calls — tests inline start_link with operations.
|
||||
|
||||
start_link(ActorId, KeySpec, ActorStateProplist) ->
|
||||
Pid = gen_server:start_link(nx_kernel,
|
||||
[ActorId, KeySpec, ActorStateProplist]),
|
||||
erlang:register(nx_kernel, Pid),
|
||||
Pid.
|
||||
|
||||
stop() ->
|
||||
R = gen_server:call(nx_kernel, '$gen_stop'),
|
||||
erlang:unregister(nx_kernel),
|
||||
R.
|
||||
|
||||
publish(Request) ->
|
||||
gen_server:call(nx_kernel, {publish, Request}).
|
||||
|
||||
query() ->
|
||||
gen_server:call(nx_kernel, get_state).
|
||||
|
||||
log_tip() ->
|
||||
gen_server:call(nx_kernel, get_log_tip).
|
||||
|
||||
with_projections(Names) ->
|
||||
gen_server:call(nx_kernel, {set_projections, Names}).
|
||||
|
||||
%% gen_server callbacks
|
||||
|
||||
init([ActorId, KeySpec, AS]) ->
|
||||
{ok, new(ActorId, KeySpec, AS)}.
|
||||
|
||||
handle_call({publish, Request}, _From, State) ->
|
||||
case publish(Request, State) of
|
||||
{ok, Result, NewState} ->
|
||||
{reply, {ok, Result}, NewState};
|
||||
{error, Reason, SameState} ->
|
||||
{reply, {error, Reason}, SameState}
|
||||
end;
|
||||
handle_call(get_state, _From, State) ->
|
||||
{reply, State, State};
|
||||
handle_call(get_log_tip, _From, State) ->
|
||||
{reply, log_tip(State), State};
|
||||
handle_call({set_projections, Names}, _From, State) ->
|
||||
{reply, ok, with_projections(Names, State)}.
|
||||
|
||||
handle_cast(_, S) -> {noreply, S}.
|
||||
|
||||
handle_info(_, S) -> {noreply, S}.
|
||||
|
||||
127
next/tests/nx_kernel_server.sh
Executable file
127
next/tests/nx_kernel_server.sh
Executable file
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env bash
|
||||
# next/tests/nx_kernel_server.sh — Step 8c-post-publish-srv tests.
|
||||
#
|
||||
# Exercises the gen_server-wrapped nx_kernel. Same port quirks
|
||||
# as registry/projection gen_servers: each test inlines start_link
|
||||
# with operations. 10 cases.
|
||||
|
||||
set -uo pipefail
|
||||
cd "$(git rev-parse --show-toplevel)"
|
||||
|
||||
SX_SERVER="${SX_SERVER:-hosts/ocaml/_build/default/bin/sx_server.exe}"
|
||||
if [ ! -x "$SX_SERVER" ]; then
|
||||
SX_SERVER="/root/rose-ash/hosts/ocaml/_build/default/bin/sx_server.exe"
|
||||
fi
|
||||
if [ ! -x "$SX_SERVER" ]; then
|
||||
echo "ERROR: sx_server.exe not found." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VERBOSE="${1:-}"
|
||||
PASS=0; FAIL=0; ERRORS=""
|
||||
TMPFILE=$(mktemp); trap "rm -f $TMPFILE" EXIT
|
||||
|
||||
# Shared prelude — KS/AS bindings + start_link + a Req binding.
|
||||
PRELUDE='KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], nx_kernel:start_link(alice, KS, AS), Req = [{type,create},{object,nil}],'
|
||||
|
||||
cat > "$TMPFILE" <<EPOCHS
|
||||
(epoch 1)
|
||||
(load "lib/erlang/tokenizer.sx")
|
||||
(load "lib/erlang/parser.sx")
|
||||
(load "lib/erlang/parser-core.sx")
|
||||
(load "lib/erlang/parser-expr.sx")
|
||||
(load "lib/erlang/parser-module.sx")
|
||||
(load "lib/erlang/transpile.sx")
|
||||
(load "lib/erlang/runtime.sx")
|
||||
(load "lib/erlang/vm/dispatcher.sx")
|
||||
(epoch 2)
|
||||
(eval "(er-load-gen-server!)")
|
||||
(epoch 3)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/envelope.erl\")) :name)")
|
||||
(epoch 4)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/log.erl\")) :name)")
|
||||
(epoch 5)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/pipeline.erl\")) :name)")
|
||||
(epoch 6)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/outbox.erl\")) :name)")
|
||||
(epoch 7)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/nx_kernel.erl\")) :name)")
|
||||
|
||||
;; start_link returns a Pid registered under nx_kernel
|
||||
(epoch 10)
|
||||
(eval "(get (erlang-eval-ast \"${PRELUDE} is_pid(whereis(nx_kernel))\") :name)")
|
||||
|
||||
;; log_tip starts at 0
|
||||
(epoch 11)
|
||||
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:log_tip()\")")
|
||||
|
||||
;; publish/1 happy path returns {ok, _}
|
||||
(epoch 12)
|
||||
(eval "(get (erlang-eval-ast \"${PRELUDE} case nx_kernel:publish(Req) of {ok, _} -> ok; _ -> bad end\") :name)")
|
||||
|
||||
;; After one publish, log_tip = 1
|
||||
(epoch 13)
|
||||
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:publish(Req), nx_kernel:log_tip()\")")
|
||||
|
||||
;; Two publishes -> log_tip = 2 (next_published counter avoids replay)
|
||||
(epoch 14)
|
||||
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:publish(Req), nx_kernel:publish(Req), nx_kernel:log_tip()\")")
|
||||
|
||||
;; query/0 returns a state proplist with the right actor_id
|
||||
(epoch 15)
|
||||
(eval "(get (erlang-eval-ast \"${PRELUDE} S = nx_kernel:query(), nx_kernel:actor_id(S) =:= alice\") :name)")
|
||||
|
||||
;; with_projections/1 sets the projection list, visible via query
|
||||
(epoch 16)
|
||||
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:with_projections([px]), S = nx_kernel:query(), nx_kernel:projections(S) =:= [px]\") :name)")
|
||||
|
||||
;; Bad key in state -> publish returns {error, bad_signature}; log_tip unchanged
|
||||
(epoch 17)
|
||||
(eval "(get (erlang-eval-ast \"OtherKM = <<9,9,9,9>>, KS = [{key_id,k1},{algorithm,ed25519},{value,OtherKM}], AS = [{public_keys,[[{id,k1},{created,0},{value,<<1,2,3,4>>}]]}], nx_kernel:start_link(alice, KS, AS), Req = [{type,create},{object,nil}], R = nx_kernel:publish(Req), Tip = nx_kernel:log_tip(), case {R, Tip} of {{error, bad_signature}, 0} -> ok; _ -> bad end\") :name)")
|
||||
|
||||
;; State persists across multiple gen_server calls in one expression
|
||||
(epoch 18)
|
||||
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(Req), Tip1 = nx_kernel:log_tip(), nx_kernel:publish(Req), Tip2 = nx_kernel:log_tip(), {Tip1, Tip2} =:= {1, 2}\") :name)")
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 240 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
|
||||
check() {
|
||||
local epoch="$1" desc="$2" expected="$3"
|
||||
local actual
|
||||
actual=$(echo "$OUTPUT" | awk -v e="$epoch" '
|
||||
$0 ~ "^\\(ok-len " e " " { getline; print; exit }
|
||||
$0 ~ "^\\(ok " e " " { print; exit }
|
||||
$0 ~ "^\\(error " e " " { print; exit }
|
||||
')
|
||||
[ -z "$actual" ] && actual="<no output for epoch $epoch>"
|
||||
if echo "$actual" | grep -qF -- "$expected"; then
|
||||
PASS=$((PASS+1))
|
||||
[ "$VERBOSE" = "-v" ] && echo " ok $desc"
|
||||
else
|
||||
FAIL=$((FAIL+1))
|
||||
ERRORS+=" FAIL [$desc] (epoch $epoch) expected: $expected | actual: $actual
|
||||
"
|
||||
fi
|
||||
}
|
||||
|
||||
check 2 "gen_server loaded" "gen_server"
|
||||
check 7 "nx_kernel module loaded" "nx_kernel"
|
||||
check 10 "start_link registered Pid" "true"
|
||||
check 11 "fresh log_tip = 0" "0"
|
||||
check 12 "publish/1 happy path" "ok"
|
||||
check 13 "tip = 1 after one publish" "1"
|
||||
check 14 "tip = 2 after two publishes" "2"
|
||||
check 15 "query returns state w/ actor_id" "true"
|
||||
check 16 "with_projections persists" "true"
|
||||
check 17 "bad key fails, tip unchanged" "ok"
|
||||
check 18 "state persists across calls" "true"
|
||||
|
||||
TOTAL=$((PASS+FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL next/tests/nx_kernel_server.sh passed"
|
||||
else
|
||||
echo "FAIL $PASS/$TOTAL passed, $FAIL failed:"
|
||||
echo "$ERRORS"
|
||||
fi
|
||||
[ $FAIL -eq 0 ]
|
||||
Reference in New Issue
Block a user