Skip to content

Commit

Permalink
Make tests less fragile by improving test timeouts and skip unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Mar 7, 2020
1 parent a2ee3c0 commit cea7c78
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 78 deletions.
74 changes: 2 additions & 72 deletions tests/FunctionalConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP()
public function connectionToRemoteTCP4ServerShouldResultInOurIP()
{
if ($this->ipv4() === false) {
// IPv4 not supported on this system
$this->assertFalse($this->ipv4());
return;
$this->markTestSkipped('IPv4 connection not supported on this system');
}

$loop = Factory::create();
Expand All @@ -79,9 +77,7 @@ public function connectionToRemoteTCP4ServerShouldResultInOurIP()
public function connectionToRemoteTCP6ServerShouldResultInOurIP()
{
if ($this->ipv6() === false) {
// IPv6 not supported on this system
$this->assertFalse($this->ipv6());
return;
$this->markTestSkipped('IPv6 connection not supported on this system');
}

$loop = Factory::create();
Expand All @@ -94,72 +90,6 @@ public function connectionToRemoteTCP6ServerShouldResultInOurIP()
$this->assertSame($ip, filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6), $ip);
}

/**
* @test
* @group internet
*
* @expectedException \RuntimeException
* @expectedExceptionMessageRegExp /Connection to ipv6.tlund.se:80 failed/
*/
public function tryingToConnectToAnIPv6OnlyHostWithOutHappyEyeBallsShouldResultInFailure()
{
$loop = Factory::create();

$connector = new Connector($loop, array('happy_eyeballs' => false));

Block\await($this->request('ipv6.tlund.se', $connector), $loop, self::TIMEOUT);
}

/**
* @test
* @group internet
*
* @expectedException \RuntimeException
* @expectedExceptionMessageRegExp /Connection to tcp:\/\/193.15.228.195:80 failed:/
*/
public function connectingDirectlyToAnIPv4AddressShouldFailWhenIPv4IsntAvailable()
{
if ($this->ipv4() === true) {
// IPv4 supported on this system
throw new \RuntimeException('Connection to tcp://193.15.228.195:80 failed:');
}

$loop = Factory::create();

$connector = new Connector($loop);

$host = current(dns_get_record('ipv4.tlund.se', DNS_A));
$host = $host['ip'];
Block\await($this->request($host, $connector), $loop, self::TIMEOUT);
}

/**
* @test
* @group internet
*
* @expectedException \RuntimeException
* @expectedExceptionMessageRegExp /Connection to tcp:\/\/\[2a00:801:f::195\]:80 failed:/
*/
public function connectingDirectlyToAnIPv6AddressShouldFailWhenIPv6IsntAvailable()
{
if ($this->ipv6() === true) {
// IPv6 supported on this system
throw new \RuntimeException('Connection to tcp://[2a00:801:f::195]:80 failed:');
}

$loop = Factory::create();

$connector = new Connector($loop);

$host = current(dns_get_record('ipv6.tlund.se', DNS_AAAA));
$host = $host['ipv6'];
$host = '[' . $host . ']';
$ip = Block\await($this->request($host, $connector), $loop, self::TIMEOUT);

$this->assertFalse(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4), $ip);
$this->assertSame($ip, filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6), $ip);
}

/**
* @internal
*/
Expand Down
18 changes: 12 additions & 6 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function ($e) use (&$wait) {
}
);

// run loop for short period to ensure we detect connection timeout error
// run loop for short period to ensure we detect a connection timeout error
Block\sleep(0.01, $loop);
if ($wait) {
Block\sleep(0.2, $loop);
Expand Down Expand Up @@ -236,7 +236,7 @@ function ($e) use (&$wait) {
}
);

// run loop for short period to ensure we detect connection timeout error
// run loop for short period to ensure we detect a connection timeout error
Block\sleep(0.01, $loop);
if ($wait) {
Block\sleep(0.2, $loop);
Expand Down Expand Up @@ -269,12 +269,15 @@ function ($e) use (&$wait) {
}
);

// run loop for short period to ensure we detect DNS error
// run loop for short period to ensure we detect a DNS error
Block\sleep(0.01, $loop);
if ($wait) {
Block\sleep(0.2, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
Block\sleep(2.0, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
}
}
}
unset($promise);
Expand Down Expand Up @@ -309,12 +312,15 @@ function ($e) use (&$wait) {
}
);

// run loop for short period to ensure we detect DNS error
// run loop for short period to ensure we detect a TLS error
Block\sleep(0.1, $loop);
if ($wait) {
Block\sleep(0.4, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
Block\sleep(self::TIMEOUT - 0.5, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
}
}
}
unset($promise);
Expand Down

0 comments on commit cea7c78

Please sign in to comment.