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

163 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-03-19 10:51:06 +00:00
func makeNamedTestBot(t testing.TB, name string, opts []roomsrv.Option) (roomdb.MembersService, *roomsrv.Server) {
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-03-19 10:51:06 +00:00
return db.Members, theBot
2021-01-25 15:35:22 +00:00
}
2021-04-12 13:00:34 +00:00
type testBot struct {
Server *roomsrv.Server
Members roomdb.MembersService
}
func createServerAndBots(t *testing.T, ctx context.Context, count uint) []testBot {
testInit(t)
r := require.New(t)
botgroup, ctx := errgroup.WithContext(ctx)
bs := newBotServer(ctx, mainLog)
appKey := make([]byte, 32)
rand.Read(appKey)
netOpts := []roomsrv.Option{
roomsrv.WithAppKey(appKey),
roomsrv.WithContext(ctx),
}
theBots := []testBot{}
srvsMembers, serv := makeNamedTestBot(t, "srv", netOpts)
botgroup.Go(bs.Serve(serv))
theBots = append(theBots, testBot{
Server: serv,
Members: srvsMembers,
})
for i := uint(1); i < count+1; i++ {
botMembers, botSrv := makeNamedTestBot(t, fmt.Sprintf("%d", i), netOpts)
botgroup.Go(bs.Serve(botSrv))
theBots = append(theBots, testBot{
Server: botSrv,
Members: botMembers,
})
}
t.Cleanup(func() {
time.Sleep(1 * time.Second)
for _, bot := range theBots {
bot.Server.Shutdown()
r.NoError(bot.Server.Close())
}
r.NoError(botgroup.Wait())
})
return theBots
}