Skip to content

Commit c4a4613

Browse files
divanfjl
authored andcommitted
p2p/simulations/adapters: fix websocket log line parsing in exec adapter (#16667)
1 parent fedae95 commit c4a4613

3 files changed

Lines changed: 75 additions & 23 deletions

File tree

‎p2p/simulations/adapters/exec.go‎

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package adapters
1818

1919
import (
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

‎p2p/simulations/adapters/ws.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

0 commit comments

Comments
 (0)