/** * @test * @group Cron * @group runsAgents * @group runAgents */ public function runsAgents() { $core = Hashmark::getModule('Core', '', $this->_db); $scheduledScalarIds = array(); $expected = array(); $expected['name'] = self::randomString(); $expected['type'] = 'decimal'; $expected['value'] = 1234; $scalarId = $core->createScalar($expected); $agent = $core->getAgentByName('ScalarValue'); if ($agent) { $agentId = $agent['id']; } else { $agentId = $core->createAgent('ScalarValue'); } // Agent is scheduled to run immediately (0 frequency). $scalarAgentId = $core->createScalarAgent($scalarId, $agentId, 0, 'Scheduled'); ob_start(); require HASHMARK_ROOT_DIR . '/Cron/runAgents.php'; ob_end_clean(); // Assert scalar value changed in last 60 seconds. $actual = $core->getScalarById($scalarId); $this->assertEquals($expected['value'] + 5, (int) $actual['value']); // Assert scalar agent is ready for future run. $scalarAgent = $core->getScalarAgentById($scalarAgentId); $this->assertEquals('Scheduled', $scalarAgent['status']); $this->assertEquals('', $scalarAgent['error']); $this->assertLessThan(5, abs(strtotime($scalarAgent['lastrun'] . ' UTC') - time())); }
/** * Resources needed for most tests. * * @return void */ protected function setUp() { parent::setUp(); $this->_partition = Hashmark::getModule('Partition', '', $this->_db); $this->_core = Hashmark::getModule('Core', '', $this->_db); $this->_mergeTablePrefix = Hashmark::getConfig('Partition', '', 'mergetable_prefix'); }
/** * Set up the test case fixtures. * * @return void */ protected function setUp() { // Extract suffix (ex. 'DbDependent') from class (ex. 'Hashmark_TestCase_Module_DbDependent'). $className = get_class($this); $this->_type = substr($className, strrpos($className, '_') + 1); $this->_db = Hashmark::getModule('DbHelper')->openDb('unittest'); }
/** * @test * @group DbHelper * @group reusesDbConnection * @group reuseDb */ public function reusesDbConnection() { $config = Hashmark::getConfig('DbHelper'); $link = new mysqli($config['profile']['unittest']['params']['host'], $config['profile']['unittest']['params']['username'], $config['profile']['unittest']['params']['password'], $config['profile']['unittest']['params']['dbname'], $config['profile']['unittest']['params']['port']); $db = Hashmark::getModule('DbHelper')->reuseDb($link, 'Mysqli'); $this->assertEquals('mysqli', get_class($db->getConnection())); }
/** * @test * @group Hashmark * @group loadsExternalModuleType * @group getModule * @group getTypeConfig */ public function loadsExternalModuleType() { $inst = Hashmark::getModule('Test', 'FakeExternalType'); $this->assertTrue(is_subclass_of($inst, 'Hashmark_Test')); $typeConfig = $inst->getTypeConfig(); $this->assertEquals(4400, $typeConfig['test_key']); }
/** * Resources needed for most tests. * * @return void */ protected function setUp() { parent::setUp(); $partition = Hashmark::getModule('Partition', '', $this->_db); $this->_analyst = Hashmark::getModule('Analyst', 'BasicDecimal', $this->_db, $partition); $this->_partition = Hashmark::getModule('Partition', '', $this->_db); $this->_core = Hashmark::getModule('Core', '', $this->_db); }
/** * @test * @group Agent * @group Test * @group runsAgent * @group run */ public function runsAgent() { $expectedFields = array(); $expectedFields['name'] = self::randomString(); $expectedFields['value'] = 'test_value'; $expectedFields['type'] = 'string'; $expectedId = Hashmark::getModule('Core', '', $this->_db)->createScalar($expectedFields); $sample = Hashmark::getModule('Agent', 'ScalarValue')->run(array('scalarId' => $expectedId)); $this->assertEquals($expectedFields['value'], $sample); }
/** * @test * @group Module * @group initsBaseAndTypeProps * @group getModule * @group getBase * @group getBaseConfig * @group getType * @group getTypeConfig */ public function initsBaseAndTypeProps() { $expectedBase = 'Test'; $expectedType = 'FakeModuleType'; $inst = Hashmark::getModule($expectedBase, $expectedType); $this->assertEquals($expectedBase, $inst->getBase()); $baseConfig = $inst->getBaseConfig(); $this->assertEquals($baseConfig['base'], $expectedBase); $this->assertEquals($expectedType, $inst->getType()); $typeConfig = $inst->getTypeConfig(); $this->assertEquals($typeConfig['type'], $expectedType); }
/** * @see Parent/interface signature docs. */ public static function run(&$agent) { if (empty($agent['scalar_id'])) { return; } $db = Hashmark::getModule('DbHelper')->openDb('cron'); $client = Hashmark::getModule('Client', '', $db); $partition = Hashmark::getModule('Partition', '', $db); if (!$client) { return; } $oldValue = $client->get((int) $agent['scalar_id']); $newValue = $oldValue + 5; $time = time(); if (!$partition->createSample($agent['scalar_id'], $newValue, $time)) { $agent['error'] = sprintf('Could not save sample: time=%s value=%s', $time, $value); } }
/** * @test * @group Cron * @group collectsGarbageMergeTables * @group getAllMergeTables */ public function collectsGarbageMergeTables() { $partition = Hashmark::getModule('Partition', '', $this->_db); $mergeTablePrefix = Hashmark::getConfig('Partition', '', 'mergetable_prefix'); // Drop all merge tables to clear the slate. $priorMergeTables = $partition->getTablesLike($mergeTablePrefix . '%'); if ($priorMergeTables) { $partition->dropTable($priorMergeTables); } $scalar = array(); $scalar['name'] = self::randomString(); $scalar['type'] = 'decimal'; $scalarId = Hashmark::getModule('Core', '', $this->_db)->createScalar($scalar); $regTableName = 'test_samples_' . self::randomString(); $partition->createTable($scalarId, $regTableName, $scalar['type']); $now = time(); $start = '2008-04-01 01:45:59'; $mergeTableNames = array(); // Create several merge tables from the same single normal table. // Space them 1 day apart. // Vary $end to make the merge table names unique. for ($t = 0; $t < 5; $t++) { $end = "2009-06-1{$t} 01:45:59"; $comment = gmdate(HASHMARK_DATETIME_FORMAT, $now - $t * 86400); $mergeTableNames[$t] = $mergeTablePrefix . "{$scalarId}_20080401_2009061{$t}"; $actualTable = $partition->createMergeTable($scalarId, $start, $end, array($regTableName), $comment); $this->assertEquals($mergeTableNames[$t], $actualTable); } $maxDays = 3; $maxCount = 2; ob_start(); require HASHMARK_ROOT_DIR . '/Cron/gcMergeTables.php'; ob_end_clean(); for ($t = 0; $t < 5; $t++) { if ($t < 2) { $this->assertTrue($partition->tableExists($mergeTableNames[$t]), "Expected {$mergeTableNames[$t]} to exist"); } else { $this->assertFalse($partition->tableExists($mergeTableNames[$t]), "Expected {$mergeTableNames[$t]} to be missing"); } } }
$core = Hashmark::getModule('Core', '', $db); $partition = Hashmark::getModule('Partition', '', $db); $scheduledAgents = $core->getScheduledAgents(); if (empty($scheduledAgents)) { exit; } // Reuse previously loaded agent objects since they have no properties. $cache = array(); foreach ($scheduledAgents as $scalarAgent) { if ('Running' == $scalarAgent['status']) { $core->setScalarAgentStatus($scalarAgent['id'], 'Unscheduled', 'Last run did not finish.'); continue; } if (!isset($cache[$scalarAgent['name']])) { try { $cache[$scalarAgent['name']] = Hashmark::getModule('Agent', $scalarAgent['name']); } catch (Exception $e) { $error = sprintf('Agent "%s" module missing: %s', $scalarAgent['name'], $e->getMessage()); $core->setScalarAgentStatus($scalarAgent['id'], 'Unscheduled', $error); continue; } } if (!$cache[$scalarAgent['name']]) { $error = "Agent '{$scalarAgent['name']}' was missing"; $core->setScalarAgentStatus($scalarAgent['id'], 'Unscheduled', $error); continue; } $core->setScalarAgentStatus($scalarAgent['id'], 'Running'); $value = $cache[$scalarAgent['name']]->run($scalarAgent); // run() received $scalarAgent by-ref and can apply indepedent logic // to set a new status, error message, etc.
/** * @test * @group Cache * @group removesWholeGroup * @group save * @group load * @group removeGroup */ public function removesWholeGroup() { $cache = Hashmark::getModule('Cache'); // Assert these are unaffected by the operations on subject keys/values/groups. $unaffectedKey = self::randomString(); $unaffectedValue = self::randomString(); $unaffectedGroup = self::randomString(); $this->assertTrue($cache->save($unaffectedValue, $unaffectedKey, $unaffectedGroup)); $this->assertEquals($unaffectedValue, $cache->load($unaffectedKey, $unaffectedGroup)); // Two keys will share group. $subjectKeys = array(self::randomString(), self::randomString()); $subjectGroup = self::randomString(); // Write to subject key/group. foreach ($subjectKeys as $subjectKey) { $subjectValue = self::randomString(); $this->assertTrue($cache->save($subjectValue, $subjectKey, $subjectGroup)); $this->assertEquals($subjectValue, $cache->load($subjectKey, $subjectGroup)); } // Remove subject group. $this->assertTrue($cache->removeGroup($subjectGroup)); foreach ($subjectKeys as $subjectKey) { $this->assertFalse($cache->load($subjectKey, $subjectGroup)); } // Verify other group not touched. $this->assertEquals($unaffectedValue, $cache->load($unaffectedKey, $unaffectedGroup)); }
*/ $modList = array('BcMath' => '', 'Cache' => '', 'Client' => '', 'Core' => '', 'DbHelper' => '', 'Partition' => '', 'Agent' => 'YahooWeather', 'Test' => 'FakeModuleType'); foreach ($modList as $baseName => $typeName) { // Cache modules don't use the DB argument, // but it should have no effect. $inst = Hashmark::getModule($baseName, $typeName, $mockDb); if ($typeName) { $className = "Hashmark_{$baseName}_{$typeName}"; } else { $className = "Hashmark_{$baseName}"; } $testDetail = "{$className} module.\n"; if ($inst instanceof $className) { echo "pass: Loaded {$testDetail}"; } else { echo "====> FAIL: Could not load {$testDetail}"; } } /** * * Misc. configuration value checks. * */ $mockScalarId = 1234; $partitionTableName = Hashmark::getModule('Partition', '', $mockDb)->getIntervalTableName($mockScalarId); $testDetail = "partition name with '" . Hashmark::getConfig('Partition', '', 'interval') . "' setting in Config/Partition.php.\n"; if ($partitionTableName) { echo "pass: Built {$partitionTableName} {$testDetail}"; } else { echo "====> FAIL: Could not build {$testDetail}"; }
*/ $likeExpr = ''; $alterExprs = array(); if (!$likeExpr) { die("\nNo LIKE expression.\n"); } if (!$alterExprs) { die("\nNo ALTER expression(s).\n"); } /** * For Hashmark::getModule(). */ require_once dirname(__FILE__) . '/../../Hashmark.php'; $db = Hashmark::getModule('DbHelper')->openDb('unittest'); if (!$db) { die("\nNo DB link.\n"); } $p = Hashmark::getModule('Partition', '', $db); $targets = $p->getTablesLike($likeExpr); if (!$targets) { die("\nNo matching tables.\n"); } foreach ($targets as $table) { foreach ($alterExprs as $alter) { $stmt = $db->query("ALTER TABLE `{$table}` {$alter}"); if ($stmt->rowCount()) { echo "`{$table}`: {$alter}\n"; } unset($stmt); } }
*/ /** * For Hashmark::getModule(). */ require_once dirname(__FILE__) . '/../../../bootstrap.php'; /** * For hashmark_random_samples(). */ require_once HASHMARK_ROOT_DIR . '/Test/Analyst/BasicDecimal/Tool/randomSamples.php'; define('HASHMARK_CREATESAMPLES_TYPE', 'decimal'); define('HASHMARK_CREATESAMPLES_SCALARS', 1); define('HASHMARK_CREATESAMPLES_COUNT', 10000); define('HASHMARK_CREATESAMPLES_STARTDATE', '2009-01-01 00:00:00 UTC'); define('HASHMARK_CREATESAMPLES_ENDDATE', '2009-01-01 23:59:59 UTC'); $db = Hashmark::getModule('DbHelper')->openDb('unittest'); $core = Hashmark::getModule('Core', '', $db); $partition = $core->getModule('Partition'); $rndSampleTime = 0; $createScalarTime = 0; $createSampleTime = 0; $totalSampleCnt = 0; $startDatetime = gmdate(HASHMARK_DATETIME_FORMAT); for ($scalars = 0; $scalars < HASHMARK_CREATESAMPLES_SCALARS; $scalars++) { $start = microtime(true); $samples = hashmark_random_samples(HASHMARK_CREATESAMPLES_TYPE, HASHMARK_CREATESAMPLES_STARTDATE, HASHMARK_CREATESAMPLES_ENDDATE, HASHMARK_CREATESAMPLES_COUNT); $end = microtime(true); $rndSampleTime += $end - $start; $scalarFields = array('type' => HASHMARK_CREATESAMPLES_TYPE, 'name' => Hashmark_Util::randomSha1()); $start = microtime(true); $scalarId = $core->createScalar($scalarFields); $end = microtime(true);
/** * @test * @group Client * @group scalarCreatedIfNotExistsWithNewSample * @group incr */ public function scalarCreatedIfNotExistsWithNewSample() { $this->_client->createScalarIfNotExists(true); $expectedName = self::randomString(); $expectedValue = self::randomDecimal(); $this->_client->incr($expectedName, $expectedValue, true); $scalar = $this->_core->getScalarByName($expectedName); $sample = Hashmark::getModule('Partition', '', $this->_db)->getLatestSample($scalar['id']); $this->assertDecimalEquals($expectedValue, $sample['value']); }
/** * Resources needed for most tests. * * @return void */ protected function setUp() { parent::setUp(); $this->_core = Hashmark::getModule('Core', '', $this->_db); }
/** * @test * @group DbDependent * @group expandsNamedMacros * @group expandSql */ public function expandsNamedMacros() { $mod = Hashmark::getModule('Core', '', $this->_db); // Verify that ':' prefixed macros get escaped/quoted; // that '@' prefixed get only escaped. $sql = 'SELECT * FROM `@table` WHERE `id` = :id OR `name` = :name'; $values = array('@table' => 'sca"lars', ':id' => 2, ':name' => 'two\'s'); $expectedSql = 'SELECT * FROM `sca\\"lars` WHERE `id` = 2 OR `name` = \'two\\\'s\''; $actualSql = $mod->expandSql($sql, $values); $this->assertEquals($expectedSql, $actualSql); }