-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: Re-implement wrap-websocket into agent
Showing
11 changed files
with
187 additions
and
98 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
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
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
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 |
---|---|---|
@@ -1,40 +1,72 @@ | ||
// const { notIOS, notSafari } = require('../../../tools/browser-matcher/common-matchers.mjs') | ||
// const { testSupportMetricsRequest } = require('../../../tools/testing-server/utils/expect-tests') | ||
const { notIOS, notSafari } = require('../../../tools/browser-matcher/common-matchers.mjs') | ||
const { testSupportMetricsRequest, testErrorsRequest } = require('../../../tools/testing-server/utils/expect-tests') | ||
|
||
describe('WebSocket supportability metrics', () => { | ||
/** Safari and iOS safari are blocked from connecting to the websocket protocol on LT, which throws socket errors instead of connecting and capturing the expected payloads. | ||
* validated that this works locally for these envs */ | ||
// it.withBrowsersMatching([notSafari, notIOS])('should capture expected SMs', async () => { | ||
// const supportabilityMetricsRequest = await browser.testHandle.createNetworkCaptures('bamServer', { test: testSupportMetricsRequest }) | ||
// const url = await browser.testHandle.assetURL('websockets.html') | ||
|
||
// await browser.url(url) | ||
// .then(() => browser.waitForAgentLoad()) | ||
// .then(() => browser.refresh()) | ||
|
||
// const [sms] = await supportabilityMetricsRequest.waitForResult({ totalCount: 1 }) | ||
// const smPayload = sms.request.body.sm | ||
// const smTags = ['New', 'Open', 'Send', 'Message', 'Close-Clean'] | ||
|
||
// smTags.forEach(expectedSm => { | ||
// const ms = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/Ms`) | ||
// const msSinceClassInit = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/MsSinceClassInit`) | ||
// const bytes = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/Bytes`) | ||
|
||
// expect(ms).toBeTruthy() | ||
// expect(ms.stats.t).toBeGreaterThan(0) | ||
// expect(ms.stats.c).toEqual(2) | ||
|
||
// expect(msSinceClassInit).toBeTruthy() | ||
// if (expectedSm === 'New') expect(msSinceClassInit.stats.t).toBeLessThanOrEqual(1) | ||
// else expect(msSinceClassInit.stats.t).toBeGreaterThan(0) | ||
// expect(msSinceClassInit.stats.c).toEqual(2) | ||
|
||
// if (['Send', 'Message'].includes(expectedSm)) { | ||
// expect(bytes).toBeTruthy() | ||
// if (expectedSm === 'Send') expect(bytes.stats.t / bytes.stats.c).toBeGreaterThanOrEqual(8) // we are sending about 8 bytes from client to server | ||
// if (expectedSm === 'Message') expect(bytes.stats.t / bytes.stats.c).toBeGreaterThanOrEqual(40) // we are sending about 40 bytes from server to client | ||
// } else expect(bytes).toBeFalsy() | ||
// }) | ||
// }) | ||
it.withBrowsersMatching([notSafari, notIOS])('should capture expected SMs', async () => { | ||
const supportabilityMetricsRequest = await browser.testHandle.createNetworkCaptures('bamServer', { test: testSupportMetricsRequest }) | ||
const url = await browser.testHandle.assetURL('websockets.html') | ||
|
||
await browser.url(url) | ||
.then(() => browser.waitForAgentLoad()) | ||
.then(() => browser.refresh()) | ||
|
||
const [sms] = await supportabilityMetricsRequest.waitForResult({ totalCount: 1 }) | ||
const smPayload = sms.request.body.sm | ||
const smTags = ['New', 'Open', 'Send', 'Message', 'Close-Clean'] | ||
|
||
smTags.forEach(expectedSm => { | ||
const ms = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/Ms`) | ||
const msSinceClassInit = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/MsSinceClassInit`) | ||
const bytes = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/Bytes`) | ||
|
||
expect(ms).toBeTruthy() | ||
expect(ms.stats.t).toBeGreaterThan(0) | ||
expect(ms.stats.c).toEqual(2) | ||
|
||
expect(msSinceClassInit).toBeTruthy() | ||
if (expectedSm === 'New') expect(msSinceClassInit.stats.t).toBeLessThanOrEqual(1) | ||
else expect(msSinceClassInit.stats.t).toBeGreaterThan(0) | ||
expect(msSinceClassInit.stats.c).toEqual(2) | ||
|
||
if (['Send', 'Message'].includes(expectedSm)) { | ||
expect(bytes).toBeTruthy() | ||
if (expectedSm === 'Send') expect(bytes.stats.t / bytes.stats.c).toBeGreaterThanOrEqual(8) // we are sending about 8 bytes from client to server | ||
if (expectedSm === 'Message') expect(bytes.stats.t / bytes.stats.c).toBeGreaterThanOrEqual(40) // we are sending about 40 bytes from server to client | ||
} else expect(bytes).toBeFalsy() | ||
}) | ||
}) | ||
|
||
;['robust-websocket', 'reconnecting-websocket'].forEach((thirdPartyWSWrapper) => { | ||
it('should work with known third-party WS wrapper - ' + thirdPartyWSWrapper, async () => { | ||
const [supportabilityMetricsRequest, errorsRequest] = | ||
await browser.testHandle.createNetworkCaptures('bamServer', [ | ||
{ test: testSupportMetricsRequest }, | ||
{ test: testErrorsRequest } | ||
]) | ||
const url = await browser.testHandle.assetURL(`test-builds/library-wrapper/${thirdPartyWSWrapper}.html`) | ||
|
||
await browser.url(url) | ||
|
||
const [errors, [sms]] = await Promise.all([ | ||
errorsRequest.waitForResult({ timeout: 10000 }), | ||
supportabilityMetricsRequest.waitForResult({ totalCount: 1 }) | ||
]) | ||
// should not have thrown errors | ||
expect(errors.length).toEqual(0) | ||
|
||
const smPayload = sms.request.body.sm | ||
const smTags = ['New', 'Open', 'Send', 'Message', 'Close-Clean'] | ||
|
||
smTags.forEach(expectedSm => { | ||
const ms = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/Ms`) | ||
const msSinceClassInit = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/MsSinceClassInit`) | ||
const bytes = smPayload.find(sm => sm.params.name === `WebSocket/${expectedSm}/Bytes`) | ||
expect(ms).toBeTruthy() | ||
expect(msSinceClassInit).toBeTruthy() | ||
if (['Send', 'Message'].includes(expectedSm))expect(bytes).toBeTruthy() | ||
}) | ||
}) | ||
}) | ||
}) |
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
25 changes: 25 additions & 0 deletions
25
tools/test-builds/library-wrapper/src/reconnecting-websocket.js
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,25 @@ | ||
import { BrowserAgent } from '@newrelic/browser-agent/loaders/browser-agent' | ||
|
||
import ReconnectingWebSocket from 'reconnecting-websocket' | ||
|
||
const opts = { | ||
info: NREUM.info, | ||
init: NREUM.init | ||
} | ||
|
||
new BrowserAgent(opts) | ||
|
||
window.bamServer = NREUM.info.beacon | ||
|
||
const rws = new ReconnectingWebSocket(`ws://${window.bamServer}/websocket`) | ||
|
||
rws.addEventListener('open', () => { | ||
rws.send('hello!') | ||
rws.addEventListener('message', (message) => { | ||
rws.close() | ||
}) | ||
|
||
rws.addEventListener('close', function () { | ||
window.location.reload() | ||
}) | ||
}) |
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,26 @@ | ||
import { BrowserAgent } from '@newrelic/browser-agent/loaders/browser-agent' | ||
|
||
import * as RobustWebSocket from 'robust-websocket' | ||
|
||
const opts = { | ||
info: NREUM.info, | ||
init: NREUM.init | ||
} | ||
|
||
new BrowserAgent(opts) | ||
|
||
window.bamServer = NREUM.info.beacon | ||
|
||
var ws = new RobustWebSocket(`ws://${window.bamServer}/websocket`) | ||
|
||
ws.addEventListener('open', function (event) { | ||
ws.send('Hello!') | ||
}) | ||
|
||
ws.addEventListener('message', function (event) { | ||
ws.close() | ||
}) | ||
|
||
ws.addEventListener('close', function () { | ||
window.location.reload() | ||
}) |
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