Skip to content

Commit 0dada23

Browse files
committed
Uses Chi as the underlying router for Parrot
1 parent 6b23f5d commit 0dada23

File tree

13 files changed

+589
-464
lines changed

13 files changed

+589
-464
lines changed

parrot/.changeset/v0.3.0.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- Changed underlying parrot router to use chi: https://github.com/go-chi/chi
2+
- Enables usage of wildcards and more stable routing with less code
3+
4+
### Before Bench
5+
6+
```sh
7+
goos: darwin
8+
goarch: arm64
9+
pkg: github.com/smartcontractkit/chainlink-testing-framework/parrot
10+
cpu: Apple M3 Max
11+
BenchmarkRegisterRoute-14 3014044 432.6 ns/op
12+
BenchmarkRouteResponse-14 16904 66540 ns/op
13+
BenchmarkSave-14 6507 177113 ns/op
14+
BenchmarkLoad-14 1258 937961 ns/op
15+
```
16+
17+
### After Bench
18+
19+
```sh
20+
goos: darwin
21+
goarch: arm64
22+
pkg: github.com/smartcontractkit/chainlink-testing-framework/parrot
23+
cpu: Apple M3 Max
24+
BenchmarkRegisterRoute-14 2144967 605.9 ns/op
25+
BenchmarkRouteResponse-14 18518 63014 ns/op
26+
BenchmarkGetRoutes-14 14031 262574 ns/op
27+
BenchmarkSave-14 6404 185332 ns/op
28+
BenchmarkLoad-14 1012 1170008 ns/op
29+
```

parrot/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Default test log level (can be overridden)
1+
# Default test log level is none
22
PARROT_TEST_LOG_LEVEL ?= ""
3-
4-
# Pass TEST_LOG_LEVEL as a flag to go test
53
TEST_ARGS ?= -testLogLevel=$(PARROT_TEST_LOG_LEVEL)
64

5+
TEST_TIMEOUT ?= 30s
6+
77
.PHONY: lint
88
lint:
99
golangci-lint --color=always run ./... --fix -v
@@ -12,17 +12,17 @@ lint:
1212
test:
1313
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
1414
set -euo pipefail
15-
go test $(TEST_ARGS) -json -cover -coverprofile cover.out -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
15+
go test $(TEST_ARGS) -json -timeout $(TEST_TIMEOUT) -cover -coverprofile cover.out -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
1616

1717
.PHONY: test_race
1818
test_race:
1919
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
2020
set -euo pipefail
21-
go test $(TEST_ARGS) -json -cover -count=1 -race -coverprofile cover.out -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
21+
go test $(TEST_ARGS) -json -timeout $(TEST_TIMEOUT) -cover -count=1 -race -coverprofile cover.out -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
2222

2323
.PHONY: test_unit
2424
test_unit:
25-
go test $(TEST_ARGS) -coverprofile cover.out ./...
25+
go test $(TEST_ARGS) -timeout $(TEST_TIMEOUT) -coverprofile cover.out ./...
2626

2727
.PHONY: bench
2828
bench:

