LOG4PHP-166: Added connection timeout parameter to MongoDB appender.
authorIvan Habunek <ihabunek@apache.org>
Thu, 22 Dec 2011 13:28:44 +0000 (13:28 +0000)
committerIvan Habunek <ihabunek@apache.org>
Thu, 22 Dec 2011 13:28:44 +0000 (13:28 +0000)
Also removed redundant tests from the test class.

git-svn-id: https://svn.apache.org/repos/asf/logging/log4php/trunk@1222222 13f79535-47bb-0310-9956-ffa450edef68

src/changes/changes.xml
src/main/php/appenders/LoggerAppenderMongoDB.php
src/site/xdoc/docs/appenders/mongodb.xml
src/test/php/appenders/LoggerAppenderMongoDBTest.php

index d7ccf5e..f9aae82 100644 (file)
@@ -20,6 +20,9 @@
                <title>Apache log4php changelog</title>
        </properties>
        <body>
+           <release version="2.3.0-SNAPSHOT" date="SVN">
+               <action date="2011-12-22" type="update" issue="LOG4PHP-166" dev="Ivan Habunek" due-to="David Hilowitz" due-to-email="dhilowitz at gmail dot com">Added connection timeout parameter to MongoDB appender.</action>
+           </release>
            <release version="2.2.0" date="2011-12-20">
                        <action date="2011-12-13" type="fix" issue="LOG4PHP-158" dev="Ivan Habunek">LoggerAppenderFile throws wrong warning messages.</action>
                <action date="2011-12-12" type="update" issue="LOG4PHP-161" dev="Ivan Habunek">All configurable components should report errors when given invalid values</action>
