Skip to content

Commit

Permalink
Add integration test for ThrottleNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Nov 9, 2023
1 parent 2fe17ac commit 0c89145
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
98 changes: 98 additions & 0 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"image/png"
"io"
"net/http"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1079,3 +1081,99 @@ func TestPageWaitForSelector(t *testing.T) {
})
}
}

func TestPageThrottleNetwork(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
networkProfile common.NetworkProfile
wantMinRoundTripDuration int64
}{
{
name: "none",
networkProfile: common.NetworkProfile{
Latency: 0,
DownloadThroughput: -1,
UploadThroughput: -1,
},
},
{
// In the ping.html file, an async ping request is made. The time it takes
// to perform the roundtrip of calling ping and getting the response is
// measured and used to assert that Latency has been correctly used.
name: "latency",
networkProfile: common.NetworkProfile{
Latency: 100,
DownloadThroughput: -1,
UploadThroughput: -1,
},
wantMinRoundTripDuration: 100,
},
{
// In the ping.html file, an async ping request is made, the ping response
// returns the request body (around a 1MB). The time it takes to perform the
// roundtrip of calling ping and getting the response body is measured and
// used to assert that DownloadThroughput has been correctly used.
name: "download_throughput",
networkProfile: common.NetworkProfile{
Latency: 0,
DownloadThroughput: 1000,
UploadThroughput: -1,
},
wantMinRoundTripDuration: 1000,
},
{
// In the ping.html file, an async ping request is made with around a 1MB body.
// The time it takes to perform the roundtrip of calling ping is measured
// and used to assert that UploadThroughput has been correctly used.
name: "upload_throughput",
networkProfile: common.NetworkProfile{
Latency: 0,
DownloadThroughput: -1,
UploadThroughput: 1000,
},
wantMinRoundTripDuration: 1000,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withFileServer())

tb.withHandler("/ping", func(w http.ResponseWriter, req *http.Request) {
defer func() {
err := req.Body.Close()
require.NoError(t, err)
}()
bb, err := io.ReadAll(req.Body)
require.NoError(t, err)

fmt.Fprint(w, string(bb))
})

page := tb.NewPage(nil)

err := page.ThrottleNetwork(tc.networkProfile)
require.NoError(t, err)

_, err = page.Goto(tb.staticURL("ping.html"), nil)
require.NoError(t, err)

selector := `div[id="result"]`

// result selector only appears once the page gets a response
// from the async ping request.
_, err = page.WaitForSelector(selector, nil)
require.NoError(t, err)

resp := page.InnerText(selector, nil)
ms, err := strconv.ParseInt(resp, 10, 64)
require.NoError(t, err)
assert.GreaterOrEqual(t, ms, tc.wantMinRoundTripDuration)
})
}
}
29 changes: 29 additions & 0 deletions tests/static/ping.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<title>Ping duration test</title>
</head>
<body>
<div id="waiting">NA</div>
<script>
asd()

async function asd() {
var sendDate = (new Date()).getTime();

var receiveDate = 0;
await fetch('/ping', {
method: 'POST',
body: JSON.stringify('0'.repeat(1024))
})
.then(response => response.json())
.then(data => {
receiveDate = (new Date()).getTime();
})

var responseTimeMs = receiveDate - sendDate;

document.getElementById("waiting").innerHTML = `<div id="result">${responseTimeMs}</div>`;
}
</script>
</body>
</html>

0 comments on commit 0c89145

Please sign in to comment.