-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: polish code, add IPv6 tests and remove unsupported ports
- Loading branch information
1 parent
f7c241c
commit 683f667
Showing
3 changed files
with
115 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,79 @@ | ||
import Hostname from 'Common/Types/API/Hostname'; | ||
import IPv4 from 'Common/Types/IP/IPv4'; | ||
import IPv6 from 'Common/Types/IP/IPv6'; | ||
import PositiveNumber from 'Common/Types/PositiveNumber'; | ||
import Ping, { | ||
PingResponse, | ||
} from '../../Utils/Monitors/MonitorTypes/PingMonitor'; | ||
|
||
describe('Ping', () => { | ||
jest.setTimeout(10000); | ||
test('Ping.ping should return appropriate object if the valid hostname is given', async () => { | ||
let result: PingResponse | null = await Ping.ping( | ||
new Hostname('google.com', 80) | ||
); | ||
|
||
expect(result).not.toBeNull(); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); | ||
expect(result!.isOnline).toBe(true); | ||
result = await Ping.ping(new Hostname('www.google.com', 80), { | ||
timeout: new PositiveNumber(5000), | ||
}); | ||
test('should succeed with a valid and reachable hostname', async () => { | ||
const result: PingResponse | null = await Ping.ping( | ||
new Hostname('google.com'), | ||
{ | ||
timeout: new PositiveNumber(5000), | ||
} | ||
); | ||
|
||
expect(result).not.toBeNull(); | ||
expect(result!.isOnline).toBe(true); | ||
expect(result!.responseTimeInMS).toBeDefined(); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); | ||
}); | ||
|
||
result = await Ping.ping(new Hostname('www.google.com', 65000), { | ||
timeout: new PositiveNumber(5000), | ||
}); | ||
expect(result).not.toBeNull(); | ||
expect(result!.isOnline).toBe(false); | ||
expect(result!.responseTimeInMS).toBeUndefined(); | ||
test('should fail with an invalid hostname', async () => { | ||
const result: PingResponse | null = await Ping.ping( | ||
new Hostname('invalid.hostname'), | ||
{ | ||
timeout: new PositiveNumber(5000), | ||
} | ||
); | ||
|
||
result = await Ping.ping(new Hostname('www.a.com', 65000), { | ||
timeout: new PositiveNumber(5000), | ||
}); | ||
expect(result).not.toBeNull(); | ||
expect(result!.isOnline).toBe(false); | ||
expect(result!.isOnline).toBe(false); | ||
expect(result!.responseTimeInMS).toBeUndefined(); | ||
}); | ||
test('Ping.ping should return appropriate object if the valid IPV4 or IPV6 is given', async () => { | ||
let result: PingResponse | null = null; | ||
|
||
result = await Ping.ping(new IPv4('172.217.170.206'), { | ||
timeout: new PositiveNumber(5000), | ||
}); // One of the google ip | ||
test('should succeed with a valid IPV4 address', async () => { | ||
const result: PingResponse | null = await Ping.ping( | ||
new IPv4('8.8.8.8'), | ||
{ | ||
timeout: new PositiveNumber(5000), | ||
} | ||
); | ||
|
||
expect(result).not.toBeNull(); | ||
expect(result!.isOnline).toBe(true); | ||
expect(result!.responseTimeInMS).toBeDefined(); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); | ||
}); | ||
|
||
test('should succeed with a valid and reachable IPv6 address', async () => { | ||
const ipv6Address: IPv6 = new IPv6('2001:4860:4860::8888'); | ||
const result: PingResponse | null = await Ping.ping(ipv6Address); | ||
|
||
result = await Ping.ping(new IPv4('192.0.2.200')); // | ||
expect(result).not.toBeNull(); | ||
expect(result!.isOnline).toBe(false); | ||
expect(result!.responseTimeInMS).toBeUndefined(); | ||
expect(result!.isOnline).toBe(true); | ||
expect(result!.responseTimeInMS).toBeDefined(); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); | ||
expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); | ||
}); | ||
|
||
test('should fail with an unreachable IPv6 address', async () => { | ||
const ipv6Address: IPv6 = new IPv6( | ||
'abcd:ef01:2345:6789:abcd:ef01:2345:6789' | ||
); | ||
// since ping does not support timeouts on IPv6, we set the deadline to 1 second to avoid exceeding jest's timeout | ||
const result: PingResponse | null = await Ping.ping(ipv6Address, { | ||
deadline: 1, | ||
}); | ||
|
||
result = await Ping.ping(new IPv4('0.42.52.42')); // ip can't start 0 | ||
expect(result).not.toBeNull(); | ||
expect(result!.responseTimeInMS).toBeUndefined(); | ||
expect(result!.isOnline).toBe(false); | ||
expect(result!.responseTimeInMS).toBeUndefined(); | ||
}); | ||
}); |
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