/** * проверка номеров шард в соответствии с id клика */ public function testCheckLinearStrategyById() { $strategy = new LinearStrategy(1, 'lines', $this->sharding->getConfig()); $this->assertInstanceOf('MysqlShard\\Strategy\\LinearStrategy', $strategy); $this->assertEquals(0, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(1000000, 'lines', $this->sharding->getConfig()); $this->assertEquals(0, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(10000000, 'lines', $this->sharding->getConfig()); $this->assertEquals(0, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(100000000, 'lines', $this->sharding->getConfig()); $this->assertEquals(1, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(99999999, 'lines', $this->sharding->getConfig()); $this->assertEquals(0, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(LinearStrategy::MAX_RECORD_COUNT, 'lines', $this->sharding->getConfig()); $this->assertEquals(1, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(NULL, 'lines', $this->sharding->getConfig()); $strategy->setId(LinearStrategy::MAX_RECORD_COUNT + 1000); $this->assertEquals(1, $strategy->getShardId()); unset($strategy); $strategy = new LinearStrategy(LinearStrategy::MAX_RECORD_COUNT * 2 + 10000000, 'lines', $this->sharding->getConfig()); $this->assertEquals(2, $strategy->getTabId()); unset($strategy); $strategy = new LinearStrategy(LinearStrategy::MAX_RECORD_COUNT * 7, 'lines', $this->sharding->getConfig()); $this->assertEquals(7, $strategy->getTabId()); unset($strategy); $strategy = new LinearStrategy(LinearStrategy::MAX_RECORD_COUNT * 2 + 10000000, 'lines', $this->sharding->getConfig()); $res = $strategy->checkNewShard(LinearStrategy::MAX_RECORD_COUNT * 3 - 200, 2); $this->assertFalse($res); // как только мы переваливаем за порог осталось в шарде менее чем LinearStrategy::LIMIT_RECORDS, // то метод возвращает TRUE unset($strategy); $strategy = new LinearStrategy(null, 'lines', $this->sharding->getConfig()); $res = $strategy->checkNewShard(LinearStrategy::MAX_RECORD_COUNT * 3 - LinearStrategy::LIMIT_RECORDS + 1, 2); $this->assertTrue($res); unset($strategy); $id = LinearStrategy::MAX_RECORD_COUNT * 3 - LinearStrategy::LIMIT_RECORDS + 11; $strategy = new LinearStrategy(null, 'lines', $this->sharding->getConfig()); $strategy->setId($id); // тестируем переключение таблицы в редис $redis = Service::redis(1); $redis->set(R_KEY, "2"); unset($strategy); $id = LinearStrategy::MAX_RECORD_COUNT * 5 + 100; $strategy = new LinearStrategy($id, 'lines', $this->sharding->getConfig()); $this->assertEquals(5, $strategy->getTabId()); $id = 299999897; $strategy->setId($id); $this->assertEquals(2, $strategy->getTabId()); $strategy->checkInserted($id); $this->assertEquals("2", $redis->get(R_KEY)); $id = 299999898; $strategy->setId($id); $this->assertEquals(2, $strategy->getTabId()); $strategy->checkInserted($id); $this->assertEquals("2", $redis->get(R_KEY)); $id = 299999899; $strategy->setId($id); $this->assertEquals(2, $strategy->getTabId()); $strategy->checkInserted($id); $this->assertEquals("2", $redis->get(R_KEY)); $id = 299999900; $strategy->setId($id); $this->assertEquals(2, $strategy->getTabId()); $strategy->checkInserted($id); $this->assertEquals("2", $redis->get(R_KEY)); // тут происходит переключение $id = 299999901; $strategy->setId($id); $this->assertEquals(2, $strategy->getTabId()); $strategy->checkInserted($id); $this->assertEquals("3", $redis->get(R_KEY)); $id = 299999902; $strategy->setId($id); $this->assertEquals(2, $strategy->getTabId()); $strategy->checkInserted($id); $this->assertEquals("3", $redis->get(R_KEY)); }
/** * @param $tab_id * * @return void */ public function setTabIdToCache($tab_id) { $this->connectMysql(); $sql = 'UPDATE state SET tab_id_' . $this->_db . '=' . $tab_id; self::$mysql->query($sql); $redis = Service::redis(1); $redis->set(self::CACHE_KEY . $this->_db, $tab_id); }