refactor makeTestClient
re-use keys and be able to change the mocks
This commit is contained in:
parent
bdf99ab4fa
commit
3093385635
|
@ -34,9 +34,9 @@ func TestEndpointClients(t *testing.T) {
|
||||||
ctx = ts.ctx
|
ctx = ts.ctx
|
||||||
|
|
||||||
// create three test clients
|
// create three test clients
|
||||||
alf, alfFeed := ts.makeTestClient("alf")
|
alf := ts.makeTestClient("alf")
|
||||||
bre, breFeed := ts.makeTestClient("bre")
|
bre := ts.makeTestClient("bre")
|
||||||
carl, carlFeed := ts.makeTestClient("carl")
|
carl := ts.makeTestClient("carl")
|
||||||
|
|
||||||
// let carl join the room
|
// let carl join the room
|
||||||
// carl wont announce to emulate manyverse
|
// carl wont announce to emulate manyverse
|
||||||
|
@ -48,7 +48,7 @@ func TestEndpointClients(t *testing.T) {
|
||||||
go logEndpointsStream(ts, carlEndpointsSerc, "carl", announcementsForCarl)
|
go logEndpointsStream(ts, carlEndpointsSerc, "carl", announcementsForCarl)
|
||||||
time.Sleep(1 * time.Second) // give some time to process new events
|
time.Sleep(1 * time.Second) // give some time to process new events
|
||||||
|
|
||||||
_, seen := announcementsForCarl[carlFeed.Ref()]
|
_, seen := announcementsForCarl[carl.feed.Ref()]
|
||||||
a.True(seen, "carl saw himself")
|
a.True(seen, "carl saw himself")
|
||||||
|
|
||||||
// let alf join the room
|
// let alf join the room
|
||||||
|
@ -60,13 +60,13 @@ func TestEndpointClients(t *testing.T) {
|
||||||
time.Sleep(1 * time.Second) // give some time to process new events
|
time.Sleep(1 * time.Second) // give some time to process new events
|
||||||
|
|
||||||
// assert what alf saw
|
// assert what alf saw
|
||||||
_, seen = announcementsForAlf[carlFeed.Ref()]
|
_, seen = announcementsForAlf[carl.feed.Ref()]
|
||||||
a.True(seen, "alf saw carl")
|
a.True(seen, "alf saw carl")
|
||||||
_, seen = announcementsForAlf[alfFeed.Ref()]
|
_, seen = announcementsForAlf[alf.feed.Ref()]
|
||||||
a.True(seen, "alf saw himself")
|
a.True(seen, "alf saw himself")
|
||||||
|
|
||||||
// assert what carl saw
|
// assert what carl saw
|
||||||
_, seen = announcementsForCarl[alfFeed.Ref()]
|
_, seen = announcementsForCarl[alf.feed.Ref()]
|
||||||
a.True(seen, "carl saw alf")
|
a.True(seen, "carl saw alf")
|
||||||
|
|
||||||
// let bre join the room
|
// let bre join the room
|
||||||
|
@ -79,17 +79,17 @@ func TestEndpointClients(t *testing.T) {
|
||||||
time.Sleep(1 * time.Second) // give some time to process new events
|
time.Sleep(1 * time.Second) // give some time to process new events
|
||||||
|
|
||||||
// assert bre saw the other two and herself
|
// assert bre saw the other two and herself
|
||||||
_, seen = announcementsForBre[carlFeed.Ref()]
|
_, seen = announcementsForBre[carl.feed.Ref()]
|
||||||
a.True(seen, "bre saw carl")
|
a.True(seen, "bre saw carl")
|
||||||
_, seen = announcementsForBre[alfFeed.Ref()]
|
_, seen = announcementsForBre[alf.feed.Ref()]
|
||||||
a.True(seen, "bre saw alf")
|
a.True(seen, "bre saw alf")
|
||||||
_, seen = announcementsForBre[breFeed.Ref()]
|
_, seen = announcementsForBre[bre.feed.Ref()]
|
||||||
a.True(seen, "bre saw herself")
|
a.True(seen, "bre saw herself")
|
||||||
|
|
||||||
// assert the others saw bre
|
// assert the others saw bre
|
||||||
_, seen = announcementsForAlf[breFeed.Ref()]
|
_, seen = announcementsForAlf[bre.feed.Ref()]
|
||||||
a.True(seen, "alf saw bre")
|
a.True(seen, "alf saw bre")
|
||||||
_, seen = announcementsForCarl[breFeed.Ref()]
|
_, seen = announcementsForCarl[bre.feed.Ref()]
|
||||||
a.True(seen, "carl saw bre")
|
a.True(seen, "carl saw bre")
|
||||||
|
|
||||||
// terminate server and the clients
|
// terminate server and the clients
|
||||||
|
|
|
@ -31,27 +31,27 @@ func TestStaleMembers(t *testing.T) {
|
||||||
|
|
||||||
// two new clients, connected to server automaticaly
|
// two new clients, connected to server automaticaly
|
||||||
// default to member during makeNamedTestBot
|
// default to member during makeNamedTestBot
|
||||||
tal, _ := ts.makeTestClient("tal")
|
tal := ts.makeTestClient("tal")
|
||||||
srh, srhFeed := ts.makeTestClient("srh")
|
srh := ts.makeTestClient("srh")
|
||||||
|
|
||||||
// announce srh so that other could connect
|
// announce srh so that other could connect
|
||||||
var ok bool
|
var ok bool
|
||||||
err := srh.Async(ctx, &ok, muxrpc.TypeJSON, muxrpc.Method{"room", "announce"})
|
err := srh.Async(ctx, &ok, muxrpc.TypeJSON, muxrpc.Method{"room", "announce"})
|
||||||
r.NoError(err)
|
r.NoError(err)
|
||||||
|
|
||||||
_, has := ts.srv.StateManager.Has(srhFeed)
|
_, has := ts.srv.StateManager.Has(srh.feed)
|
||||||
r.True(has, "srh should be connected")
|
r.True(has, "srh should be connected")
|
||||||
|
|
||||||
// shut down srh
|
// shut down srh
|
||||||
srh.Terminate()
|
srh.Terminate()
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
_, has = ts.srv.StateManager.Has(srhFeed)
|
_, has = ts.srv.StateManager.Has(srh.feed)
|
||||||
r.False(has, "srh shouldn't be connected")
|
r.False(has, "srh shouldn't be connected")
|
||||||
|
|
||||||
// try to connect srh
|
// try to connect srh
|
||||||
var arg server.ConnectArg
|
var arg server.ConnectArg
|
||||||
arg.Portal = ts.srv.Whoami()
|
arg.Portal = ts.srv.Whoami()
|
||||||
arg.Target = srhFeed
|
arg.Target = srh.feed
|
||||||
|
|
||||||
src, snk, err := tal.Duplex(ctx, muxrpc.TypeBinary, muxrpc.Method{"room", "connect"}, arg)
|
src, snk, err := tal.Duplex(ctx, muxrpc.TypeBinary, muxrpc.Method{"room", "connect"}, arg)
|
||||||
r.NoError(err)
|
r.NoError(err)
|
||||||
|
@ -67,8 +67,12 @@ func TestStaleMembers(t *testing.T) {
|
||||||
_, err = snk.Write([]byte("fake keyexchange"))
|
_, err = snk.Write([]byte("fake keyexchange"))
|
||||||
r.Error(err, "stream should should be canceled")
|
r.Error(err, "stream should should be canceled")
|
||||||
|
|
||||||
|
// restart srh
|
||||||
|
|
||||||
|
// shut everythign down
|
||||||
ts.srv.Shutdown()
|
ts.srv.Shutdown()
|
||||||
tal.Terminate()
|
tal.Terminate()
|
||||||
|
srh.Terminate()
|
||||||
ts.srv.Close()
|
ts.srv.Close()
|
||||||
|
|
||||||
// wait for all muxrpc serve()s to exit
|
// wait for all muxrpc serve()s to exit
|
||||||
|
|
|
@ -82,6 +82,9 @@ type testSession struct {
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
serveGroup *errgroup.Group
|
serveGroup *errgroup.Group
|
||||||
|
|
||||||
|
// so that we can re-spawn clients
|
||||||
|
clientKeys map[string]*keys.KeyPair
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roomsrv.Option) *testSession {
|
func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roomsrv.Option) *testSession {
|
||||||
|
@ -123,8 +126,9 @@ func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roo
|
||||||
r.NoError(err)
|
r.NoError(err)
|
||||||
|
|
||||||
ts := testSession{
|
ts := testSession{
|
||||||
t: t,
|
t: t,
|
||||||
srv: theBot,
|
srv: theBot,
|
||||||
|
clientKeys: make(map[string]*keys.KeyPair),
|
||||||
}
|
}
|
||||||
|
|
||||||
ts.serveGroup, ts.ctx = errgroup.WithContext(ctx)
|
ts.serveGroup, ts.ctx = errgroup.WithContext(ctx)
|
||||||
|
@ -136,19 +140,37 @@ func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roo
|
||||||
return &ts
|
return &ts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *testSession) makeTestClient(name string) (muxrpc.Endpoint, refs.FeedRef) {
|
type testClient struct {
|
||||||
|
muxrpc.Endpoint
|
||||||
|
|
||||||
|
feed refs.FeedRef
|
||||||
|
|
||||||
|
mockedHandler muxrpc.FakeHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *testSession) makeTestClient(name string) testClient {
|
||||||
r := require.New(ts.t)
|
r := require.New(ts.t)
|
||||||
|
|
||||||
// create a fresh keypairs for the clients
|
// create a fresh keypairs for the clients
|
||||||
client, err := keys.NewKeyPair(nil)
|
client, has := ts.clientKeys[name]
|
||||||
r.NoError(err)
|
if !has {
|
||||||
|
var err error
|
||||||
|
client, err = keys.NewKeyPair(nil)
|
||||||
|
r.NoError(err)
|
||||||
|
ts.clientKeys[name] = client
|
||||||
|
}
|
||||||
|
|
||||||
ts.t.Log(name, "is", client.Feed.ShortRef())
|
ts.t.Log(name, "is", client.Feed.ShortRef())
|
||||||
|
|
||||||
// add it as a memeber
|
// add it as a memeber, if it isnt already
|
||||||
memberID, err := ts.srv.Members.Add(ts.ctx, client.Feed, roomdb.RoleMember)
|
_, err := ts.srv.Members.GetByFeed(ts.ctx, client.Feed)
|
||||||
r.NoError(err)
|
if errors.Is(err, roomdb.ErrNotFound) {
|
||||||
ts.t.Log(name, "is member ID:", memberID)
|
memberID, err := ts.srv.Members.Add(ts.ctx, client.Feed, roomdb.RoleMember)
|
||||||
|
r.NoError(err)
|
||||||
|
ts.t.Log(name, "is member ID:", memberID)
|
||||||
|
} else {
|
||||||
|
r.NoError(err)
|
||||||
|
}
|
||||||
|
|
||||||
// default app key for the secret-handshake connection
|
// default app key for the secret-handshake connection
|
||||||
ak, err := base64.StdEncoding.DecodeString("1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=")
|
ak, err := base64.StdEncoding.DecodeString("1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=")
|
||||||
|
@ -167,9 +189,9 @@ func (ts *testSession) makeTestClient(name string) (muxrpc.Endpoint, refs.FeedRe
|
||||||
|
|
||||||
var muxMock muxrpc.FakeHandler
|
var muxMock muxrpc.FakeHandler
|
||||||
|
|
||||||
testPath := filepath.Join("testrun", ts.t.Name())
|
// testPath := filepath.Join("testrun", ts.t.Name())
|
||||||
debugConn := debug.Dump(filepath.Join(testPath, "client-"+name), authedConn)
|
// debugConn := debug.Dump(filepath.Join(testPath, "client-"+name), authedConn)
|
||||||
pkr := muxrpc.NewPacker(debugConn)
|
pkr := muxrpc.NewPacker(authedConn)
|
||||||
|
|
||||||
wsEndpoint := muxrpc.Handle(pkr, &muxMock, muxrpc.WithContext(ts.ctx))
|
wsEndpoint := muxrpc.Handle(pkr, &muxMock, muxrpc.WithContext(ts.ctx))
|
||||||
|
|
||||||
|
@ -188,7 +210,11 @@ func (ts *testSession) makeTestClient(name string) (muxrpc.Endpoint, refs.FeedRe
|
||||||
r.NoError(err)
|
r.NoError(err)
|
||||||
r.True(yup, "server is not a room?")
|
r.True(yup, "server is not a room?")
|
||||||
|
|
||||||
return wsEndpoint, client.Feed
|
return testClient{
|
||||||
|
feed: client.Feed,
|
||||||
|
Endpoint: wsEndpoint,
|
||||||
|
mockedHandler: muxMock,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: refactor for single test session and use makeTestClient()
|
// TODO: refactor for single test session and use makeTestClient()
|
||||||
|
|
Loading…
Reference in New Issue