restructuring test code by introducing a testsupport package
Making it easy 6to start a porof server in tests.
This commit is contained in:
parent
b3f5eead9b
commit
d5a6d70bc4
@ -7,6 +7,7 @@ import (
|
||||
"git.wamblee.org/converge/pkg/server/matchmaker"
|
||||
"git.wamblee.org/converge/pkg/server/prometheus"
|
||||
"git.wamblee.org/converge/pkg/server/ui"
|
||||
pprof2 "git.wamblee.org/converge/pkg/support/pprof"
|
||||
"git.wamblee.org/converge/pkg/support/websocketutil"
|
||||
"log"
|
||||
"net"
|
||||
@ -143,7 +144,7 @@ func main() {
|
||||
context := HttpContext{mux: http.NewServeMux(), path: contextpath}
|
||||
|
||||
if pprof {
|
||||
registerPprof(context.mux, "/debug/pprof")
|
||||
pprof2.RegisterPprof(context.mux, "/debug/pprof")
|
||||
}
|
||||
setupWebUI(context, registrationService, clientService, sessionService, staticdir, downloaddir)
|
||||
// Catch all for the web UI
|
||||
|
@ -2,7 +2,7 @@ package comms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.wamblee.org/converge/pkg/support/iowrappers"
|
||||
"git.wamblee.org/converge/pkg/testsupport"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"log"
|
||||
"sync"
|
||||
@ -24,7 +24,7 @@ func TestAgentServerTestSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
func (suite *AgentServerTestSuite) TestNewCommChannel() {
|
||||
bitpipe := iowrappers.NewInmemoryConnection(context.Background(), "inmemory")
|
||||
bitpipe := testsupport.NewInmemoryConnection(context.Background(), "inmemory")
|
||||
agentConnection := bitpipe.Front()
|
||||
serverConnection := bitpipe.Back()
|
||||
requires := suite.Require()
|
||||
|
@ -1,11 +1,11 @@
|
||||
package main
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
)
|
||||
|
||||
func registerPprof(mux *http.ServeMux, contextPath string) {
|
||||
func RegisterPprof(mux *http.ServeMux, contextPath string) {
|
||||
mux.HandleFunc(contextPath+"/", pprof.Index)
|
||||
mux.HandleFunc(contextPath+"/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc(contextPath+"/profile", pprof.Profile)
|
@ -1,4 +1,6 @@
|
||||
package iowrappers
|
||||
package testsupport
|
||||
|
||||
import "git.wamblee.org/converge/pkg/support/iowrappers"
|
||||
|
||||
type dummyRemoteAddr string
|
||||
|
||||
@ -14,6 +16,6 @@ func (r dummyRemoteAddr) String() string {
|
||||
// code under test reads the other side.
|
||||
|
||||
type BitPipe interface {
|
||||
Front() ReadWriteAddrCloser
|
||||
Back() ReadWriteAddrCloser
|
||||
Front() iowrappers.ReadWriteAddrCloser
|
||||
Back() iowrappers.ReadWriteAddrCloser
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package iowrappers
|
||||
package testsupport
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,11 +1,11 @@
|
||||
package iowrappers
|
||||
package testsupport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -13,6 +13,7 @@ import (
|
||||
type ChannelReadWriterTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
pprofServer *http.Server
|
||||
ctx context.Context
|
||||
cancelFunc context.CancelFunc
|
||||
toChannel chan<- []byte
|
||||
@ -24,48 +25,38 @@ func TestChannelReadWriterSuite(t *testing.T) {
|
||||
suite.Run(t, &ChannelReadWriterTestSuite{})
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) createChannel() {
|
||||
func (s *ChannelReadWriterTestSuite) createChannel() {
|
||||
toChannel := make(chan []byte)
|
||||
fromChannel := make(chan []byte)
|
||||
suite.toChannel = toChannel
|
||||
suite.fromChannel = fromChannel
|
||||
s.toChannel = toChannel
|
||||
s.fromChannel = fromChannel
|
||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||
ctx, timeoutCancelFunc := context.WithTimeout(ctx, 10*time.Second)
|
||||
suite.ctx = ctx
|
||||
suite.cancelFunc = func() {
|
||||
s.ctx = ctx
|
||||
s.cancelFunc = func() {
|
||||
timeoutCancelFunc()
|
||||
cancelFunc()
|
||||
}
|
||||
suite.conn = NewChannelReadWriter(ctx, toChannel, fromChannel)
|
||||
s.conn = NewChannelReadWriter(ctx, toChannel, fromChannel)
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) SetupTest() {
|
||||
suite.createChannel()
|
||||
func (s *ChannelReadWriterTestSuite) SetupSuite() {
|
||||
s.pprofServer = startPprof("")
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) TearDownTest() {
|
||||
suite.cancelFunc()
|
||||
func (s *ChannelReadWriterTestSuite) TearDownSuite() {
|
||||
stopPprof(s.ctx, s.pprofServer)
|
||||
}
|
||||
|
||||
type TestFunc func() any
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) runAndWait(functions ...TestFunc) []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 (s *ChannelReadWriterTestSuite) SetupTest() {
|
||||
s.createChannel()
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) Test_SuccessfulCommunication() {
|
||||
func (s *ChannelReadWriterTestSuite) TearDownTest() {
|
||||
s.cancelFunc()
|
||||
}
|
||||
|
||||
func (s *ChannelReadWriterTestSuite) Test_SuccessfulCommunication() {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []string
|
||||
@ -107,15 +98,27 @@ func (suite *ChannelReadWriterTestSuite) Test_SuccessfulCommunication() {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
suite.Run(test.name, func() {
|
||||
suite.runAndWait(
|
||||
s.Run(test.name, s.runSuccessfulCommunicationTest(test))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *ChannelReadWriterTestSuite) runSuccessfulCommunicationTest(test struct {
|
||||
name string
|
||||
data []string
|
||||
chunkSizes []int
|
||||
chunks []string
|
||||
}) func() {
|
||||
return func() {
|
||||
runAndWait(
|
||||
&s.Suite,
|
||||
func() any {
|
||||
for _, d := range test.data {
|
||||
select {
|
||||
case <-suite.ctx.Done():
|
||||
suite.FailNow("deadline reached")
|
||||
case <-s.ctx.Done():
|
||||
s.FailNow("deadline reached")
|
||||
log.Println("Write deadline exceeded")
|
||||
case suite.toChannel <- []byte(d):
|
||||
case s.toChannel <- []byte(d):
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@ -124,28 +127,26 @@ func (suite *ChannelReadWriterTestSuite) Test_SuccessfulCommunication() {
|
||||
remainder := strings.Join(test.data, "")
|
||||
for i, chunkSize := range test.chunkSizes {
|
||||
buf := make([]byte, chunkSize)
|
||||
n, err := suite.conn.Read(buf)
|
||||
suite.Nil(err)
|
||||
suite.Equal(n, len(test.chunks[i]))
|
||||
suite.Equal([]byte(remainder[:n]), buf[:n])
|
||||
n, err := s.conn.Read(buf)
|
||||
s.Nil(err)
|
||||
s.Equal(n, len(test.chunks[i]))
|
||||
s.Equal([]byte(remainder[:n]), buf[:n])
|
||||
remainder = remainder[n:]
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChannelReadWriterTestSuite) Test_Close() {
|
||||
s.FailNow("todo")
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) Test_Close() {
|
||||
suite.FailNow("todo")
|
||||
func (s *ChannelReadWriterTestSuite) Test_CloseTwice() {
|
||||
s.FailNow("todo")
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) Test_CloseTwice() {
|
||||
suite.FailNow("todo")
|
||||
}
|
||||
|
||||
func (suite *ChannelReadWriterTestSuite) Test_ContextCanceled() {
|
||||
suite.FailNow("todo")
|
||||
func (s *ChannelReadWriterTestSuite) Test_ContextCanceled() {
|
||||
s.FailNow("todo")
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package iowrappers
|
||||
package testsupport
|
||||
|
||||
import "context"
|
||||
|
57
pkg/testsupport/utils.go
Normal file
57
pkg/testsupport/utils.go
Normal file
@ -0,0 +1,57 @@
|
||||
package testsupport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.wamblee.org/converge/pkg/support/pprof"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"log"
|
||||
"net/http"
|
||||
_ "runtime/pprof"
|
||||
"sync"
|
||||
)
|
||||
|
||||
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 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")
|
||||
}()
|
||||
return &srv
|
||||
}
|
||||
|
||||
func stopPprof(ctx context.Context, server *http.Server) {
|
||||
err := server.Shutdown(ctx)
|
||||
if err != nil {
|
||||
log.Println("Error shutting down test pprof server")
|
||||
return
|
||||
}
|
||||
log.Println("Test pprof server stopped")
|
||||
}
|
Loading…
Reference in New Issue
Block a user