index e9c3305..b3781fc 100644 (file)
@@ -52,6 +52,9 @@ class LoggerAppenderMongoDB extends LoggerAppender {
        /** Default value for {@link $collectionName} */
        const DEFAULT_COLLECTION_NAME = 'logs';
        
+       /** Default value for {@link $timeout} */
+       const DEFAULT_TIMEOUT_VALUE = 3000;
+       
        // ******************************************
        // ** Configurable parameters              **
        // ******************************************
@@ -74,6 +77,9 @@ class LoggerAppenderMongoDB extends LoggerAppender {
        /** Password used to connect to the database. */
        protected $password;
        
+       /** Timeout value used when connecting to the database (in milliseconds). */
+       protected $timeout;
+       
        // ******************************************
        // ** Member variables                     **
        // ******************************************
@@ -116,7 +122,7 @@ class LoggerAppenderMongoDB extends LoggerAppender {
         */
        public function activateOptions() {
                try {
-                       $this->connection = new Mongo(sprintf('%s:%d', $this->host, $this->port));
+                       $this->connection = new Mongo(sprintf('%s:%d', $this->host, $this->port), array("timeout" => $this->timeout));
                        $db     = $this->connection->selectDB($this->databaseName);
                        if ($this->userName !== null && $this->password !== null) {
                                $authResult = $db->authenticate($this->userName, $this->password);
@@ -279,6 +285,15 @@ class LoggerAppenderMongoDB extends LoggerAppender {
                return $this->password;
        }
        
+       /** Sets the value of {@link $timeout} parameter. */
+       public function setTimeout($timeout) {
+               $this->setPositiveInteger('timeout', $timeout);
+       }
+
+       /** Returns the value of {@link $timeout} parameter. */
+       public function getTimeout() {
+               return $this->timeout;
+       }
        /** 
         * Returns the mongodb connection.
         * @return Mongo
index aa2dc07..513b2a6 100644 (file)
                                                <td>-</td>\r
                                                <td>Password used to connect to the database.</td>\r
                                        </tr>\r
-                                       \r
+                                   <tr>\r
+                                       <td>timeout</td>\r
+                                       <td>integer</td>\r
+                                       <td>No</td>\r
+                                       <td>3000</td>\r
+                                       <td>For how long the driver should try to connect to the database (in milliseconds).</td>\r
+                                   </tr>\r
                                </table>\r
                                \r
                        </subsection>\r
index a8b1686..06dc7ff 100644 (file)
@@ -48,126 +48,77 @@ class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
        }
        
        protected function setUp() {
-               if (extension_loaded('mongo') == false) {
+               if (!extension_loaded('mongo')) {
                        $this->markTestSkipped(
                                'The Mongo extension is not available.'
                        );
                }
        }
        
-       public function test__construct() {
-               $appender = new LoggerAppenderMongoDB('mongo_appender');
-               $this->assertTrue($appender instanceof LoggerAppenderMongoDB);
-       }
-       
-       public function testSetGetHost() {
+       public function testHost() {
                $expected = 'mongodb://localhost';
                self::$appender->setHost($expected);
                $result = self::$appender->getHost();
-               $this->assertEquals($expected, $result, 'Host doesn\'t match expted value');
+               self::assertEquals($expected, $result);
        }
        
-       public function testSetGetHostMongoPrefix() {
-               $expected = 'mongodb://localhost';
-               self::$appender->setHost('localhost');
-               $result = self::$appender->getHost();
-               $this->assertEquals($expected, $result, 'Host doesn\'t match expted value');
-       }
-       
-       public function testSetPort() {
+       public function testPort() {
                $expected = 27017;
                self::$appender->setPort($expected);
                $result = self::$appender->getPort();
-               $this->assertEquals($expected, $result, 'Port doesn\'t match expted value');
+               self::assertEquals($expected, $result);
        }
 
-       public function testGetPort() {
-               $expected = 27017;
-               self::$appender->setPort($expected);
-               $result = self::$appender->getPort();
-               $this->assertEquals($expected, $result, 'Port doesn\'t match expted value');
-       }
-       
-       public function testSetDatabaseName() {
+       public function testDatabaseName() {
                $expected = 'log4php_mongodb';
                self::$appender->setDatabaseName($expected);
                $result = self::$appender->getDatabaseName();
-               $this->assertEquals($expected, $result, 'Database name doesn\'t match expted value');
+               self::assertEquals($expected, $result);
        }
        
-       public function testGetDatabaseName() {
-               $expected = 'log4php_mongodb';
-               self::$appender->setDatabaseName($expected);
-               $result = self::$appender->getDatabaseName();
-               $this->assertEquals($expected, $result, 'Database name doesn\'t match expted value');
-       }                
-       
-       public function testSetCollectionName() {
+       public function testCollectionName() {
                $expected = 'logs';
                self::$appender->setCollectionName($expected);
                $result = self::$appender->getCollectionName();
-               $this->assertEquals($expected, $result, 'Collection name doesn\'t match expted value');
+               self::assertEquals($expected, $result);
        }
        
-       public function testGetCollectionName() {
-               $expected = 'logs';
-               self::$appender->setCollectionName($expected);
-               $result = self::$appender->getCollectionName();
-               $this->assertEquals($expected, $result, 'Collection name doesn\'t match expted value');
-       }        
-       
-       public function testSetUserName() {
+       public function testUserName() {
                $expected = 'char0n';
                self::$appender->setUserName($expected);
                $result = self::$appender->getUserName();
-               $this->assertEquals($expected, $result, 'UserName doesn\'t match expted value');
+               self::assertEquals($expected, $result);
        }
        
-       public function testGetUserName() {
-               $expected = 'char0n';
-               self::$appender->setUserName($expected);
-               $result = self::$appender->getUserName();
-               $this->assertEquals($expected, $result, 'UserName doesn\'t match expted value');
-       }                                        
-       
-       public function testSetPassword() {
+       public function testPassword() {
                $expected = 'secret pass';
                self::$appender->setPassword($expected);
                $result = self::$appender->getPassword();
-               $this->assertEquals($expected, $result, 'Password doesn\'t match expted value');
+               self::assertEquals($expected, $result);
        }
        
-       public function testGetPassword() {
-               $expected = 'secret pass';
-               self::$appender->setPassword($expected);
-               $result = self::$appender->getPassword();
-               $this->assertEquals($expected, $result, 'Password doesn\'t match expted value');
-       } 
-       
        public function testActivateOptionsNoCredentials() {
                self::$appender->setUserName(null);
                self::$appender->setPassword(null);
                self::$appender->activateOptions();
        }               
        
-       public function testAppend() {
-               self::$appender->append(self::$event);
-       }
-       
        public function testFormat() {
-               $record = $this->logOne(self::$event);
+               $event = LoggerTestHelper::getErrorEvent("testmessage");
+               $record = $this->logOne($event);
                
-               $this->assertEquals('ERROR', $record['level']);
-               $this->assertEquals('testmessage', $record['message']);
-               $this->assertEquals('test.Logger', $record['loggerName']);
+               self::assertEquals('ERROR', $record['level']);
+               self::assertEquals('testmessage', $record['message']);
+               self::assertEquals('test', $record['loggerName']);
                
-               $this->assertEquals('NA', $record['fileName']);         
-               $this->assertEquals('getLocationInformation', $record['method']);
-               $this->assertEquals('NA', $record['lineNumber']);
-               $this->assertEquals('LoggerLoggingEvent', $record['className']);
+               self::assertEquals('NA', $record['fileName']);          
+               self::assertEquals('getLocationInformation', $record['method']);
+               self::assertEquals('NA', $record['lineNumber']);
+               self::assertEquals('LoggerLoggingEvent', $record['className']);
                
-               $this->assertTrue(is_int($record['thread']));
-               $this->assertTrue(is_int($record['lineNumber']) || $record['lineNumber'] == 'NA');
+               self::assertTrue(is_int($record['thread']));
+               self::assertSame(getmypid(), $record['thread']);
+               self::assertTrue(is_int($record['lineNumber']) || $record['lineNumber'] == 'NA');
        }
        
        public function testFormatThrowableInfo() {
@@ -182,10 +133,10 @@ class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
                
                $record = $this->logOne($event);
                
-               $this->assertArrayHasKey('exception', $record);
-               $this->assertEquals(1, $record['exception']['code']);
-               $this->assertEquals('test exception', $record['exception']['message']);
-               $this->assertContains('[internal function]: LoggerAppenderMongoDBTest', $record['exception']['stackTrace']);
+               self::assertArrayHasKey('exception', $record);
+               self::assertEquals(1, $record['exception']['code']);
+               self::assertEquals('test exception', $record['exception']['message']);
+               self::assertContains('[internal function]: LoggerAppenderMongoDBTest', $record['exception']['stackTrace']);
        }
        
        public function testFormatThrowableInfoWithInnerException() {
@@ -206,14 +157,14 @@ class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
                
                $record = $this->logOne($event);
 
-               $this->assertArrayHasKey('exception', $record);
-               $this->assertEquals(1, $record['exception']['code']);
-               $this->assertEquals('test exception', $record['exception']['message']);
-               $this->assertContains('[internal function]: LoggerAppenderMongoDBTest', $record['exception']['stackTrace']);
-               
-               $this->assertTrue(array_key_exists('innerException', $record['exception']));
-               $this->assertEquals(2, $record['exception']['innerException']['code']);
-               $this->assertEquals('test exception inner', $record['exception']['innerException']['message']);
+               self::assertArrayHasKey('exception', $record);
+               self::assertEquals(1, $record['exception']['code']);
+               self::assertEquals('test exception', $record['exception']['message']);
+               self::assertContains('[internal function]: LoggerAppenderMongoDBTest', $record['exception']['stackTrace']);
+               
+               self::assertTrue(array_key_exists('innerException', $record['exception']));
+               self::assertEquals(2, $record['exception']['innerException']['code']);
+               self::assertEquals('test exception inner', $record['exception']['innerException']['message']);
        }
        
        public function testClose() {
@@ -223,20 +174,23 @@ class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
        /** Logs the event and returns the record from the database. */
        private function logOne($event)
        {
-               self::$appender = new LoggerAppenderMongoDB();
-               self::$appender->setHost('localhost');
-               self::$appender->activateOptions();
-               $mongo = self::$appender->getConnection();
+               $appender = new LoggerAppenderMongoDB();
+               $appender->setHost('localhost');
+               $appender->activateOptions();
+               
+               $mongo = $appender->getConnection();
                $collection = $mongo->log4php_mongodb->logs;
                
                $result = $collection->drop();
                self::assertSame((float) 1, $result['ok'], "Could not clear the collection before logging.");
                
-               self::$appender->append($event);
+               $appender->append($event);
                
                $record = $collection->findOne();
                self::assertNotNull($record, "Could not read the record from the database.");
                
+               $appender->close();
+               
                return $record;
        }