@@ -12,7 +12,7 @@ import (
1212 "github.com/smartcontractkit/chainlink-testing-framework/parrot"
1313)
1414
15- func ExampleServer_Register_internal () {
15+ func ExampleServer_internal () {
1616 // Create a new parrot instance with no logging and a custom save file
1717 saveFile := "register_example.json"
1818 p , err := parrot .Wake (parrot .WithLogLevel (zerolog .NoLevel ), parrot .WithSaveFile (saveFile ))
@@ -36,6 +36,8 @@ func ExampleServer_Register_internal() {
3636 ResponseStatusCode : http .StatusOK ,
3737 }
3838
39+ waitForParrotServerInternal (p , time .Second ) // Wait for the parrot server to start
40+
3941 // Register the route with the parrot instance
4042 err = p .Register (route )
4143 if err != nil {
@@ -55,10 +57,7 @@ func ExampleServer_Register_internal() {
5557 fmt .Println (len (routes ))
5658
5759 // Delete the route
58- err = p .Delete (route .ID ())
59- if err != nil {
60- panic (err )
61- }
60+ p .Delete (route )
6261
6362 // Get all routes from the parrot instance
6463 routes = p .Routes ()
@@ -70,7 +69,7 @@ func ExampleServer_Register_internal() {
7069 // 0
7170}
7271
73- func ExampleServer_Register_external () {
72+ func ExampleServer_external () {
7473 var (
7574 saveFile = "route_example.json"
7675 port = 9090
@@ -89,7 +88,7 @@ func ExampleServer_Register_external() {
8988 client := resty .New ()
9089 client .SetBaseURL (fmt .Sprintf ("http://localhost:%d" , port )) // The URL of the parrot server
9190
92- waitForParrotServer (client , time .Second ) // Wait for the parrot server to start
91+ waitForParrotServerExternal (client , time .Second ) // Wait for the parrot server to start
9392
9493 // Register a new route /test that will return a 200 status code with a text/plain response body of "Squawk"
9594 route := & parrot.Route {
@@ -98,7 +97,7 @@ func ExampleServer_Register_external() {
9897 RawResponseBody : "Squawk" ,
9998 ResponseStatusCode : http .StatusOK ,
10099 }
101- resp , err := client .R ().SetBody (route ).Post ("/routes" )
100+ resp , err := client .R ().SetBody (route ).Post (parrot . RoutesRoute )
102101 if err != nil {
103102 panic (err )
104103 }
@@ -107,7 +106,7 @@ func ExampleServer_Register_external() {
107106
108107 // Get all routes from the parrot server
109108 routes := make ([]* parrot.Route , 0 )
110- resp , err = client .R ().SetResult (& routes ).Get ("/routes" )
109+ resp , err = client .R ().SetResult (& routes ).Get (parrot . RoutesRoute )
111110 if err != nil {
112111 panic (err )
113112 }
@@ -116,7 +115,7 @@ func ExampleServer_Register_external() {
116115 fmt .Println (len (routes ))
117116
118117 // Delete the route
119- resp , err = client .R ().SetBody (route ).Delete ("/routes" )
118+ resp , err = client .R ().SetBody (route ).Delete (parrot . RoutesRoute )
120119 if err != nil {
121120 panic (err )
122121 }
@@ -125,7 +124,7 @@ func ExampleServer_Register_external() {
125124
126125 // Get all routes from the parrot server
127126 routes = make ([]* parrot.Route , 0 )
128- resp , err = client .R ().SetResult (& routes ).Get ("/routes" )
127+ resp , err = client .R ().SetResult (& routes ).Get (parrot . RoutesRoute )
129128 if err != nil {
130129 panic (err )
131130 }
@@ -161,6 +160,8 @@ func ExampleRecorder_internal() {
161160 panic (err )
162161 }
163162
163+ waitForParrotServerInternal (p , time .Second ) // Wait for the parrot server to start
164+
164165 // Register the recorder with the parrot instance
165166 err = p .Record (recorder .URL ())
166167 if err != nil {
@@ -228,7 +229,7 @@ func ExampleRecorder_external() {
228229 client := resty .New ()
229230 client .SetBaseURL (fmt .Sprintf ("http://localhost:%d" , port )) // The URL of the parrot server
230231
231- waitForParrotServer (client , time .Second ) // Wait for the parrot server to start
232+ waitForParrotServerExternal (client , time .Second ) // Wait for the parrot server to start
232233
233234 // Register a new route /test that will return a 200 status code with a text/plain response body of "Squawk"
234235 route := & parrot.Route {
@@ -239,7 +240,7 @@ func ExampleRecorder_external() {
239240 }
240241
241242 // Register the route with the parrot instance
242- resp , err := client .R ().SetBody (route ).Post ("/routes" )
243+ resp , err := client .R ().SetBody (route ).Post (parrot . RoutesRoute )
243244 if err != nil {
244245 panic (err )
245246 }
@@ -256,7 +257,7 @@ func ExampleRecorder_external() {
256257 }
257258
258259 // Register the recorder with the parrot instance
259- resp , err = client .R ().SetBody (recorder ).Post ("/record" )
260+ resp , err = client .R ().SetBody (recorder ).Post (parrot . RecorderRoute )
260261 if err != nil {
261262 panic (err )
262263 }
@@ -293,15 +294,15 @@ func ExampleRecorder_external() {
293294 // Squawk
294295}
295296
296- // waitForParrotServer checks the parrot server health endpoint until it returns a 200 status code or the timeout is reached
297- func waitForParrotServer (client * resty.Client , timeoutDur time.Duration ) {
297+ // waitForParrotServerExternal checks the parrot server health endpoint until it returns a 200 status code or the timeout is reached
298+ func waitForParrotServerExternal (client * resty.Client , timeoutDur time.Duration ) {
298299 ticker := time .NewTicker (50 * time .Millisecond )
299300 defer ticker .Stop ()
300301 timeout := time .NewTimer (timeoutDur )
301302 for { // Wait for the parrot server to start
302303 select {
303304 case <- ticker .C :
304- resp , err := client .R ().Get ("/health" )
305+ resp , err := client .R ().Get (parrot . HealthRoute )
305306 if err != nil {
306307 continue
307308 }
@@ -313,3 +314,19 @@ func waitForParrotServer(client *resty.Client, timeoutDur time.Duration) {
313314 }
314315 }
315316}
317+
318+ func waitForParrotServerInternal (p * parrot.Server , timeoutDur time.Duration ) {
319+ ticker := time .NewTicker (50 * time .Millisecond )
320+ defer ticker .Stop ()
321+ timeout := time .NewTimer (timeoutDur )
322+ for { // Wait for the parrot server to start
323+ select {
324+ case <- ticker .C :
325+ if err := p .Healthy (); err == nil {
326+ return
327+ }
328+ case <- timeout .C :
329+ panic ("timeout waiting for parrot server to start" )
330+ }
331+ }
332+ }
0 commit comments