def fetch3(url): http_client = AsyncHTTPClient() fetch_future = http_client.fetch(url) future = gen.Future() def callback(f): result = f.result().body print('Done: ', future.done()) future.set_result(result) print('Done: ', future.done()) fetch_future.add_done_callback(callback) return (yield future)
def execute(): """Execute fn on the IOLoop.""" try: result = gen.maybe_future(fn(*args, **kwargs)) except Exception: # The function we ran didn't return a future and instead raised # an exception. Let's pretend that it returned this dummy # future with our stack trace. f = gen.Future() f.set_exc_info(sys.exc_info()) on_done(f) else: result.add_done_callback(on_done)
def test_filtered_cluster_node_stats(self, mock_get_current, mock_fetch, mock_ips_getter, mock_get_private_ip, mock_options): # Mock appscale_info functions for getting IPs mock_get_private_ip.return_value = '192.168.33.10' mock_ips_getter.return_value = ['192.168.33.10', '192.168.33.11'] # Mock secret mock_options.secret = 'secret' # Read test data from json file raw_test_data, stats_test_data = get_stats_from_file( 'node-stats.json', node_stats.NodeStatsSnapshot) # Mock local source mock_get_current.return_value = stats_test_data['192.168.33.10'] # Mock AsyncHTTPClient.fetch using raw stats dictionaries from test data response = MagicMock(body=json.dumps(raw_test_data['192.168.33.11']), code=200, reason='OK') future_response = gen.Future() future_response.set_result(response) mock_fetch.return_value = future_response #Prepare raw dict with include lists raw_include_lists = { 'node': ['cpu', 'memory'], 'node.cpu': ['percent', 'count'], 'node.memory': ['available'] } # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^ # Call method under test to get stats with filtered set of fields include_lists = IncludeLists(raw_include_lists) stats, failures = yield cluster_stats.cluster_nodes_stats.get_current( max_age=10, include_lists=include_lists) # ASSERTING EXPECTATIONS request_to_slave = mock_fetch.call_args[0][0] self.assertEqual(json.loads(request_to_slave.body), { 'max_age': 10, 'include_lists': raw_include_lists, }) self.assertEqual(request_to_slave.url, 'http://192.168.33.11:4378/stats/local/node') self.assertDictContainsSubset(request_to_slave.headers, {'Appscale-Secret': 'secret'}) self.assertEqual(failures, {}) local_stats = stats['192.168.33.10'] slave_stats = stats['192.168.33.11'] self.assertIsInstance(local_stats, node_stats.NodeStatsSnapshot) self.assertEqual(local_stats.utc_timestamp, 1494248091.0) self.assertIsInstance(slave_stats, node_stats.NodeStatsSnapshot) self.assertEqual(slave_stats.utc_timestamp, 1494248082.0)
def create_session_if_needed(self, session_id, handler=None): # this is because empty session_ids would be "falsey" and # potentially open up a way for clients to confuse us if len(session_id) == 0: raise ProtocolError("Session ID must not be empty") if session_id not in self._sessions and \ session_id not in self._pending_sessions: future = self._pending_sessions[session_id] = gen.Future() doc = Document() session_context = BokehSessionContext(session_id, self.server_context, doc) # using private attr so users only have access to a read-only property session_context._request = _RequestProxy(handler.request) session_context._path_args = [arg for arg in handler.path_args] session_context._path_kwargs = dict(handler.path_kwargs) # expose the session context to the document # use the _attribute to set the public property .session_context doc._session_context = session_context try: yield yield_for_all_futures( self._application.on_session_created(session_context)) except Exception as e: log.error("Failed to run session creation hooks %r", e, exc_info=True) self._application.initialize_document(doc) session = ServerSession(session_id, doc, io_loop=self._loop) del self._pending_sessions[session_id] self._sessions[session_id] = session session_context._set_session(session) self._session_contexts[session_id] = session_context # notify anyone waiting on the pending session future.set_result(session) if session_id in self._pending_sessions: # another create_session_if_needed is working on # creating this session session = yield self._pending_sessions[session_id] else: session = self._sessions[session_id] raise gen.Return(session)
def test_Militia(self): tu.print_test_header("test Militia") player2_discard_future = gen.Future() player3_discard_future = gen.Future() select_mock2 = unittest.mock.MagicMock( return_value=player2_discard_future) select_mock3 = unittest.mock.MagicMock( return_value=player3_discard_future) discard_mock2 = unittest.mock.Mock() discard_mock3 = unittest.mock.Mock() self.player2.select = select_mock2 self.player3.select = select_mock3 self.player2.discard = gen.coroutine(discard_mock2) self.player3.discard = gen.coroutine(discard_mock3) base.Militia(self.game, self.player1).play() self.assertTrue(select_mock2.called) self.assertTrue(select_mock3.called) select_mock2.assert_called_with( unittest.mock.ANY, unittest.mock.ANY, crd.card_list_to_titles(self.player2.hand.card_array()), unittest.mock.ANY) player2_selection = crd.card_list_to_titles( self.player2.hand.card_array())[:2] player2_discard_future.set_result(player2_selection) yield gen.moment discard_mock2.assert_called_once_with(player2_selection, self.player2.discard_pile) self.assertTrue(self.player1.last_mode["mode"] == "wait") player3_selection = ["Copper", "Copper"] player3_discard_future.set_result(player3_selection) yield gen.moment discard_mock3.assert_called_once_with(player3_selection, self.player3.discard_pile)
def test_filtered_cluster_proxies_stats(self, mock_fetch, mock_ips_getter, mock_get_private_ip, mock_options): # Mock appscale_info functions for getting IPs mock_get_private_ip.return_value = '192.168.33.10' mock_ips_getter.return_value = ['192.168.33.11'] # Mock secret mock_options.secret = 'secret' # Read test data from json file raw_test_data = get_stats_from_file( 'proxies-stats.json', proxy_stats.ProxiesStatsSnapshot)[0] #Prepare raw dict with include lists raw_include_lists = { 'proxy': [ 'name', 'unified_service_name', 'application_id', 'frontend', 'backend' ], 'proxy.frontend': ['scur', 'smax', 'rate', 'req_rate', 'req_tot'], 'proxy.backend': ['qcur', 'scur', 'hrsp_5xx', 'qtime', 'rtime'], } # Mock AsyncHTTPClient.fetch using raw stats dictionaries from test data response = MagicMock(body=json.dumps(raw_test_data['192.168.33.11']), code=200, reason='OK') future_response = gen.Future() future_response.set_result(response) mock_fetch.return_value = future_response # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^ # Call method under test to get stats with filtered set of fields include_lists = IncludeLists(raw_include_lists) stats, failures = yield cluster_stats.cluster_proxies_stats.get_current( max_age=18, include_lists=include_lists) # ASSERTING EXPECTATIONS request_to_lb = mock_fetch.call_args[0][0] self.assertEqual(json.loads(request_to_lb.body), { 'max_age': 18, 'include_lists': raw_include_lists, }) self.assertEqual(request_to_lb.url, 'http://192.168.33.11:4378/stats/local/proxies') self.assertDictContainsSubset(request_to_lb.headers, {'Appscale-Secret': 'secret'}) self.assertEqual(failures, {}) lb_stats = stats['192.168.33.11'] self.assertIsInstance(lb_stats, proxy_stats.ProxiesStatsSnapshot) self.assertEqual(len(lb_stats.proxies_stats), 5) self.assertEqual(lb_stats.utc_timestamp, 1494248097.0)
def test_dynamic_delete(self): async_true = gen.Future() async_true.set_result(True) entity_lock = flexmock(EntityLock) entity_lock.should_receive('acquire').and_return(async_true) entity_lock.should_receive('release') del_request = flexmock() del_request.should_receive("key_list") del_request.should_receive("has_transaction").never() del_request.should_receive("transaction").never() db_batch = flexmock() db_batch.should_receive('valid_data_version_sync').and_return(True) transaction_manager = flexmock( create_transaction_id=lambda project, xg: 1, delete_transaction_id=lambda project, txid: None, set_groups=lambda project_id, txid, groups: None) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) yield dd.dynamic_delete("appid", del_request) fake_key = entity_pb.Reference() fake_key.set_app('foo') path = fake_key.mutable_path() element = path.add_element() element.set_type('bar') element.set_id(1) del_request = flexmock() del_request.should_receive("key_list").and_return([fake_key]) del_request.should_receive("has_transaction").and_return(True) transaction = flexmock() transaction.should_receive("handle").and_return(1) del_request.should_receive("transaction").and_return(transaction) del_request.should_receive("has_mark_changes").and_return(False) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) flexmock(utils).should_receive("get_entity_kind").and_return("kind") db_batch.should_receive('delete_entities_tx').and_return(ASYNC_NONE) yield dd.dynamic_delete("appid", del_request) del_request = flexmock() del_request.should_receive("key_list").and_return([fake_key]) del_request.should_receive("has_transaction").and_return(False) del_request.should_receive("has_mark_changes").and_return(False) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) flexmock(dd).should_receive("delete_entities").and_return(ASYNC_NONE).once() yield dd.dynamic_delete("appid", del_request)
def fwrap(gf, ioloop=None): """ Wraps a GRPC result in a future that can be yielded by tornado Usage:: @coroutine def my_fn(param): result = yield fwrap(stub.function_name.future(param, timeout)) """ f = gen.Future() if ioloop is None: ioloop = IOLoop.current() gf.add_done_callback(lambda _: ioloop.add_callback(_fwrap, f, gf)) return f
def task_async(pid): """Same task but non blocking. We could just use a coroutine here but I went for a quick test for Futures. Before finding that gevent 1.1a2 was compatible with Python3. """ task_future = gen.Future() sleep_future = gen.sleep(random.randint(0, 2)*0.001) def callback(f): task_future.set_result(f.set_result) print('Task %s done' % pid) sleep_future.add_done_callback(callback) return task_future
def test_tr_tr_duration(self): tu.print_test_header("Test throne room throne room duration") tr = base.Throne_Room(self.game, self.player1) caravan = sea.Caravan(self.game, self.player1) tu.add_many_to_hand(self.player1, tr, 2) tu.add_many_to_hand(self.player1, caravan, 2) player1_tr_1_select_future = gen.Future() player1_tr_2_select_future = gen.Future() player1_tr_3_select_future = gen.Future() self.player1.select = unittest.mock.Mock(side_effect=[ player1_tr_1_select_future, player1_tr_2_select_future, player1_tr_3_select_future ]) tr.play() player1_tr_1_select_future.set_result(["Throne Room"]) yield gen.moment player1_tr_2_select_future.set_result(["Caravan"]) yield gen.moment player1_tr_3_select_future.set_result(["Caravan"]) yield gen.moment self.assertTrue(len(self.player1.durations) == 3) self.assertTrue(len(self.player1.played_cards) == 1) self.assertTrue(len(self.player1.duration_cbs) == 6)
def test_Chancellor(self): tu.print_test_header("test Chancellor") self.player1.discard_pile.append( supply_cards.Copper(self.game, self.player1)) chancellor = base.Chancellor(self.game, self.player1) self.player1.hand.add(chancellor) select_future = gen.Future() self.player1.select = unittest.mock.Mock(return_value=select_future) chancellor.play() self.assertTrue(len(self.player1.discard_pile) == 1) decksize = len(self.player1.deck) select_future.set_result(["Yes"]) yield gen.moment self.assertTrue(len(self.player1.discard_pile) == decksize + 1) self.assertTrue(len(self.player1.deck) == 0)
def emit(self, event, data, ack_callback=None): acknowledge = str(uuid.uuid4()) self.send({ 'acknowledge': acknowledge, 'type': 'event', 'event': event, 'data': data }) if ack_callback: future = gen.Future() BaseSocketHandler._pending_futures[acknowledge] = future ioloop.IOLoop.current().add_future(future, ack_callback) ioloop.IOLoop.current().add_timeout(timezone.timedelta(minutes=1), self.remove_pending_future, *(acknowledge, ))
def get(self): name = self.get_query_argument("name", "") force_refresh = self.get_query_argument("force_refresh", 0) if force_refresh: future = gen.Future() tasks.put((future, spider.get_session, (True, ), {})) yield future if name: self.write(spider.session.get(name, "").encode("utf-8")) else: self.write(("&".join([ "%s=%s" % (key, value) for key, value in spider.session.iteritems() ])).encode("utf-8"))
def test_verbose_cluster_processes_stats(self, mock_get_current, mock_fetch, mock_ips_getter, mock_get_private_ip, mock_options): # Mock appscale_info functions for getting IPs mock_get_private_ip.return_value = '192.168.33.10' mock_ips_getter.return_value = ['192.168.33.10', '192.168.33.11'] # Mock secret mock_options.secret = 'secret' # Read test data from json file raw_test_data, stats_test_data = get_stats_from_file( 'processes-stats.json', process_stats.ProcessesStatsSnapshot) # Mock local source mock_get_current.return_value = stats_test_data['192.168.33.10'] # Mock AsyncHTTPClient.fetch using raw stats dictionaries from test data response = MagicMock(body=json.dumps(raw_test_data['192.168.33.11']), code=200, reason='OK') future_response = gen.Future() future_response.set_result(response) mock_fetch.return_value = future_response # Initialize stats source cluster_stats_source = cluster_stats.ClusterProcessesStatsSource() # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^ # Call method under test to get the latest stats stats, failures = yield cluster_stats_source.get_current_async() # ASSERTING EXPECTATIONS request_to_slave = mock_fetch.call_args[0][0] self.assertEqual(json.loads(request_to_slave.body), {}) self.assertEqual(request_to_slave.url, 'http://192.168.33.11:4378/stats/local/processes') self.assertDictContainsSubset(request_to_slave.headers, {'Appscale-Secret': 'secret'}) self.assertEqual(failures, {}) local_stats = stats['192.168.33.10'] slave_stats = stats['192.168.33.11'] self.assertIsInstance(local_stats, process_stats.ProcessesStatsSnapshot) self.assertEqual(len(local_stats.processes_stats), 24) self.assertEqual(local_stats.utc_timestamp, 1494248000.0) self.assertIsInstance(slave_stats, process_stats.ProcessesStatsSnapshot) self.assertEqual(len(slave_stats.processes_stats), 10) self.assertEqual(slave_stats.utc_timestamp, 1494248091.0)
def _main(): fwait = gen.Future() sc = stack_context.StackContext(_mgr) assert _current_stackcontext() is None with sc: assert _current_stackcontext() is sc f = greenado.gcall(_gthing, sc, fwait) assert _current_stackcontext() is sc fwait.set_result(True) assert _current_stackcontext() is sc assert _current_stackcontext() is None return f
def test_Treasury(self): tu.print_test_header("test Treasury") treasury1 = sea.Treasury(self.game, self.player1) treasury2 = sea.Treasury(self.game, self.player1) self.player1.hand.add(treasury1) self.player1.hand.add(treasury2) select_future = gen.Future() self.player1.select = unittest.mock.Mock(return_value=select_future) tu.send_input(self.player1, "play", "Treasury") tu.send_input(self.player1, "play", "Treasury") self.player1.end_turn() self.player1.select.assert_called_once_with(1, 1, [0, 1, 2], unittest.mock.ANY) select_future.set_result([2]) yield gen.sleep(.1) self.assertTrue(self.player1.hand.get_count("Treasury") == 2)
def cluster_safe_cmd(client, cmd, workers=None): exe = partial(safe_cmd, escape_cmd(cmd)) if client.asynchronous: result_future = gen.Future() @gen.coroutine def async_exe(x, fu): r = yield client.run(exe, workers=workers) fu.set_result(r) client.loop.add_callback(partial(async_exe, exe, result_future)) return result_future else: r = client.run(exe) return r
def test_Library(self): tu.print_test_header("test Library") library = base.Library(self.game, self.player1) village = base.Village(self.game, self.player1) copper = supply_cards.Copper(self.game, self.player1) self.player1.deck = [copper, village, copper] self.player1.hand.add(library) self.player1.hand.add = unittest.mock.Mock() select_future = gen.Future() self.player1.select = unittest.mock.Mock(return_value=select_future) library.play() self.player1.hand.add.assert_called_with(copper) select_future.set_result(["Yes"]) yield gen.moment self.player1.hand.add.assert_called_with(copper) self.assertTrue("Village" not in self.player1.hand)
def test_Moat_reaction(self): tu.print_test_header("test Moat Reaction") moat = base.Moat(self.game, self.player2) self.player2.hand.add(moat) moat.react = unittest.mock.Mock(moat, wraps=moat.react) self.player2.gain = unittest.mock.Mock() reveal_future = gen.Future() reveal_mock = unittest.mock.MagicMock(return_value=reveal_future) self.player2.select = reveal_mock base.Witch(self.game, self.player1).play() self.assertTrue(moat.react.called) reveal_future.set_result(["Reveal"]) yield gen.moment self.assertTrue(self.player2.protection == 1) self.assertFalse(self.player2.gain.called) self.assertTrue(self.player2.search_and_extract_card("Curse") == None)
def update(self, x, who=None): future = gen.Future() self.futures.append(future) @gen.coroutine def _(): while True: try: # this runs asynchronously, in C-K's thread self.producer.produce(self.topic, x, callback=self.cb) return except BufferError: yield gen.sleep(self.polltime) except Exception as e: future.set_exception(e) return self.loop.add_callback(_) return future
def run_custom_cmd(repo_service, custom_cmd, interactive=False, board='dummyboard'): future = gen.Future() _schedule_build(repo_service, None, TYPE_CUSTOM, board, custom_cmd=custom_cmd, interactive=interactive, callback=future.set_result) build = yield future if build.exit_code: raise BuildException('custom build command failed') return build
def test_cannot_authenticate_a_user_with_invalid_google_plus_token(self): with patch.object(AuthenticateHandler, '_fetch_google_userinfo') as auth_mock: result = gen.Future() response_mock = Mock(code=401, body=('Unauthorized')) result.set_result(response_mock) auth_mock.return_value = result try: response = yield self.anonymous_fetch('/authenticate', method='POST', body=dumps({ 'provider': 'GooglePlus', 'access_token': 'INVALID-TOKEN', })) except HTTPError, e: response = e.response
def test_Cellar(self): tu.print_test_header("test Cellar") selection_future = gen.Future() select_mock = unittest.mock.MagicMock(return_value=selection_future) draw_mock = unittest.mock.Mock() discard_mock = unittest.mock.Mock() self.player1.select = select_mock self.player1.draw = draw_mock self.player1.discard = gen.coroutine(discard_mock) base.Cellar(self.game, self.player1).play() self.assertTrue(select_mock.called) to_discard = crd.card_list_to_titles(self.player1.hand.card_array()) selection_future.set_result(to_discard) yield gen.moment discard_mock.assert_any_call(to_discard, self.player1.discard_pile) draw_mock.assert_called_once_with(5)
def test_Throne_Room_on_Village(self): tu.print_test_header("test Throne Room Village") throne_room_card = base.Throne_Room(self.game, self.player1) village = base.Village(self.game, self.player1) self.player1.hand.add(throne_room_card) self.player1.hand.add(village) village.play = unittest.mock.Mock() select_future = gen.Future() throne_room_selection = unittest.mock.MagicMock( return_value=select_future) self.player1.select = throne_room_selection throne_room_card.play() select_future.set_result(["Village"]) yield gen.moment village.play.assert_called_with(True) self.assertTrue(village.play.call_count == 2)
def test_advertise_should_take_a_router_file(router_file): from tchannel.tornado.response import Response as TornadoResponse tchannel = TChannel(name='client') with open(router_file, 'r') as json_data: routers = json.load(json_data) with (patch( 'tchannel.tornado.TChannel.advertise', autospec=True, )) as mock_advertise: f = gen.Future() mock_advertise.return_value = f f.set_result(TornadoResponse()) tchannel.advertise(router_file=router_file) mock_advertise.assert_called_once_with(ANY, routers=routers, name=ANY, timeout=ANY)
def test_remote_failure(self, mock_get_current, mock_fetch, mock_ips_getter, mock_get_private_ip, mock_options): # Mock appscale_info functions for getting IPs mock_get_private_ip.return_value = '192.168.33.10' mock_ips_getter.return_value = ['192.168.33.10', '192.168.33.11'] # Mock secret mock_options.secret = 'secret' # Read test data from json file stats_test_data = get_stats_from_file( 'node-stats.json', node_stats.NodeStatsSnapshot )[1] # Mock local source mock_get_current.return_value = stats_test_data['192.168.33.10'] # Mock AsyncHTTPClient.fetch using raw stats dictionaries from test data response = MagicMock(code=500, reason="Timeout error") future_response = gen.Future() future_response.set_result(response) mock_fetch.return_value = future_response # Initialize cluster stats source cluster_stats_source = cluster_stats.ClusterNodesStatsSource() # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^ # Call method under test stats, failures = yield cluster_stats_source.get_current_async() # ASSERTING EXPECTATIONS request_to_slave = mock_fetch.call_args[0][0] self.assertEqual(json.loads(request_to_slave.body), {}) self.assertEqual( request_to_slave.url, 'http://192.168.33.11:4378/stats/local/node' ) self.assertDictContainsSubset( request_to_slave.headers, {'Appscale-Secret': 'secret'} ) local_stats = stats['192.168.33.10'] self.assertNotIn('192.168.33.11', stats) self.assertIsInstance(local_stats, node_stats.NodeStatsSnapshot) self.assertEqual(local_stats.utc_timestamp, 1494248091.0) self.assertEqual(failures, {'192.168.33.11': '500 Timeout error'})
def connect(self): """Get a connection to this peer. If an connection to the peer already exists (either incoming or outgoing), that's returned. Otherwise, a new outgoing connection to this peer is created. :return: A future containing a connection to this host. """ # Prefer incoming connections over outgoing connections. if self._connections: # First value is an incoming connection future = gen.Future() future.set_result(self._connections[0]) return future if self._connecting: # If we're in the process of connecting to the peer, just wait # and re-use that connection. return self._connecting conn_future = self._connecting = self.connection_class.outgoing( hostport=self.hostport, process_name=self.tchannel.process_name, serve_hostport=self.tchannel.hostport, handler=self.tchannel.receive_call, tchannel=self.tchannel, ) def on_connect(_): if not conn_future.exception(): # We don't actually need to handle the exception. That's on # the caller. connection = conn_future.result() self._connections.append(connection) self._set_on_close_cb(connection) self._connecting = None conn_future.add_done_callback(on_connect) return conn_future
def create_session_if_needed(self, session_id): # this is because empty session_ids would be "falsey" and # potentially open up a way for clients to confuse us if len(session_id) == 0: raise ProtocolError("Session ID must not be empty") if session_id not in self._sessions and \ session_id not in self._pending_sessions: future = self._pending_sessions[session_id] = gen.Future() doc = Document() session_context = BokehSessionContext(session_id, self._server_context, doc) try: result = yield yield_for_all_futures( self._application.on_session_created(session_context)) except Exception as e: log.error("Failed to run session creation hooks %r", e, exc_info=True) self._application.initialize_document(doc) session = ServerSession(session_id, doc, io_loop=self._loop) del self._pending_sessions[session_id] self._sessions[session_id] = session session_context._set_session(session) self._session_contexts[session_id] = session_context # notify anyone waiting on the pending session future.set_result(session) if session_id in self._pending_sessions: # another create_session_if_needed is working on # creating this session session = yield self._pending_sessions[session_id] else: session = self._sessions[session_id] raise gen.Return(session)
def build(self): if self._canceling: raise ValueError('Cancel in progress') if not self.building: self.canceled = False self._future = future = gen.Future() self.building = True try: yield self._run_build() future.set_result(True) except Exception as e: if str(e) == 'Aborted': future.set_result(False) else: future.set_exception(e) finally: self.building = False try: yield self._future except Exception as e: raise e
def pull(self, seqid, iprot, oprot): if self._result_future: self._oprot.trans = self._otrans self._result_future.set_result(True) self._seqid = seqid self._iprot = iprot self._oprot = oprot self._writing = False self._stoped = False self._result_future = gen.Future() self._otrans = self._oprot.trans self._oprot.trans = BytesIO() if self._buffer and not self._writing: self._writing = True self.do_write() return self._result_future