File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717package adapters
1818
1919import (
20- "bufio"
2120 "context"
2221 "crypto/ecdsa"
2322 "encoding/json"
@@ -29,7 +28,6 @@ import (
2928 "os/exec"
3029 "os/signal"
3130 "path/filepath"
32- "regexp"
3331 "strings"
3432 "sync"
3533 "syscall"
@@ -150,10 +148,6 @@ func (n *ExecNode) Client() (*rpc.Client, error) {
150148 return n .client , nil
151149}
152150
153- // wsAddrPattern is a regex used to read the WebSocket address from the node's
154- // log
155- var wsAddrPattern = regexp .MustCompile (`ws://[\d.:]+` )
156-
157151// Start exec's the node passing the ID and service as command line arguments
158152// and the node config encoded as JSON in the _P2P_NODE_CONFIG environment
159153// variable
@@ -196,23 +190,9 @@ func (n *ExecNode) Start(snapshots map[string][]byte) (err error) {
196190 n .Cmd = cmd
197191
198192 // read the WebSocket address from the stderr logs
199- var wsAddr string
200- wsAddrC := make (chan string )
201- go func () {
202- s := bufio .NewScanner (stderrR )
203- for s .Scan () {
204- if strings .Contains (s .Text (), "WebSocket endpoint opened:" ) {
205- wsAddrC <- wsAddrPattern .FindString (s .Text ())
206- }
207- }
208- }()
209- select {
210- case wsAddr = <- wsAddrC :
211- if wsAddr == "" {
212- return errors .New ("failed to read WebSocket address from stderr" )
213- }
214- case <- time .After (10 * time .Second ):
215- return errors .New ("timed out waiting for WebSocket address on stderr" )
193+ wsAddr , err := findWSAddr (stderrR , 10 * time .Second )
194+ if err != nil {
195+ return fmt .Errorf ("error getting WebSocket address: %s" , err )
216196 }
217197
218198 // create the RPC client and load the node info
Original file line number Diff line number Diff line change 1+ package adapters
2+
3+ import (
4+ "bufio"
5+ "errors"
6+ "io"
7+ "regexp"
8+ "strings"
9+ "time"
10+ )
11+
12+ // wsAddrPattern is a regex used to read the WebSocket address from the node's
13+ // log
14+ var wsAddrPattern = regexp .MustCompile (`ws://[\d.:]+` )
15+
16+ func matchWSAddr (str string ) (string , bool ) {
17+ if ! strings .Contains (str , "WebSocket endpoint opened" ) {
18+ return "" , false
19+ }
20+
21+ return wsAddrPattern .FindString (str ), true
22+ }
23+
24+ // findWSAddr scans through reader r, looking for the log entry with
25+ // WebSocket address information.
26+ func findWSAddr (r io.Reader , timeout time.Duration ) (string , error ) {
27+ ch := make (chan string )
28+
29+ go func () {
30+ s := bufio .NewScanner (r )
31+ for s .Scan () {
32+ addr , ok := matchWSAddr (s .Text ())
33+ if ok {
34+ ch <- addr
35+ }
36+ }
37+ close (ch )
38+ }()
39+
40+ var wsAddr string
41+ select {
42+ case wsAddr = <- ch :
43+ if wsAddr == "" {
44+ return "" , errors .New ("empty result" )
45+ }
46+ case <- time .After (timeout ):
47+ return "" , errors .New ("timed out" )
48+ }
49+
50+ return wsAddr , nil
51+ }
Original file line number Diff line number Diff line change 1+ package adapters
2+
3+ import (
4+ "bytes"
5+ "testing"
6+ "time"
7+ )
8+
9+ func TestFindWSAddr (t * testing.T ) {
10+ line := `t=2018-05-02T19:00:45+0200 lvl=info msg="WebSocket endpoint opened" node.id=26c65a606d1125a44695bc08573190d047152b6b9a776ccbbe593e90f91444d9c1ebdadac6a775ad9fdd0923468a1d698ed3a842c1fb89c1bc0f9d4801f8c39c url=ws://127.0.0.1:59975`
11+ buf := bytes .NewBufferString (line )
12+ got , err := findWSAddr (buf , 10 * time .Second )
13+ if err != nil {
14+ t .Fatalf ("Failed to find addr: %v" , err )
15+ }
16+ expected := `ws://127.0.0.1:59975`
17+
18+ if got != expected {
19+ t .Fatalf ("Expected to get '%s', but got '%s'" , expected , got )
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments