Files
netrunner/zaprpc/zaprpc_test.go
T
Hanzo AI 301a8e2601 rip gRPC, USE ZAP: netrunner control-plane now luxfi/zap-native (FORWARDS ONLY)
Per the project rule 'ZAP internal, ZIP edge': netrunner's 26-method
ControlService + PingService move off google.golang.org/grpc and
grpc-gateway onto a luxfi/zap envelope. Default builds now have ZERO
google.golang.org/grpc transitive deps.

New package: netrunner/zaprpc/
  - protocol.go: MsgType (uint8 range, encoded in ZAP flags upper byte).
    One stable wire ID per method. Append-only.
  - dispatch.go: typed Bind[Req, Resp](d, msg, handler) — register a
    'func(ctx, *Req) (*Resp, error)' handler. JSON encode/decode +
    envelope framing happen here, handlers stay business-logic-only.
  - server.go: thin wrapper around zap.Node hosting a Dispatcher.
  - client.go: typed Call[Req, Resp](ctx, c, msg, req) — same DX as the
    old gRPC client but transport is ZAP. Includes connect-retry around
    the boot race.

server/bind.go: one-shot registration of all 26 RPC methods onto a
Dispatcher. The canonical map of 'what this server exposes'.

server/server.go: gRPC server + grpc-gateway HTTP bridge are gone.
Run() now starts the ZAP server and waits for rootCtx.Done. The
'soldier-on' isClientCanceled helper goes with the gRPC stream code.

client/client.go: full rewrite. Same Client interface (28 methods),
implementation is now one-line zaprpc.Call per method. StreamStatus
becomes a client-driven Status() poll on the requested interval —
ZAP has no native server-streaming, but the observable contract
(channel of ClusterInfo) is unchanged.

Deleted:
  - rpcpb/rpc_grpc.pb.go (gRPC service stubs)
  - rpcpb/rpc.pb.gw.go (grpc-gateway HTTP bridge)

The rpcpb message types (rpc.pb.go) stay — they're plain Go structs
with json: tags, used as the JSON payload format inside ZAP envelopes.
A future cleanup pass can replace them with hand-written structs to
drop google.golang.org/protobuf from the dep tree too.

go.mod cleanup: drop github.com/grpc-ecosystem/grpc-gateway/v2,
google.golang.org/grpc, google.golang.org/genproto/googleapis/api.
Pin luxfi/proto v1.0.0 (was luxfi/protocol v0.0.4 — stale rename
artifact). Add luxfi/zap v0.2.0.

Tests: zaprpc/zaprpc_test.go round-trips request/response and proves
the error path propagates through the envelope.

No backwards compat — forwards perfection per the project rule.
2026-05-20 19:15:40 -07:00

120 lines
3.1 KiB
Go

// Copyright (C) 2021-2026, Lux Industries Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package zaprpc_test
import (
"context"
"fmt"
"net"
"testing"
"time"
"github.com/luxfi/netrunner/zaprpc"
)
// Echo is the smallest possible RPC for the round-trip test.
type EchoReq struct {
Message string `json:"message"`
}
type EchoResp struct {
Echo string `json:"echo"`
N int `json:"n"`
}
// TestRoundTrip wires a dispatcher with one bound method and proves the
// envelope+JSON+ZAP path round-trips end to end.
func TestRoundTrip(t *testing.T) {
// MsgTypes use the upper 8 bits of the ZAP flags field, so values must
// fit in uint8. Pick something outside the production range (1..26).
const echoMsg zaprpc.MsgType = 200
disp := zaprpc.NewDispatcher()
zaprpc.Bind[EchoReq, EchoResp](disp, echoMsg, func(_ context.Context, req *EchoReq) (*EchoResp, error) {
return &EchoResp{Echo: req.Message, N: len(req.Message)}, nil
})
port := freePort(t)
srv := zaprpc.NewServer(zaprpc.ServerConfig{
NodeID: "test-server",
Port: port,
}, disp)
if err := srv.Start(); err != nil {
t.Fatalf("server start: %v", err)
}
defer srv.Stop()
cli, err := zaprpc.NewClient(zaprpc.ClientConfig{
NodeID: "test-client",
ServerAddr: fmt.Sprintf("127.0.0.1:%d", port),
DialTimeout: 3 * time.Second,
})
if err != nil {
t.Fatalf("client connect: %v", err)
}
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
resp, err := zaprpc.Call[EchoReq, EchoResp](ctx, cli, echoMsg, &EchoReq{Message: "hello"})
if err != nil {
t.Fatalf("call: %v", err)
}
if resp.Echo != "hello" || resp.N != 5 {
t.Fatalf("unexpected response: %+v", resp)
}
}
// TestErrorPropagation exercises the errStr return path.
func TestErrorPropagation(t *testing.T) {
const failMsg zaprpc.MsgType = 201
disp := zaprpc.NewDispatcher()
zaprpc.Bind[EchoReq, EchoResp](disp, failMsg, func(_ context.Context, _ *EchoReq) (*EchoResp, error) {
return nil, fmt.Errorf("deliberate failure")
})
port := freePort(t)
srv := zaprpc.NewServer(zaprpc.ServerConfig{NodeID: "err-server", Port: port}, disp)
if err := srv.Start(); err != nil {
t.Fatalf("server start: %v", err)
}
defer srv.Stop()
cli, err := zaprpc.NewClient(zaprpc.ClientConfig{
NodeID: "err-client",
ServerAddr: fmt.Sprintf("127.0.0.1:%d", port),
DialTimeout: 3 * time.Second,
})
if err != nil {
t.Fatalf("client connect: %v", err)
}
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err = zaprpc.Call[EchoReq, EchoResp](ctx, cli, failMsg, &EchoReq{Message: "ignored"})
if err == nil {
t.Fatal("expected error, got nil")
}
if err.Error() != "deliberate failure" {
t.Fatalf("expected 'deliberate failure', got %q", err.Error())
}
}
func freePort(t *testing.T) int {
t.Helper()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
port := l.Addr().(*net.TCPAddr).Port
if err := l.Close(); err != nil {
t.Fatalf("close: %v", err)
}
return port
}