parrot/cmd/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func main() {
4242
if json {
4343
options = append(options, parrot.WithJSONLogs())
4444
}
45+
options = append(options, parrot.WithRecorders(recorders...))
4546

4647
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
4748
defer cancel()
@@ -51,13 +52,6 @@ func main() {
5152
return err
5253
}
5354

54-
for _, r := range recorders {
55-
err = p.Record(r)
56-
if err != nil {
57-
return err
58-
}
59-
}
60-
6155
c := make(chan os.Signal, 1)
6256
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
6357
<-c

parrot/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var (
1818
ErrInvalidRecorderURL = errors.New("invalid recorder URL")
1919
ErrRecorderNotFound = errors.New("recorder not found")
2020

21-
ErrServerShutdown = errors.New("parrot is already asleep")
21+
ErrServerShutdown = errors.New("parrot is already asleep")
22+
ErrServerUnhealthy = errors.New("parrot is unhealthy")
2223
)
2324

2425
// Custom error type to help add more detail to base errors

parrot/examples_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ func ExampleServer_Register_internal() {
5555
fmt.Println(len(routes))
5656

5757
// Delete the route
58-
err = p.Delete(route.ID())
59-
if err != nil {
60-
panic(err)
61-
}
58+
p.Delete(route)
6259

6360
// Get all routes from the parrot instance
6461
routes = p.Routes()
@@ -98,7 +95,7 @@ func ExampleServer_Register_external() {
9895
RawResponseBody: "Squawk",
9996
ResponseStatusCode: http.StatusOK,
10097
}
101-
resp, err := client.R().SetBody(route).Post("/routes")
98+
resp, err := client.R().SetBody(route).Post(parrot.RoutesRoute)
10299
if err != nil {
103100
panic(err)
104101
}
@@ -107,7 +104,7 @@ func ExampleServer_Register_external() {
107104

108105
// Get all routes from the parrot server
109106
routes := make([]*parrot.Route, 0)
110-
resp, err = client.R().SetResult(&routes).Get("/routes")
107+
resp, err = client.R().SetResult(&routes).Get(parrot.RoutesRoute)
111108
if err != nil {
112109
panic(err)
113110
}
@@ -116,7 +113,7 @@ func ExampleServer_Register_external() {
116113
fmt.Println(len(routes))
117114

118115
// Delete the route
119-
resp, err = client.R().SetBody(route).Delete("/routes")
116+
resp, err = client.R().SetBody(route).Delete(parrot.RoutesRoute)
120117
if err != nil {
121118
panic(err)
122119
}
@@ -125,7 +122,7 @@ func ExampleServer_Register_external() {
125122

126123
// Get all routes from the parrot server
127124
routes = make([]*parrot.Route, 0)
128-
resp, err = client.R().SetResult(&routes).Get("/routes")
125+
resp, err = client.R().SetResult(&routes).Get(parrot.RoutesRoute)
129126
if err != nil {
130127
panic(err)
131128
}
@@ -239,7 +236,7 @@ func ExampleRecorder_external() {
239236
}
240237

241238
// Register the route with the parrot instance
242-
resp, err := client.R().SetBody(route).Post("/routes")
239+
resp, err := client.R().SetBody(route).Post(parrot.RoutesRoute)
243240
if err != nil {
244241
panic(err)
245242
}
@@ -256,7 +253,7 @@ func ExampleRecorder_external() {
256253
}
257254

258255
// Register the recorder with the parrot instance
259-
resp, err = client.R().SetBody(recorder).Post("/record")
256+
resp, err = client.R().SetBody(recorder).Post(parrot.RecorderRoute)
260257
if err != nil {
261258
panic(err)
262259
}
@@ -301,7 +298,7 @@ func waitForParrotServer(client *resty.Client, timeoutDur time.Duration) {
301298
for { // Wait for the parrot server to start
302299
select {
303300
case <-ticker.C:
304-
resp, err := client.R().Get("/health")
301+
resp, err := client.R().Get(parrot.HealthRoute)
305302
if err != nil {
306303
continue
307304
}

parrot/go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ module github.com/smartcontractkit/chainlink-testing-framework/parrot
33
go 1.23.4
44

55
require (
6+
github.com/go-chi/chi v1.5.5
67
github.com/go-resty/resty/v2 v2.16.3
78
github.com/google/uuid v1.6.0
89
github.com/rs/zerolog v1.33.0
910
github.com/spf13/cobra v1.8.1
1011
github.com/stretchr/testify v1.9.0
12+
golang.org/x/sync v0.10.0
1113
)
1214

1315
require (
1416
github.com/davecgh/go-spew v1.1.1 // indirect
1517
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1618
github.com/mattn/go-colorable v0.1.13 // indirect
17-
github.com/mattn/go-isatty v0.0.19 // indirect
19+
github.com/mattn/go-isatty v0.0.20 // indirect
1820
github.com/pmezard/go-difflib v1.0.0 // indirect
1921
github.com/rs/xid v1.5.0 // indirect
2022
github.com/spf13/pflag v1.0.5 // indirect

parrot/go.sum

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
22
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
6+
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
57
github.com/go-resty/resty/v2 v2.16.3 h1:zacNT7lt4b8M/io2Ahj6yPypL7bqx9n1iprfQuodV+E=
68
github.com/go-resty/resty/v2 v2.16.3/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
79
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@@ -12,8 +14,9 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
1214
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
1315
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
1416
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
15-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
1617
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
18+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
19+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
1720
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1821
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1922
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -30,6 +33,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
3033
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3134
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
3235
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
36+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
37+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3338
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3439
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3540
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)