mesh: implement UDP gossip transport; wire mesh comms into main
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Iea0b2f250b01df78b1c7be73d69d28c06a6a6964
This commit is contained in:
parent
91ffc0eadd
commit
d290bcf4ad
6 changed files with 226 additions and 5 deletions
54
internal/mesh/gossip_test.go
Normal file
54
internal/mesh/gossip_test.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package mesh_test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"notashelf.dev/ncro/internal/cache"
|
||||
"notashelf.dev/ncro/internal/mesh"
|
||||
)
|
||||
|
||||
func TestAnnounceAndReceive(t *testing.T) {
|
||||
store := mesh.NewRouteStore()
|
||||
node, err := mesh.NewNode("", store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Bind to an ephemeral port.
|
||||
conn, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := conn.LocalAddr().String()
|
||||
conn.Close()
|
||||
|
||||
if err := mesh.ListenAndServe(addr, store); err != nil {
|
||||
t.Fatalf("ListenAndServe: %v", err)
|
||||
}
|
||||
|
||||
routes := []cache.RouteEntry{
|
||||
{
|
||||
StorePath: "test-pkg-abc",
|
||||
UpstreamURL: "https://cache.nixos.org",
|
||||
LatencyEMA: 25,
|
||||
TTL: time.Now().Add(time.Hour),
|
||||
},
|
||||
}
|
||||
|
||||
if err := mesh.Announce(addr, node, routes); err != nil {
|
||||
t.Fatalf("Announce: %v", err)
|
||||
}
|
||||
|
||||
// Give the listener goroutine time to process the packet.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
entry := store.Get("test-pkg-abc")
|
||||
if entry == nil {
|
||||
t.Fatal("route not merged into store after announce")
|
||||
}
|
||||
if entry.UpstreamURL != "https://cache.nixos.org" {
|
||||
t.Errorf("UpstreamURL = %q", entry.UpstreamURL)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue