fix: handle multiple strings that look like they might be JSON (#361)

The test output can have occurrences of `{` that aren't the string we are looking for, eg:

```
dialer-1    | test browser
dialer-1    | [JavaScript Error: "Clear-Site-Data header found. Unknown value “"cache"”." {file: "http://127.0.0.1:34451/" line: 0}]
dialer-1    | [JavaScript Error: "Clear-Site-Data header forced the clean up of “cookies” data." {file: "http://127.0.0.1:34451/" line: 0}]
dialer-1    | [JavaScript Error: "Clear-Site-Data header forced the clean up of “storage” data." {file: "http://127.0.0.1:34451/" line: 0}]
```

These fail to parse so improve the results parsing by:

1. Try parsing every instance of `{...}` in the output, not just the first
2. Test the output for the `handshakePlusOneRTTMillis` and `pingRTTMilllis` keys
This commit is contained in:
Alex Potsides 2024-05-07 11:10:40 +01:00 committed by GitHub
parent 0518ca618c
commit fd59ed70fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 80 additions and 13 deletions

View File

@ -13,11 +13,21 @@ export function dialerStdout(composeStdout: string): string {
}
export function dialerTimings(dialerStdout: string): Object {
const openBracket = dialerStdout.indexOf("{")
if (openBracket === -1) throw new Error("Invalid JSON. No opening curly bracket found")
const closeBracket = dialerStdout.indexOf("}", openBracket)
if (closeBracket === -1) throw new Error("Invalid JSON. No closing curly bracket found")
return JSON.parse(dialerStdout.substring(openBracket, closeBracket + 1))
let openBracket = dialerStdout.indexOf("{")
let error
while (true) {
if (openBracket === -1) break
const closeBracket = dialerStdout.indexOf("}", openBracket)
if (closeBracket === -1) throw new Error("Invalid JSON. No closing curly bracket found")
try {
const result = JSON.parse(dialerStdout.substring(openBracket, closeBracket + 1))
if (result.handshakePlusOneRTTMillis != null && result.pingRTTMilllis != null) return result
} catch (err) {
error = err
}
openBracket = dialerStdout.indexOf("{", openBracket + 1)
}
throw error ?? new Error("Invalid JSON. No opening curly bracket found")
}
// simple test case - avoids bringing in a whole test framework
@ -96,6 +106,63 @@ function test() {
const expectedParsed = JSON.stringify({ "handshakePlusOneRTTMillis": 4155, "pingRTTMilllis": 781 })
assert(JSON.stringify(dialerTimings(dialerStdout(exampleComposeStdout))) === expectedParsed)
}
{
const exampleComposeStdout = `
Attaching to dialer-1, listener-1, redis-1
redis-1 | 1:C 05 May 2024 12:33:40.595 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-1 | 1:C 05 May 2024 12:33:40.595 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-1 | 1:C 05 May 2024 12:33:40.595 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
redis-1 | 1:C 05 May 2024 12:33:40.595 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-1 | 1:M 05 May 2024 12:33:40.596 * monotonic clock: POSIX clock_gettime
redis-1 | 1:M 05 May 2024 12:33:40.596 * Running mode=standalone, port=6379.
redis-1 | 1:M 05 May 2024 12:33:40.597 * Server initialized
redis-1 | 1:M 05 May 2024 12:33:40.597 * Ready to accept connections tcp
listener-1 | [2024-05-05T12:33:41Z INFO interop_tests] Running ping test: 12D3KooWT3dMe8wpuqmztR3syYzni4vVk3TSCGujMGBeGN3qR4Cn
listener-1 | [2024-05-05T12:33:41Z INFO interop_tests] Test instance, listening for incoming connections on: "/ip4/0.0.0.0/udp/0/webrtc-direct".
dialer-1 |
dialer-1 | > @libp2p/multidim-interop@2.0.31 test:interop:multidim
dialer-1 | > aegir test --build false --types false -t browser -- --browser firefox
dialer-1 |
dialer-1 | test browser
dialer-1 | [JavaScript Error: "Clear-Site-Data header found. Unknown value “"cache"”." {file: "http://127.0.0.1:34451/" line: 0}]
dialer-1 | [JavaScript Error: "Clear-Site-Data header forced the clean up of “cookies” data." {file: "http://127.0.0.1:34451/" line: 0}]
dialer-1 | [JavaScript Error: "Clear-Site-Data header forced the clean up of “storage” data." {file: "http://127.0.0.1:34451/" line: 0}]
dialer-1 |
dialer-1 | ping test
dialer-1 | - should listen for ping
listener-1 | [2024-05-05T12:33:55Z INFO webrtc::peer_connection] signaling state changed to have-remote-offer
listener-1 | [2024-05-05T12:33:55Z INFO webrtc_ice::mdns] mDNS is using 224.0.0.251:5353 as dest_addr
listener-1 | [2024-05-05T12:33:55Z INFO webrtc_ice::agent::agent_internal] [controlled]: Setting new connection state: Checking
listener-1 | [2024-05-05T12:33:55Z INFO webrtc_mdns::conn] Looping and listening Ok(224.0.0.251:5353)
listener-1 | [2024-05-05T12:33:55Z INFO webrtc::peer_connection] ICE connection state changed: checking
listener-1 | [2024-05-05T12:33:55Z INFO webrtc::peer_connection] signaling state changed to stable
listener-1 | [2024-05-05T12:33:55Z INFO webrtc_ice::agent::agent_internal] [controlled]: Setting new connection state: Connected
listener-1 | [2024-05-05T12:33:55Z INFO webrtc::peer_connection] ICE connection state changed: connected
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 16
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 28
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 16
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 28
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 16
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 28
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 16
listener-1 | [2024-05-05T12:33:55Z WARN webrtc_dtls::handshake::handshake_message_client_hello] Unsupported Extension Type 0 28
listener-1 | [2024-05-05T12:33:55Z INFO webrtc::peer_connection] peer connection state changed: connected
listener-1 | [2024-05-05T12:33:55Z INFO libp2p_webrtc_utils::stream::drop_listener] Stream dropped without graceful close, sending Reset
listener-1 | [2024-05-05T12:33:56Z INFO libp2p_webrtc_utils::stream::drop_listener] Stream dropped without graceful close, sending Reset
dialer-1 | {"handshakePlusOneRTTMillis":1384,"pingRTTMilllis":39}
dialer-1 | should dial and ping (1405ms)
listener-1 | [2024-05-05T12:33:57Z WARN webrtc_sctp::association] [] failed to read packets on net_conn: Alert is Fatal or Close Notify
dialer-1 | 1 passing (2s)
dialer-1 | 1 pending
listener-1 | [2024-05-05T12:33:57Z INFO libp2p_webrtc_utils::stream::drop_listener] Stream dropped without graceful close, sending Reset
dialer-1 exited with code 0
`
const expectedParsed = JSON.stringify({ "handshakePlusOneRTTMillis": 1384, "pingRTTMilllis": 39 })
assert(JSON.stringify(dialerTimings(dialerStdout(exampleComposeStdout))) === expectedParsed)
}
}