Browse Source

Merge pull request #100 from hason/socket

Improved SocketHandler
Jordi Boggiano 13 years ago
parent
commit
ae31045917
2 changed files with 21 additions and 14 deletions
  1. 17 10
      src/Monolog/Handler/SocketHandler.php
  2. 4 4
      tests/Monolog/Handler/SocketHandlerTest.php

+ 17 - 10
src/Monolog/Handler/SocketHandler.php

@@ -89,7 +89,7 @@ class SocketHandler extends AbstractProcessingHandler
     /**
      * Set connection timeout.  Only has effect before we connect.
      *
-     * @param integer $seconds
+     * @param float $seconds
      *
      * @see http://php.net/manual/en/function.fsockopen.php
      */
@@ -102,14 +102,14 @@ class SocketHandler extends AbstractProcessingHandler
     /**
      * Set write timeout. Only has effect before we connect.
      *
-     * @param type $seconds
+     * @param float $seconds
      *
      * @see http://php.net/manual/en/function.stream-set-timeout.php
      */
     public function setTimeout($seconds)
     {
         $this->validateTimeout($seconds);
-        $this->timeout = (int) $seconds;
+        $this->timeout = (float) $seconds;
     }
 
     /**
@@ -183,10 +183,15 @@ class SocketHandler extends AbstractProcessingHandler
 
     /**
      * Wrapper to allow mocking
+     *
+     * @see http://php.net/manual/en/function.stream-set-timeout.php
      */
     protected function streamSetTimeout()
     {
-        return stream_set_timeout($this->resource, $this->timeout);
+        $seconds = floor($this->timeout);
+        $microseconds = round(($this->timeout - $seconds)*1e6);
+        
+        return stream_set_timeout($this->resource, $seconds, $microseconds);
     }
 
     /**
@@ -207,11 +212,9 @@ class SocketHandler extends AbstractProcessingHandler
 
     private function validateTimeout($value)
     {
-        $ok = filter_var($value, FILTER_VALIDATE_INT, array('options' => array(
-                'min_range' => 0,
-                )));
-        if ($ok === false) {
-            throw new \InvalidArgumentException("Timeout must be 0 or a positive integer (got $value)");
+        $ok = filter_var($value, FILTER_VALIDATE_FLOAT);
+        if ($ok === false || $value < 0) {
+            throw new \InvalidArgumentException("Timeout must be 0 or a positive float (got $value)");
         }
     }
 
@@ -254,7 +257,11 @@ class SocketHandler extends AbstractProcessingHandler
         $length = strlen($data);
         $sent = 0;
         while ($this->isConnected() && $sent < $length) {
-            $chunk = $this->fwrite(substr($data, $sent));
+            if (0 == $sent) {
+                $chunk = $this->fwrite($data);
+            } else {
+                $chunk = $this->fwrite(substr($data, $sent));
+            }
             if ($chunk === false) {
                 throw new \RuntimeException("Could not write to socket");
             }

+ 4 - 4
tests/Monolog/Handler/SocketHandlerTest.php

@@ -50,8 +50,8 @@ class SocketHandlerTest extends TestCase
     public function testSetConnectionTimeout()
     {
         $this->createHandler('localhost:1234');
-        $this->handler->setConnectionTimeout(10);
-        $this->assertEquals(10, $this->handler->getConnectionTimeout());
+        $this->handler->setConnectionTimeout(10.1);
+        $this->assertEquals(10.1, $this->handler->getConnectionTimeout());
     }
 
     /**
@@ -66,8 +66,8 @@ class SocketHandlerTest extends TestCase
     public function testSetTimeout()
     {
         $this->createHandler('localhost:1234');
-        $this->handler->setTimeout(10);
-        $this->assertEquals(10, $this->handler->getTimeout());
+        $this->handler->setTimeout(10.25);
+        $this->assertEquals(10.25, $this->handler->getTimeout());
     }
 
     public function testSetConnectionString()