@@ -3,33 +3,36 @@ package parrot_test
33import (
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
1414func 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
5656func 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
125128func 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