public function test_ShouldBeUnsuableOnTooManyFailsOrIfBlocked() { $maxFails = 10; $rp = new RotatingProxy("test", null, $maxFails, -1, null); $this->assertTrue($rp->isUsable(), "Expected NOT to have enough consecutive fails"); $rp->setCurrentConsecutiveFails($maxFails); $this->assertFalse($rp->isUsable(), "Expected to have enough consecutive fails"); $rp->setCurrentConsecutiveFails(0); $rp->block(); $this->assertFalse($rp->isUsable(), "Expected to be blocked"); $rp->unblock(); $this->assertTrue($rp->isUsable(), "Expected NOT to be blocked"); }
public function test_integration_ChooseTheRightProxyAtTheRightTime() { /* * Scenario: * 10 requests, 2 proxies * In the end, * proxy_0 should have 6 total requests and 1 total error and 0 consecutive errors * proxy_1 should have 4 successful requests and 3 total and consecutive errors (making it fail) * No retries take place */ $total_0 = 6; $total_error_0 = 1; $consecutive_error_0 = 0; $max_consecutive_error_0 = 3; $total_1 = 4; $total_error_1 = 3; $consecutive_error_1 = 3; $max_consecutive_error_1 = 3; $client = new Client(); $proxy0 = new RotatingProxy("0", null, $max_consecutive_error_0, 10, null); $proxy1 = new RotatingProxy("1", null, $max_consecutive_error_1, 10, null); $proxies = [0 => $proxy0, 1 => $proxy1]; $success = true; $fail = false; $responses2Proxy = [[$success, $proxy0], [$success, $proxy0], [$success, $proxy1], [$fail, $proxy1], [$fail, $proxy0], [$fail, $proxy1], [$fail, $proxy1], [$success, $proxy0], [$success, $proxy0], [$success, $proxy0]]; $randKeys = []; $responses = []; foreach ($responses2Proxy as $key => $val) { $randKeys[$key] = array_search($val[1], $proxies); $responses[$key] = $val[0] ? new Response(200) : new Response(403); } $h = $this->getHelper(null, null, $randKeys); $useOwnIp = false; $rotator = new ProxyRotator($proxies, $useOwnIp, $h->getRandomMock()); $sub = new RotatingProxySubscriber($rotator); $mock = new Mock($responses); // Add the mock subscriber to the client. $client->getEmitter()->attach($mock); $client->getEmitter()->attach($sub); // build requests - we need to do this _after_ the $mock hast been attached to the client, // otherwise a real request is sent. $requests = []; foreach ($responses as $key => $val) { $req = $client->createRequest("GET"); $req->getConfig()->set("request_id", $key); $requests[$key] = $req; } // $sucFn = function(RequestInterface $request){ // echo "Success at request ".$request->getConfig()->get("request_id")." using proxy ".$request->getConfig()->get("proxy")."\n"; // }; // $errFn = function(RequestInterface $request, Exception $e){ // echo "Error at request ".$request->getConfig()->get("request_id")." using proxy ".$request->getConfig()->get("proxy").": ".$e->getMessage()."\n"; // }; // foreach($requests as $key => $request){ // try { // $client->send($request); // $sucFn($request); // }catch(Exception $e){ // $errFn($request, $e); // } // } $options = []; $pool = new Pool($client, $requests, $options); $pool->wait(); $this->assertEquals($total_0, $proxy0->getTotalRequests()); $this->assertEquals($total_error_0, $proxy0->getCurrentTotalFails()); $this->assertEquals($consecutive_error_0, $proxy0->getCurrentConsecutiveFails()); $this->assertEquals($consecutive_error_0 < $max_consecutive_error_0, $proxy0->isUsable()); $this->assertEquals($total_1, $proxy1->getTotalRequests()); $this->assertEquals($total_error_1, $proxy1->getCurrentTotalFails()); $this->assertEquals($consecutive_error_1, $proxy1->getCurrentConsecutiveFails()); $this->assertEquals($consecutive_error_1 < $max_consecutive_error_1, $proxy1->isUsable()); }