This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p2p/simulations/adapters: fix websocket log line parsing in exec adap…
…ter (#16667)
- Loading branch information
Showing
3 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package adapters | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"io" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// wsAddrPattern is a regex used to read the WebSocket address from the node's | ||
// log | ||
var wsAddrPattern = regexp.MustCompile(`ws://[\d.:]+`) | ||
|
||
func matchWSAddr(str string) (string, bool) { | ||
if !strings.Contains(str, "WebSocket endpoint opened") { | ||
return "", false | ||
} | ||
|
||
return wsAddrPattern.FindString(str), true | ||
} | ||
|
||
// findWSAddr scans through reader r, looking for the log entry with | ||
// WebSocket address information. | ||
func findWSAddr(r io.Reader, timeout time.Duration) (string, error) { | ||
ch := make(chan string) | ||
|
||
go func() { | ||
s := bufio.NewScanner(r) | ||
for s.Scan() { | ||
addr, ok := matchWSAddr(s.Text()) | ||
if ok { | ||
ch <- addr | ||
} | ||
} | ||
close(ch) | ||
}() | ||
|
||
var wsAddr string | ||
select { | ||
case wsAddr = <-ch: | ||
if wsAddr == "" { | ||
return "", errors.New("empty result") | ||
} | ||
case <-time.After(timeout): | ||
return "", errors.New("timed out") | ||
} | ||
|
||
return wsAddr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package adapters | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFindWSAddr(t *testing.T) { | ||
line := `t=2018-05-02T19:00:45+0200 lvl=info msg="WebSocket endpoint opened" node.id=26c65a606d1125a44695bc08573190d047152b6b9a776ccbbe593e90f91444d9c1ebdadac6a775ad9fdd0923468a1d698ed3a842c1fb89c1bc0f9d4801f8c39c url=ws://127.0.0.1:59975` | ||
buf := bytes.NewBufferString(line) | ||
got, err := findWSAddr(buf, 10*time.Second) | ||
if err != nil { | ||
t.Fatalf("Failed to find addr: %v", err) | ||
} | ||
expected := `ws://127.0.0.1:59975` | ||
|
||
if got != expected { | ||
t.Fatalf("Expected to get '%s', but got '%s'", expected, got) | ||
} | ||
} |