Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
CRAP
94.12% covered (success)
94.12%
16 / 17
BasicRequestTimeout
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
10.02
94.12% covered (success)
94.12%
16 / 17
 enterCriticalSection
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 exitCriticalSection
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCpuTime
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setWallTimeLimit
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 getWallTimeRemaining
0.00% covered (danger)
0.00%
0 / 1
3.33
66.67% covered (warning)
66.67%
2 / 3
 getWallTimeLimit
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
<?php
namespace Wikimedia\RequestTimeout\Detail;
use Wikimedia\RequestTimeout\RequestTimeout;
/**
 * A fallback implementation used when Excimer is not available
 */
class BasicRequestTimeout extends RequestTimeout {
    /** @var float */
    private $limit;
    /** @var float */
    private $startTime;
    /** @var int The next critical section ID to use */
    private $nextCriticalId = 1;
    public function enterCriticalSection( $name, $emergencyLimit, $emergencyCallback ) {
        return $this->nextCriticalId++;
    }
    public function exitCriticalSection( $id ) {
    }
    /**
     * Get the elapsed CPU time. This can be used to estimate when the time
     * limit will expire.
     *
     * @return float
     */
    private function getCpuTime() {
        $r = getrusage();
        return $r['ru_utime.tv_sec'] + $r['ru_stime.tv_sec']
            + ( $r['ru_utime.tv_usec'] + $r['ru_stime.tv_usec'] ) * 1e-6;
    }
    public function setWallTimeLimit( $limit ) {
        if ( $limit === INF ) {
            $limit = 0;
        }
        $this->limit = ceil( $limit );
        $this->startTime = $this->getCpuTime();
        set_time_limit( $this->limit );
    }
    public function getWallTimeRemaining() {
        if ( $this->startTime !== null && $this->limit > 0 ) {
            return $this->limit - $this->getCpuTime() + $this->startTime;
        } else {
            return INF;
        }
    }
    public function getWallTimeLimit() {
        if ( $this->limit > 0 ) {
            return $this->limit;
        } else {
            return INF;
        }
    }
}