refactor makeTestClient

re-use keys and be able to change the mocks
This commit is contained in:
Henry 2021-04-22 12:13:34 +02:00
parent bdf99ab4fa
commit 3093385635
3 changed files with 60 additions and 30 deletions

View File

@ -34,9 +34,9 @@ func TestEndpointClients(t *testing.T) {
ctx = ts.ctx
// create three test clients
alf, alfFeed := ts.makeTestClient("alf")
bre, breFeed := ts.makeTestClient("bre")
carl, carlFeed := ts.makeTestClient("carl")
alf := ts.makeTestClient("alf")
bre := ts.makeTestClient("bre")
carl := ts.makeTestClient("carl")
// let carl join the room
// carl wont announce to emulate manyverse
@ -48,7 +48,7 @@ func TestEndpointClients(t *testing.T) {
go logEndpointsStream(ts, carlEndpointsSerc, "carl", announcementsForCarl)
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")
// 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
// assert what alf saw
_, seen = announcementsForAlf[carlFeed.Ref()]
_, seen = announcementsForAlf[carl.feed.Ref()]
a.True(seen, "alf saw carl")
_, seen = announcementsForAlf[alfFeed.Ref()]
_, seen = announcementsForAlf[alf.feed.Ref()]
a.True(seen, "alf saw himself")
// assert what carl saw
_, seen = announcementsForCarl[alfFeed.Ref()]
_, seen = announcementsForCarl[alf.feed.Ref()]
a.True(seen, "carl saw alf")
// 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
// assert bre saw the other two and herself
_, seen = announcementsForBre[carlFeed.Ref()]
_, seen = announcementsForBre[carl.feed.Ref()]
a.True(seen, "bre saw carl")
_, seen = announcementsForBre[alfFeed.Ref()]
_, seen = announcementsForBre[alf.feed.Ref()]
a.True(seen, "bre saw alf")
_, seen = announcementsForBre[breFeed.Ref()]
_, seen = announcementsForBre[bre.feed.Ref()]
a.True(seen, "bre saw herself")
// assert the others saw bre
_, seen = announcementsForAlf[breFeed.Ref()]
_, seen = announcementsForAlf[bre.feed.Ref()]
a.True(seen, "alf saw bre")
_, seen = announcementsForCarl[breFeed.Ref()]
_, seen = announcementsForCarl[bre.feed.Ref()]
a.True(seen, "carl saw bre")
// terminate server and the clients

View File

@ -31,27 +31,27 @@ func TestStaleMembers(t *testing.T) {
// two new clients, connected to server automaticaly
// default to member during makeNamedTestBot
tal, _ := ts.makeTestClient("tal")
srh, srhFeed := ts.makeTestClient("srh")
tal := ts.makeTestClient("tal")
srh := ts.makeTestClient("srh")
// announce srh so that other could connect
var ok bool
err := srh.Async(ctx, &ok, muxrpc.TypeJSON, muxrpc.Method{"room", "announce"})
r.NoError(err)
_, has := ts.srv.StateManager.Has(srhFeed)
_, has := ts.srv.StateManager.Has(srh.feed)
r.True(has, "srh should be connected")
// shut down srh
srh.Terminate()
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")
// try to connect srh
var arg server.ConnectArg
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)
r.NoError(err)
@ -67,8 +67,12 @@ func TestStaleMembers(t *testing.T) {
_, err = snk.Write([]byte("fake keyexchange"))
r.Error(err, "stream should should be canceled")
// restart srh
// shut everythign down
ts.srv.Shutdown()
tal.Terminate()
srh.Terminate()
ts.srv.Close()
// wait for all muxrpc serve()s to exit

View File

@ -82,6 +82,9 @@ type testSession struct {
ctx context.Context
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 {
@ -123,8 +126,9 @@ func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roo
r.NoError(err)
ts := testSession{
t: t,
srv: theBot,
t: t,
srv: theBot,
clientKeys: make(map[string]*keys.KeyPair),
}
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
}
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)
// create a fresh keypairs for the clients
client, err := keys.NewKeyPair(nil)
r.NoError(err)
client, has := ts.clientKeys[name]
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())
// add it as a memeber
memberID, err := ts.srv.Members.Add(ts.ctx, client.Feed, roomdb.RoleMember)
r.NoError(err)
ts.t.Log(name, "is member ID:", memberID)
// add it as a memeber, if it isnt already
_, err := ts.srv.Members.GetByFeed(ts.ctx, client.Feed)
if errors.Is(err, roomdb.ErrNotFound) {
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
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
testPath := filepath.Join("testrun", ts.t.Name())
debugConn := debug.Dump(filepath.Join(testPath, "client-"+name), authedConn)
pkr := muxrpc.NewPacker(debugConn)
// testPath := filepath.Join("testrun", ts.t.Name())
// debugConn := debug.Dump(filepath.Join(testPath, "client-"+name), authedConn)
pkr := muxrpc.NewPacker(authedConn)
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.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()