Skip to content

Commit

Permalink
fix: wait for ngrok to be up before connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
ViRb3 committed Mar 9, 2021
1 parent 420c1f5 commit c2dac03
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
22 changes: 2 additions & 20 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package main
import (
"archive/tar"
"bytes"
"context"
"fmt"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
Expand Down Expand Up @@ -94,33 +92,17 @@ func TestMain(m *testing.M) {
storage.Load()

go startWorkflowServer(workflowPort)
if err := waitForServer(fmt.Sprintf("http://localhost:%d", workflowPort), 5*time.Second); err != nil {
if err := util.WaitForServer(fmt.Sprintf("http://localhost:%d", workflowPort), 5*time.Second); err != nil {
log.Fatalln(err)
}

go serve(serveHost, servePort)
if err := waitForServer(fmt.Sprintf("http://localhost:%d", servePort), 5*time.Second); err != nil {
if err := util.WaitForServer(fmt.Sprintf("http://localhost:%d", servePort), 5*time.Second); err != nil {
log.Fatalln(err)
}
m.Run()
}

func waitForServer(url string, timeout time.Duration) error {
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
for {
select {
case <-ctx.Done():
return errors.New("internet check timed out")
default:
if _, err := http.Get(url); err == nil {
return nil
}
time.Sleep(100 * time.Millisecond)
}
}
}

var triggerHit = false
var secretsHit = false

Expand Down
7 changes: 6 additions & 1 deletion ngrok/ngrok.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
"github.com/pkg/errors"
"ios-signer-service/util"
"strings"
"time"
)

func GetPublicUrl(ngrokPort uint64, proto string) (string, error) {
ngrokUrl := fmt.Sprintf("http://localhost:%d/api/tunnels", ngrokPort)
if err := util.WaitForServer(ngrokUrl, 10*time.Second); err != nil {
return "", errors.WithMessage(err, "connecting to ngrok")
}
var tunnels Tunnels
response, err := sling.New().Get(fmt.Sprintf("http://localhost:%d/api/tunnels", ngrokPort)).ReceiveSuccess(&tunnels)
response, err := sling.New().Get(ngrokUrl).ReceiveSuccess(&tunnels)
if err != nil {
return "", err
}
Expand Down
19 changes: 19 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package util

import (
"context"
"fmt"
"github.com/pkg/errors"
"net/http"
"net/url"
"path"
"path/filepath"
"time"
)

func SafeJoin(basePath string, unsafePath ...string) string {
Expand Down Expand Up @@ -42,3 +45,19 @@ func Check2xxCode(code int) error {
}
return nil
}

func WaitForServer(url string, timeout time.Duration) error {
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
for {
select {
case <-ctx.Done():
return errors.New("reaching server timed out: " + url)
default:
if _, err := http.Get(url); err == nil {
return nil
}
time.Sleep(100 * time.Millisecond)
}
}
}

0 comments on commit c2dac03

Please sign in to comment.