Commit one move with request
Rosie sends her king’s move from 10 to 7 on checkers/move. The worker calls the move mutation from the board lessons. It publishes the new landing and replies only after checking whether the mutation committed. Rosie repeats the request. Square 10 is now empty and it is Bob’s turn, so the worker replies false.
Go example
func request(t *testing.T, ctx context.Context, db *stardust.Database) {
worker := must(stardust.Accept[choice](db, "request", "checkers/move"))
defer worker.Close()
board(db) // Wait for registration on the single request worker.
move := choice{10, 7}
for _, want := range []bool{true, false} {
type response struct {
committed bool
err error
}
replies := make(chan response, 1)
go func() {
ok, err := stardust.CallEvent[bool](db, "checkers/move", move)
replies <- response{ok, err}
}()
open := must(worker.Next(ctx))
input := open.Data
result := must(stardust.CallAs[stardust.MutationResult](db, table, "move", input.From, input.Landing))
committed := result.Transaction != nil
if committed {
must(stardust.Publish(db, "checkers/table/turn", input.Landing))
must(stardust.Publish(db, "checkers/table/animate", []int64{10, 7}))
}
check(worker.Send(open.Handle, committed))
select {
case reply := <-replies:
check(reply.err)
if reply.committed != want {
t.Fatal("unexpected move reply", reply)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
}