converge/pkg/testsupport/utils.go
Erik Brakkee 7d25f39f5b test for connecting clients and bidirectional communication to agent.
Required lots of rework since the GOBChannel appeared to be reading
ahead of the data it actually needed. Now using more low-level IO
to send the clientId over to the agent instead.
2024-08-22 16:16:02 +02:00

91 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 func() {
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]))
}