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

170 lines
3.7 KiB
Go
Raw Normal View History

2021-02-09 11:53:33 +00:00
// SPDX-License-Identifier: MIT
2021-01-25 12:23:03 +00:00
package go_test
import (
"context"
2021-04-12 13:00:34 +00:00
"crypto/rand"
2021-01-25 12:23:03 +00:00
"errors"
2021-04-12 13:00:34 +00:00
"fmt"
2021-01-25 12:23:03 +00:00
"io"
2021-01-26 17:33:29 +00:00
"net"
2021-01-25 15:35:22 +00:00
"os"
"path/filepath"
"sync"
"testing"
2021-04-12 13:00:34 +00:00
"time"
"golang.org/x/sync/errgroup"
2021-01-25 12:23:03 +00:00
2021-03-19 10:51:06 +00:00
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
2021-01-25 12:23:03 +00:00
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
2021-02-11 15:43:19 +00:00
_ "github.com/mattn/go-sqlite3"
2021-01-25 15:35:22 +00:00
"github.com/stretchr/testify/require"
2021-01-26 17:33:29 +00:00
"go.cryptoscope.co/muxrpc/v2/debug"
2021-01-25 15:35:22 +00:00
"github.com/ssb-ngi-pointer/go-ssb-room/internal/maybemod/testutils"
"github.com/ssb-ngi-pointer/go-ssb-room/internal/network"
2021-02-11 15:43:19 +00:00
"github.com/ssb-ngi-pointer/go-ssb-room/internal/repo"
"github.com/ssb-ngi-pointer/go-ssb-room/internal/signinwithssb"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite"
"github.com/ssb-ngi-pointer/go-ssb-room/roomsrv"
2021-01-25 12:23:03 +00:00
)
2021-01-25 15:35:22 +00:00
var (
initLogging sync.Once
mainLog = log.NewNopLogger()
)
// cant call testing.pkg in init()
func testInit(t *testing.T) {
initLogging.Do(func() {
if testing.Verbose() {
mainLog = testutils.NewRelativeTimeLogger(nil)
}
})
os.RemoveAll(filepath.Join("testrun", t.Name()))
}
2021-01-25 12:23:03 +00:00
type botServer struct {
ctx context.Context
log log.Logger
}
func newBotServer(ctx context.Context, log log.Logger) botServer {
return botServer{ctx, log}
}
func (bs botServer) Serve(s *roomsrv.Server) func() error {
return func() error {
err := s.Network.Serve(bs.ctx)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, context.Canceled) {
return nil
}
level.Warn(bs.log).Log("event", "bot serve exited", "err", err)
}
return err
}
}
2021-01-25 15:35:22 +00:00
2021-04-20 10:14:24 +00:00
type testSession struct {
t testing.TB
srv *roomsrv.Server
ctx context.Context
serveGroup *errgroup.Group
}
func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roomsrv.Option) *testSession {
2021-01-25 15:35:22 +00:00
r := require.New(t)
testPath := filepath.Join("testrun", t.Name(), "bot-"+name)
2021-01-26 17:33:29 +00:00
os.RemoveAll(testPath)
2021-01-25 15:35:22 +00:00
botOptions := append(opts,
roomsrv.WithLogger(log.With(mainLog, "bot", name)),
roomsrv.WithRepoPath(testPath),
roomsrv.WithNetworkConnTracker(network.NewLastWinsTracker()),
2021-01-26 17:33:29 +00:00
roomsrv.WithPostSecureConnWrapper(func(conn net.Conn) (net.Conn, error) {
return debug.WrapDump(filepath.Join(testPath, "muxdump"), conn)
}),
2021-01-25 15:35:22 +00:00
)
2021-02-11 15:43:19 +00:00
// could also use the mocks
db, err := sqlite.Open(repo.New(testPath))
r.NoError(err)
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Log("db close failed: ", err)
}
2021-02-11 15:43:19 +00:00
})
err = db.Config.SetPrivacyMode(context.TODO(), roomdb.ModeRestricted)
r.NoError(err)
netInfo := network.ServerEndpointDetails{
Domain: name,
ListenAddressMUXRPC: ":0",
2021-04-19 12:57:42 +00:00
UseSubdomainForAliases: true,
}
sb := signinwithssb.NewSignalBridge()
theBot, err := roomsrv.New(db.Members, db.DeniedKeys, db.Aliases, db.AuthWithSSB, sb, db.Config, netInfo, botOptions...)
2021-01-25 15:35:22 +00:00
r.NoError(err)
2021-04-12 13:00:34 +00:00
2021-04-20 10:14:24 +00:00
ts := testSession{
t: t,
srv: theBot,
}
ts.serveGroup, ts.ctx = errgroup.WithContext(ctx)
ts.serveGroup.Go(func() error {
return theBot.Network.Serve(ts.ctx)
})
return &ts
2021-04-12 13:00:34 +00:00
}
2021-04-20 10:14:24 +00:00
// TODO: refactor for single test session and use makeTestClient()
func createServerAndBots(t *testing.T, ctx context.Context, count uint) []*testSession {
2021-04-12 13:00:34 +00:00
testInit(t)
r := require.New(t)
appKey := make([]byte, 32)
rand.Read(appKey)
netOpts := []roomsrv.Option{
roomsrv.WithAppKey(appKey),
roomsrv.WithContext(ctx),
}
2021-04-20 10:14:24 +00:00
theBots := []*testSession{}
session := makeNamedTestBot(t, "srv", ctx, netOpts)
theBots = append(theBots, session)
2021-04-12 13:00:34 +00:00
for i := uint(1); i < count+1; i++ {
2021-04-20 10:14:24 +00:00
// TODO: replace with makeClient?!
clientSession := makeNamedTestBot(t, fmt.Sprintf("%d", i), ctx, netOpts)
theBots = append(theBots, clientSession)
2021-04-12 13:00:34 +00:00
}
t.Cleanup(func() {
time.Sleep(1 * time.Second)
for _, bot := range theBots {
2021-04-20 10:14:24 +00:00
bot.srv.Shutdown()
r.NoError(bot.srv.Close())
2021-04-12 13:00:34 +00:00
}
})
return theBots
}