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