mirror of
https://github.com/logos-messaging/logos-messaging-go-bindings.git
synced 2026-08-25 09:51:16 +00:00
* feat(messaging): object-oriented Go mirror of the Nim MessagingClient
Adds pkg/messaging: a high-level, idiomatic Go binding for the Messaging
API, mirroring logos-delivery's Nim MessagingClient. A MessagingClient owns
a node and carries the messaging surface as methods on it — New / Start /
Stop / Close, Subscribe / Unsubscribe, Send(ctx, Envelope) (RequestID,
error) — over internal/ffi rather than exposing the raw FFI.
Events arrive on a single Events() <-chan Event with a sealed Event
interface: MessageReceivedEvent, MessageSentEvent, MessagePropagatedEvent,
MessageErrorEvent and ConnectionStatusEvent. Delivery never blocks the
library's event thread; an event is dropped when a consumer falls behind.
Config marshals to the layered configuration JSON (mode / preset /
messagingOverrides / channelsOverrides), with every field omitempty so it
can never be mistaken for the legacy flat blob.
Migrating internal/ffi to the current C ABI comes with it, because the
generated surface has moved on since the bridge was written and no longer
compiles: nim-ffi now generates the header from the {.ffi.} annotations,
argument-taking calls pass a per-call <Name>Req struct and a typed
<Name>ReplyFn, no-argument calls take a raw scalar callback, destroy is
synchronous, and the single set_event_callback has been replaced by a
per-event listener registry. The bridge now also copies every callback
string while it is still borrowed, and ignores the non-terminal
STALE_WARN progress code instead of settling the call on it.
pkg/kernel follows the same listener change, registering the three kernel
events it already consumed.
Verified against a liblogosdelivery built from logos-delivery master:
build / vet / golangci-lint / go mod tidy clean, unit tests green, and the
tagged integration test does a full create-start-subscribe-send round trip
on logos.dev, observing the message back and its propagation confirmation.
Closes #119.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CnzdnSMtHM5aLHLtDGBGR9
* review: address feedback on the MessagingClient API
- Send takes contentTopic / payload / ephemeral directly instead of an
Envelope struct, which drops the Envelope type; ContentTopic and RequestID
move to types.go.
- messagingEvents becomes a function returning the slice, so the set cannot
be mutated by accident.
- Document what seals the Event interface and what that buys callers.
- Document why the received payload is decoded from a JSON integer array:
base64 is only used on the send path and by the channel events, not by the
messaging events.
* ci: stop golangci-lint's config verify from failing on a network timeout
golangci-lint-action runs `golangci-lint config verify` before linting, which
fetches the v2.4 JSON schema from golangci-lint.run on every run. That request
timed out on the runner and failed the gate with no lint finding behind it. An
invalid config still fails the lint run itself, so the pre-check only costs a
network dependency.
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
//go:build integration
|
|
|
|
// Integration coverage for the Messaging API against a real network. It needs
|
|
// a built liblogosdelivery and outbound connectivity, so it is behind a build
|
|
// tag and is not part of the PR gate:
|
|
//
|
|
// go test -tags integration -v ./pkg/messaging/...
|
|
package messaging
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestSendReceiveRoundTrip drives the full client surface against the Logos Dev
|
|
// network: create, start, subscribe, send, and observe the resulting events.
|
|
// A node relays its own published messages back to itself, so the message sent
|
|
// here is also the one received.
|
|
func TestSendReceiveRoundTrip(t *testing.T) {
|
|
contentTopic := "/logos-delivery-go-bindings/1/it/proto"
|
|
payload := []byte(fmt.Sprintf("round trip %d", time.Now().UnixNano()))
|
|
|
|
client, err := New(Config{
|
|
Mode: ModeCore,
|
|
Preset: PresetLogosDev,
|
|
MessagingOverrides: Overrides{
|
|
"listen-address": "0.0.0.0",
|
|
"tcp-port": 60123,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := client.Close(); err != nil {
|
|
t.Errorf("Close: %v", err)
|
|
}
|
|
})
|
|
|
|
if err := client.Start(); err != nil {
|
|
t.Fatalf("Start: %v", err)
|
|
}
|
|
if err := client.Subscribe(contentTopic); err != nil {
|
|
t.Fatalf("Subscribe: %v", err)
|
|
}
|
|
|
|
// Wait for the node to reach the network before publishing.
|
|
waitFor(t, client, 60*time.Second, func(ev Event) bool {
|
|
e, ok := ev.(ConnectionStatusEvent)
|
|
return ok && e.Status == Connected
|
|
}, "connected")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
requestID, err := client.Send(ctx, contentTopic, payload, false)
|
|
if err != nil {
|
|
t.Fatalf("Send: %v", err)
|
|
}
|
|
if requestID == "" {
|
|
t.Fatal("Send returned an empty request id")
|
|
}
|
|
|
|
waitFor(t, client, 60*time.Second, func(ev Event) bool {
|
|
e, ok := ev.(MessageReceivedEvent)
|
|
return ok && e.Message.ContentTopic == contentTopic && bytes.Equal(e.Message.Payload, payload)
|
|
}, "the published message back on Events()")
|
|
|
|
waitFor(t, client, 60*time.Second, func(ev Event) bool {
|
|
e, ok := ev.(MessagePropagatedEvent)
|
|
return ok && e.RequestID == requestID
|
|
}, "a propagation confirmation for the sent request id")
|
|
}
|
|
|
|
// waitFor drains the event stream until match accepts an event or time runs out.
|
|
func waitFor(t *testing.T, c *MessagingClient, timeout time.Duration, match func(Event) bool, what string) {
|
|
t.Helper()
|
|
deadline := time.After(timeout)
|
|
for {
|
|
select {
|
|
case ev, ok := <-c.Events():
|
|
if !ok {
|
|
t.Fatalf("events channel closed while waiting for %s", what)
|
|
}
|
|
t.Logf("event: %#v", ev)
|
|
if match(ev) {
|
|
return
|
|
}
|
|
case <-deadline:
|
|
t.Fatalf("timed out after %s waiting for %s", timeout, what)
|
|
}
|
|
}
|
|
}
|