Skip to content

Commit 52c2561

Browse files
committed
fix(ping): fix parsing logic for fallback
Signed-off-by: Yagiz Degirmenci <yagizcanilbey1903@gmail.com>
1 parent 0cef18d commit 52c2561

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

pkg/dstp/dstp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build integration || darwin
1+
//go:build integration || darwin || !windows
22

33
package dstp
44

pkg/ping/ping.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ func runPingFallback(ctx context.Context, addr common.Address, count int, timeou
7171
return common.Output(""), err
7272
}
7373

74-
return common.Output(po.AvgRTT), nil
74+
return common.Output(po.AvgRTT + "ms"), nil
7575
}
7676

7777
func executeCommand(shell, command string) (string, error) {
7878
var errb bytes.Buffer
79-
var out bytes.Buffer
79+
var out string
8080

8181
cmd := exec.Command(shell, "-c", command)
8282
cmd.Stderr = &errb
@@ -91,14 +91,14 @@ func executeCommand(shell, command string) (string, error) {
9191

9292
scanner := bufio.NewScanner(stdout)
9393
for scanner.Scan() {
94-
out.Write(scanner.Bytes())
94+
out += scanner.Text() + "\n"
9595
}
9696

9797
if err := cmd.Wait(); err != nil {
9898
return "", fmt.Errorf("got error: %v, stderr: %v", err, errb.String())
9999
}
100100

101-
return out.String(), nil
101+
return out, nil
102102
}
103103

104104
type pingOutput struct {
@@ -112,7 +112,7 @@ type pingOutput struct {
112112

113113
var (
114114
RequestTimeoutError = fmt.Errorf("requests timed out")
115-
PacketLossError = fmt.Errorf("100.0%% packet loss")
115+
PacketLossError = fmt.Errorf("timeout error: 100.0%% packet loss")
116116
)
117117

118118
// parsePingOutput parses the output of ping by parsing the stdout
@@ -134,14 +134,14 @@ func parsePingOutput(out string) (pingOutput, error) {
134134

135135
for _, line := range lines {
136136
switch {
137-
case strings.Contains(line, "packets transmitted") && strings.Contains(line, "packets received"):
137+
case strings.Contains(line, "packets transmitted"):
138138
arr := strings.Split(line, ",")
139-
139+
fmt.Println(arr)
140140
if len(arr) != 3 {
141141
continue
142142
}
143143

144-
po.MinRTT, po.AvgRTT, po.MaxRTT = arr[0], arr[1], arr[2]
144+
po.PacketTransmitted, po.PacketReceived, po.PacketLoss = arr[0], arr[1], arr[2]
145145

146146
case strings.Contains(line, "round-trip min/avg/max"):
147147
l := strings.ReplaceAll(line, " = ", " ")

pkg/ping/ping_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build integration
2+
3+
package ping
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"github.com/ycd/dstp/pkg/common"
9+
"testing"
10+
)
11+
12+
func TestPingFallback(t *testing.T) {
13+
out, err := runPingFallback(context.Background(), common.Address("8.8.8.8"), 3, 10)
14+
if err != nil {
15+
t.Fatal(err.Error())
16+
}
17+
18+
fmt.Println(out.String())
19+
}

0 commit comments

Comments
 (0)