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

80 lines
1.8 KiB
Go
Raw Normal View History

2021-01-25 12:23:03 +00:00
package go_test
import (
"context"
"errors"
"io"
2021-01-26 17:33:29 +00:00
"net"
2021-01-25 15:35:22 +00:00
"os"
"path/filepath"
"sync"
"testing"
2021-01-25 12:23:03 +00:00
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
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/gossb-rooms/internal/maybemod/testutils"
"github.com/ssb-ngi-pointer/gossb-rooms/internal/network"
"github.com/ssb-ngi-pointer/gossb-rooms/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
func makeNamedTestBot(t testing.TB, name string, opts []roomsrv.Option) *roomsrv.Server {
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.WithListenAddr(":0"),
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
)
theBot, err := roomsrv.New(botOptions...)
r.NoError(err)
return theBot
}