converge/pkg/testsupport/utils.go
Erik Brakkee 538a697770 multiple clients connecting to multiple agents.
Clients cannot yet be started in parallel. due to subtle issue in test
setup with accept
2024-08-22 22:44:54 +02:00

89 lines
1.9 KiB
Go

package testsupport
import (
"context"
"git.wamblee.org/converge/pkg/support/pprof"
"github.com/stretchr/testify/suite"
"io"
"log"
"net/http"
"os"
_ "runtime/pprof"
"sync"
"time"
)
type TestFunction func() any
func RunAndWait(suite *suite.Suite, functions ...TestFunction) []any {
wg := sync.WaitGroup{}
wg.Add(len(functions))
res := make([]any, len(functions))
for i, function := range functions {
go func() {
defer wg.Done()
res[i] = function()
}()
}
wg.Wait()
return res
}
func StartPprof(port string) *http.Server {
if os.Getenv("PPROF") == "" {
return nil
}
if port == "" {
port = ":9000"
}
mux := http.NewServeMux()
pprof.RegisterPprof(mux, "/debug/pprof")
srv := http.Server{
Addr: port,
Handler: mux,
}
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("Could not start pprof listener: %v", err)
}
log.Println("Test pprof server started: " + port)
}()
return &srv
}
func StopPprof(ctx context.Context, server *http.Server) {
if os.Getenv("PPROF") == "" {
return
}
err := server.Shutdown(ctx)
if err != nil {
log.Println("Error shutting down test pprof server")
return
}
log.Println("Test pprof server stopped")
}
func CreateTestContext(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
ctx, cancelFunc := context.WithCancel(ctx)
ctx, timeoutCancelFunc := context.WithTimeout(ctx, timeout)
compositeCancelFunc := func() {
timeoutCancelFunc()
cancelFunc()
}
return ctx, compositeCancelFunc
}
func AssertWriteData(s *suite.Suite, data string, writer io.Writer) {
n, err := writer.Write([]byte(data))
s.Nil(err)
s.Equal(len(data), n)
}
func AssertReadData(s *suite.Suite, data string, reader io.Reader) {
buf := make([]byte, len(data)*2)
n, err := reader.Read(buf)
s.Nil(err)
s.Equal(len(data), n)
s.Equal(data, string(buf[:n]))
}