Skip to content

Commit 5c2f4bd

Browse files
committed
More tests on recorders
1 parent 47685ce commit 5c2f4bd

File tree

10 files changed

+508
-189
lines changed

10 files changed

+508
-189
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ import_keys_test.go
8080
tag.py
8181

8282
parrot/*.json
83-
parrot/*.log
83+
parrot/*.log
84+
# Executable
85+
parrot/parrot

parrot/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ test_unit:
2626

2727
.PHONY: bench
2828
bench:
29-
go test $(TEST_ARGS) -bench=. -run=^$$ ./...
29+
go test $(TEST_ARGS) -bench=. -run=^$$ ./...
30+
31+
.PHONY: build
32+
build:
33+
go build -o ./parrot ./cmd

parrot/cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/signal"
77
"syscall"
8+
"time"
89

910
"github.com/rs/zerolog"
1011
"github.com/rs/zerolog/log"
@@ -42,7 +43,7 @@ func main() {
4243
options = append(options, parrot.WithJSONLogs())
4344
}
4445

45-
ctx, cancel := context.WithCancel(context.Background())
46+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
4647
defer cancel()
4748

4849
p, err := parrot.Wake(options...)
@@ -62,7 +63,7 @@ func main() {
6263
<-c
6364
err = p.Shutdown(ctx)
6465
if err != nil {
65-
log.Error().Err(err).Msg("error shutting down server")
66+
log.Error().Err(err).Msg("Error putting parrot to sleep")
6667
}
6768
return nil
6869
},

parrot/errors.go

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

21-
ErrParrotAsleep = errors.New("parrot is asleep")
21+
ErrServerShutdown = errors.New("parrot is already asleep")
2222
)
2323

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

parrot/examples_test.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@ package parrot_test
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"net/http"
7+
"os"
88

99
"github.com/go-resty/resty/v2"
1010
"github.com/rs/zerolog"
1111
"github.com/smartcontractkit/chainlink-testing-framework/parrot"
1212
)
1313

1414
func ExampleRegister() {
15-
// Create a new parrot instance with no logging
16-
p, err := parrot.Wake(parrot.WithLogLevel(zerolog.NoLevel))
15+
// Create a new parrot instance with no logging and a custom save file
16+
saveFile := "register_example.json"
17+
p, err := parrot.Wake(parrot.WithLogLevel(zerolog.NoLevel), parrot.WithSaveFile(saveFile))
1718
if err != nil {
1819
panic(err)
1920
}
20-
defer func() {
21-
err = p.Shutdown(context.Background())
21+
defer func() { // Cleanup the parrot instance
22+
err = p.Shutdown(context.Background()) // Gracefully shutdown the parrot instance
2223
if err != nil {
2324
panic(err)
2425
}
26+
p.WaitShutdown() // Wait for the parrot instance to shutdown. Usually unnecessary, but we want to clean up the save file
27+
os.Remove(saveFile) // Cleanup the save file for the example
2528
}()
2629

2730
// Create a new route /test that will return a 200 status code with a text/plain response body of "Squawk"
2831
route := &parrot.Route{
2932
Method: http.MethodGet,
3033
Path: "/test",
3134
RawResponseBody: "Squawk",
32-
ResponseStatusCode: 200,
35+
ResponseStatusCode: http.StatusOK,
3336
}
3437

3538
// Register the route with the parrot instance
@@ -43,27 +46,27 @@ func ExampleRegister() {
4346
if err != nil {
4447
panic(err)
4548
}
46-
defer resp.Body.Close()
47-
48-
fmt.Println(resp.StatusCode)
49-
body, _ := io.ReadAll(resp.Body)
50-
fmt.Println(string(body))
49+
fmt.Println(resp.StatusCode())
50+
fmt.Println(string(resp.Body()))
5151
// Output:
5252
// 200
5353
// Squawk
5454
}
5555

5656
func ExampleRoute() {
5757
// Run the parrot server as a separate instance, like in a Docker container
58-
p, err := parrot.Wake(parrot.WithPort(9090), parrot.WithLogLevel(zerolog.NoLevel))
58+
saveFile := "route_example.json"
59+
p, err := parrot.Wake(parrot.WithPort(9090), parrot.WithLogLevel(zerolog.NoLevel), parrot.WithSaveFile(saveFile))
5960
if err != nil {
6061
panic(err)
6162
}
62-
defer func() {
63-
err = p.Shutdown(context.Background())
63+
defer func() { // Cleanup the parrot instance
64+
err = p.Shutdown(context.Background()) // Gracefully shutdown the parrot instance
6465
if err != nil {
6566
panic(err)
6667
}
68+
p.WaitShutdown() // Wait for the parrot instance to shutdown. Usually unnecessary, but we want to clean up the save file
69+
os.Remove(saveFile) // Cleanup the save file for the example
6770
}()
6871

6972
// Code that calls the parrot server from another service
@@ -75,7 +78,7 @@ func ExampleRoute() {
7578
Method: http.MethodGet,
7679
Path: "/test",
7780
RawResponseBody: "Squawk",
78-
ResponseStatusCode: 200,
81+
ResponseStatusCode: http.StatusOK,
7982
}
8083
resp, err := client.R().SetBody(route).Post("http://localhost:9090/routes")
8184
if err != nil {
@@ -123,15 +126,18 @@ func ExampleRoute() {
123126
}
124127

125128
func ExampleRecorder() {
126-
p, err := parrot.Wake(parrot.WithLogLevel(zerolog.NoLevel))
129+
saveFile := "recorder_example.json"
130+
p, err := parrot.Wake(parrot.WithLogLevel(zerolog.NoLevel), parrot.WithSaveFile(saveFile))
127131
if err != nil {
128132
panic(err)
129133
}
130-
defer func() {
131-
err = p.Shutdown(context.Background())
134+
defer func() { // Cleanup the parrot instance
135+
err = p.Shutdown(context.Background()) // Gracefully shutdown the parrot instance
132136
if err != nil {
133137
panic(err)
134138
}
139+
p.WaitShutdown() // Wait for the parrot instance to shutdown. Usually unnecessary, but we want to clean up the save file
140+
os.Remove(saveFile) // Cleanup the save file for the example
135141
}()
136142

137143
// Create a new recorder
@@ -161,11 +167,10 @@ func ExampleRecorder() {
161167

162168
// Call the route
163169
go func() {
164-
resp, err := p.Call(http.MethodGet, "/test")
170+
_, err := p.Call(http.MethodGet, "/test")
165171
if err != nil {
166172
panic(err)
167173
}
168-
defer resp.Body.Close()
169174
}()
170175

171176
// Record the route call

0 commit comments

Comments
 (0)