go-ssb-room/muxrpc/test/go/endpoints_test.go

144 lines
3.7 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
//
// SPDX-License-Identifier: MIT
2021-04-20 10:14:35 +00:00
package go_test
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/ssbc/go-muxrpc/v2"
2021-04-20 15:02:28 +00:00
"github.com/stretchr/testify/assert"
2021-04-20 10:14:35 +00:00
"github.com/stretchr/testify/require"
refs "github.com/ssbc/go-ssb-refs"
2021-04-20 10:14:35 +00:00
)
2021-04-20 15:02:28 +00:00
type announcements map[string]struct{}
2021-04-20 10:14:35 +00:00
// we will let three clients (alf, bre, crl) join and see that the endpoint output is as expected
func TestEndpointClients(t *testing.T) {
testInit(t)
2021-04-20 10:14:35 +00:00
r := require.New(t)
2021-04-20 15:02:28 +00:00
a := assert.New(t)
2021-04-20 10:14:35 +00:00
testPath := filepath.Join("testrun", t.Name())
os.RemoveAll(testPath)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
// create the roomsrv
ts := makeNamedTestBot(t, "server", ctx, nil)
ctx = ts.ctx
2021-04-20 15:02:28 +00:00
// create three test clients
alf := ts.makeTestClient("alf")
bre := ts.makeTestClient("bre")
carl := ts.makeTestClient("carl")
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
// let carl join the room
// carl wont announce to emulate manyverse
carlEndpointsSrc, err := carl.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
2021-04-20 15:02:28 +00:00
r.NoError(err)
t.Log("carl opened endpoints")
announcementsForCarl := make(announcements)
go logEndpointsStream(ts, carlEndpointsSrc, "carl", announcementsForCarl)
2021-04-20 15:02:28 +00:00
time.Sleep(1 * time.Second) // give some time to process new events
2021-04-20 10:14:35 +00:00
_, seen := announcementsForCarl[carl.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "carl saw himself")
// let alf join the room
2021-04-20 10:14:35 +00:00
alfEndpointsSerc, err := alf.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
r.NoError(err)
2021-04-20 15:02:28 +00:00
announcementsForAlf := make(announcements)
2021-04-22 08:19:03 +00:00
go logEndpointsStream(ts, alfEndpointsSerc, "alf", announcementsForAlf)
2021-04-20 15:02:28 +00:00
time.Sleep(1 * time.Second) // give some time to process new events
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
// assert what alf saw
_, seen = announcementsForAlf[carl.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "alf saw carl")
_, seen = announcementsForAlf[alf.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "alf saw himself")
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
// assert what carl saw
_, seen = announcementsForCarl[alf.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "carl saw alf")
2021-04-20 10:14:35 +00:00
// let bre join the room
breEndpointsSrc, err := bre.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
r.NoError(err)
2021-04-20 15:02:28 +00:00
announcementsForBre := make(announcements)
2021-04-22 08:19:03 +00:00
go logEndpointsStream(ts, breEndpointsSrc, "bre", announcementsForBre)
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
time.Sleep(1 * time.Second) // give some time to process new events
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
// assert bre saw the other two and herself
_, seen = announcementsForBre[carl.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "bre saw carl")
_, seen = announcementsForBre[alf.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "bre saw alf")
_, seen = announcementsForBre[bre.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "bre saw herself")
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
// assert the others saw bre
_, seen = announcementsForAlf[bre.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "alf saw bre")
_, seen = announcementsForCarl[bre.feed.String()]
2021-04-20 15:02:28 +00:00
a.True(seen, "carl saw bre")
2021-04-20 10:14:35 +00:00
2021-04-20 15:02:28 +00:00
// terminate server and the clients
ts.srv.Shutdown()
2021-04-20 10:14:35 +00:00
alf.Terminate()
bre.Terminate()
carl.Terminate()
2021-04-20 15:02:28 +00:00
ts.srv.Close()
2021-04-20 10:14:35 +00:00
// wait for all muxrpc serve()s to exit
r.NoError(ts.serveGroup.Wait())
2021-04-20 15:02:28 +00:00
cancel()
2021-04-20 10:14:35 +00:00
}
2021-04-22 08:19:03 +00:00
// consume endpoint messaes and put each peer on the passed map
func logEndpointsStream(ts *testSession, src *muxrpc.ByteSource, who string, a announcements) {
2021-04-20 15:02:28 +00:00
var edps []refs.FeedRef
2021-04-20 10:14:35 +00:00
for src.Next(ts.ctx) {
body, err := src.Bytes()
if err != nil {
panic(err)
}
// ts.t.Log(who, "got body:", string(body))
2021-04-20 15:02:28 +00:00
err = json.Unmarshal(body, &edps)
2021-04-20 10:14:35 +00:00
if err != nil {
panic(err)
}
2021-04-20 15:02:28 +00:00
ts.t.Log(who, "got endpoints:", len(edps))
for i, f := range edps {
ts.t.Log(who, ":", i, f.ShortSigil())
2021-04-20 15:02:28 +00:00
// mark as f is present
a[f.String()] = struct{}{}
2021-04-20 15:02:28 +00:00
}
2021-04-20 10:14:35 +00:00
}
if err := src.Err(); err != nil {
ts.t.Log("source errored: ", err)
return
}
ts.t.Log(who, "stream closed")
}