-
Notifications
You must be signed in to change notification settings - Fork 32
/
suite_test.go
84 lines (73 loc) · 2.14 KB
/
suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package ginhelper_test
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/ipfs/go-log"
"github.com/phayes/freeport"
. "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/synapsecns/sanguine/core/ginhelper"
"github.com/synapsecns/sanguine/core/metrics/logger"
baseServer "github.com/synapsecns/sanguine/core/server"
"github.com/synapsecns/sanguine/core/testsuite"
"testing"
)
// GinHelperSuite defines the basic test suite.
type GinHelperSuite struct {
*testsuite.TestSuite
urls []urlTest
logger *log.ZapEventLogger
}
type urlTest struct {
url string
testType string
}
func (g *GinHelperSuite) SetupTest() {
g.urls = []urlTest{}
useExperimental := []bool{true, false}
for _, useExp := range useExperimental {
g.TestSuite.SetupTest()
g.logger = log.Logger(fmt.Sprintf("test-%d-logger", g.GetTestID()))
var testServer *gin.Engine
if useExp {
testServer = ginhelper.NewWithExperimentalLogger(g.GetTestContext(), logger.NewNullLogger())
} else {
testServer = ginhelper.New(g.logger)
}
freePort, err := freeport.GetFreePort()
Nil(g.T(), err)
g.urls = append(g.urls, urlTest{
url: fmt.Sprintf("http://localhost:%d", freePort),
testType: fmt.Sprintf("experimental: %t", useExp),
})
go func() {
connection := baseServer.Server{}
err = connection.ListenAndServe(g.GetTestContext(), fmt.Sprintf(":%d", freePort), testServer)
// we expect context cancellation errors at the end of the test
if !errors.Is(err, context.Canceled) {
Nil(g.T(), err)
}
}()
}
}
func (g *GinHelperSuite) runOnAllURLs(f func(url string)) {
for _, url := range g.urls {
url := url // capture range variable
g.T().Run(url.testType, func(t *testing.T) {
f(url.url)
})
}
}
// NewTestSuite creates a new test suite and performs some basic checks afterward.
// Every test suite in the synapse library should inherit from this suite and override where necessary.
func NewTestSuite(tb testing.TB) *GinHelperSuite {
tb.Helper()
return &GinHelperSuite{
TestSuite: testsuite.NewTestSuite(tb),
}
}
func TestGinHelperSuite(t *testing.T) {
suite.Run(t, NewTestSuite(t))
}