public function test_should_mirror_table_router_set_and_get_works_fine() { try { Router::set('i am not exists'); $this->assertTrue(false); } catch (\Exception $e) { $this->assertTrue($e instanceof \Myfox\Lib\Exception); $this->assertContains('Undefined table named as "i am not exists"', $e->getMessage()); } $mirror = \Myfox\App\Model\Table::instance('mirror'); foreach (array(0, 1, 2, 0) as $key => $id) { Setting::set('last_assign_host', 0); $this->assertEquals(array('' => array(array('rows' => 1300, 'hosts' => '3,4,1,2', 'table' => 'mirror_0.t_' . $mirror->get('autokid') . '_' . $id))), Router::set('mirror', array(array('count' => 1300)))); if ($key == 0) { $this->assertEquals(array(), Router::get('mirror')); } } $this->assertEquals(1, Setting::get('last_assign_host')); $this->assertEquals(4, Setting::get('table_route_count', 'mirror')); $this->assertEquals(0, (int) Setting::get('table_real_count', 'mirror')); $where = Router::instance('mirror')->where(null); $route = self::$mysql->getRow(self::$mysql->query(sprintf("SELECT autokid,route_flag,real_table,hittime,hosts_list FROM %s WHERE table_name='mirror' AND route_flag=%d LIMIT 1", $where['table'], Router::FLAG_PRE_IMPORT))); $this->assertEquals(0, $route['hittime']); $this->assertEquals('$', $route['hosts_list']); $real_table = $route['real_table']; // XXX: 数据装完 $this->assertEquals(1, Router::effect('mirror', null, $route['real_table'], '1,2')); $this->assertEquals(array('hosts_list' => '1,2,$', 'route_flag' => Router::FLAG_IMPORT_END), self::$mysql->getRow(self::$mysql->query(sprintf("SELECT hosts_list,route_flag FROM %s WHERE table_name='mirror' AND real_table='%s' AND autokid = %u", $where['table'], $route['real_table'], $route['autokid'])))); $this->assertEquals(array(), Router::get('mirror')); // xxx: 模拟路由生效 Router::flush(); $route = Router::get('mirror', null, true); $this->assertEquals(1, count($route)); $route = reset($route); $this->assertTrue(0 < $route['mtime']); $this->assertEquals('1,2', $route['hosts']); $this->assertEquals($real_table, $route['table']); Router::removeAllCache(); $query = sprintf('SELECT hittime FROM %s WHERE %s AND route_flag=%d ORDER BY autokid DESC LIMIT 1', $where['table'], $where['where'], Router::FLAG_NORMAL_USE); $this->assertEquals(intval(time() / 2), intval(self::$mysql->getOne(self::$mysql->query($query)) / 2)); }