def test_remove_server(self): upstream = self._upstream([Server('1', 1), Server('2', 1)]) upstream.update({}, [Server('1', 1)]) self.assertEqual(len(upstream.servers), 2) self.assertEqual( len([server for server in upstream.servers if server is not None]), 1) self.assertEqual(_total_weight(upstream), 1)
def test_add_server(self): servers = [Server('1', 1), Server('2', 1)] upstream = self._upstream(servers) servers.append(Server('3', 1)) upstream.update({}, servers) self.assertEqual(len(upstream.servers), 3) self.assertEqual(_total_weight(upstream), 3)
def test_remove_add_server_one_by_one(self): upstream = self._upstream([Server('1', 1), Server('2', 1)]) upstream.update({}, [Server('2', 1)]) self.assertEqual(len(upstream.servers), 2) self.assertEqual(_total_weight(upstream), 1) upstream.update({}, [Server('2', 1), Server('3', 10)]) self.assertEqual(len(upstream.servers), 2) self.assertEqual(_total_weight(upstream), 11)
def test_create_with_rack_and_datacenter(self): upstream = self._upstream( [Server('1', 1, 'rack1', 'dc1'), Server('2', 1)]) self.assertEqual(len(upstream.servers), 2) self.assertEqual( [server.rack for server in upstream.servers if server is not None], ['rack1', None]) self.assertEqual([ server.datacenter for server in upstream.servers if server is not None ], ['dc1', None])
def test_borrow_server(self): upstream = self._upstream([Server('1', 2), Server('2', 1)]) _, address, _, _ = upstream.borrow_server() self.assertEqual(address, '1') _, address, _, _ = upstream.borrow_server() self.assertEqual(address, '2') _, address, _, _ = upstream.borrow_server() self.assertEqual(address, '1') _, address, _, _ = upstream.borrow_server() self.assertEqual(address, '1') self.assertEqual(_total_requests(upstream), 4)
def test_borrow_return_server(self): upstream = self._upstream( [Server('1', 1, rack='rack1'), Server('2', 5, rack='rack2')]) _, address, rack, _ = upstream.borrow_server() self.assertEqual(address, '1') self.assertEqual(rack, 'rack1') fd, address, rack, _ = upstream.borrow_server() self.assertEqual(address, '2') self.assertEqual(rack, 'rack2') upstream.return_server(fd) _, address, rack, _ = upstream.borrow_server() self.assertEqual(address, '2') self.assertEqual(rack, 'rack2') self.assertEqual(_total_requests(upstream), 2)
def test_replace_in_process(self): upstream = self._upstream([Server('1', 1), Server('2', 5)]) fd, address, _, _ = upstream.borrow_server() self.assertEqual(address, '1') _, address, _, _ = upstream.borrow_server() self.assertEqual(address, '2') server = Server('3', 1) upstream.update({}, [Server('1', 1), server]) self.assertEqual(_total_requests(upstream), 1) upstream.return_server(fd) self.assertEqual(_total_requests(upstream), 0) self.assertEqual(server.current_requests, 0) _, address, _, _ = upstream.borrow_server() self.assertEqual(address, '3')
def get_non_listening_server(): return Server('http://10.0.0.0:12345')
def get_server(handler, type): return Server('127.0.0.1:' + handler.get_argument(type))
def _upstream(weights): return Upstream('upstream', {}, [Server(str(weight), weight) for weight in weights])
def test_replace_server(self): upstream = self._upstream([Server('1', 1), Server('2', 1)]) upstream.update({}, [Server('1', 2), Server('2', 5)]) self.assertEqual(len(upstream.servers), 2) self.assertEqual(_total_weight(upstream), 7)
def test_create(self): upstream = self._upstream([Server('1', 1), Server('2', 1)]) self.assertEqual(len(upstream.servers), 2) self.assertEqual(_total_weight(upstream), 2)