def test_future_etree_element(self): d = Doc('a') f = Future() f.set_result(etree.Element('b')) d.put(f) self.assertXmlAlmostEqual(d.to_etree_element(), """<?xml version='1.0' encoding='utf-8'?>\n<a><b/></a>""")
def test_future_list(self): d = Doc('a') f = Future() f.set_result([etree.Comment('ccc'), etree.Element('bbb')]) d.put(f) self.assertXmlAlmostEqual( d.to_etree_element(), """<?xml version='1.0' encoding='utf-8'?>\n<a><!--ccc--><bbb/></a>""" )
def test_failed_future(self): j = JsonBuilder() f = Future() result = RequestResult() result.set_exception(FailedRequestException(reason='error', code='code')) f.set_result(result) j.put(f) self.assertEqual(j.to_string(), """{"error": {"reason": "error", "code": "code"}}""")
def test_future_simple(self): d = Doc('a') f = Future() d.put(f) self.assertXmlAlmostEqual(d.to_etree_element(), """<?xml version='1.0' encoding='utf-8'?>\n<a/>""") f.set_result('test') self.assertXmlAlmostEqual(d.to_etree_element(), """<?xml version='1.0' encoding='utf-8'?>\n<a>test</a>""")
def test_finish(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) self.assertEqual(ag._finish_cb_called, False) ag.add_notification() ag.finish() self.assertEqual(ag._finish_cb_called, True) self.assertEqual(ag._aborted, False) self.assertEqual(f.result(), True)
def test_future(self): j = JsonBuilder() f = Future() j.put(f) self.assertFalse(j.is_empty()) self.assertEqual(j.to_string(), """{}""") f.set_result({'a': 'b'}) self.assertEqual(j.to_dict()['a'], 'b') self.assertEqual(j.to_string(), """{"a": "b"}""")
def test_failed_future(self): d = Doc('a') f = Future() result = RequestResult() result.set_exception(FailedRequestException(reason='error', code='code')) f.set_result(result) d.put(f) self.assertXmlAlmostEqual( d.to_etree_element(), """<?xml version='1.0' encoding='utf-8'?>\n<a><error reason="error" code="code"/></a>""" )
def post_url(self, url, data='', headers=None, files=None, connect_timeout=None, request_timeout=None, callback=None, follow_redirects=True, content_type=None, labels=None, add_to_finish_group=True, parse_response=True, parse_on_error=False): future = Future() request = frontik.util.make_post_request(url, data, headers, files, content_type, connect_timeout, request_timeout, follow_redirects) request._frontik_labels = labels self.fetcher_wrapper(request, partial(self._parse_response, future, callback, parse_response, parse_on_error), add_to_finish_group=add_to_finish_group) return future
def delete_url(self, url, data='', headers=None, connect_timeout=None, request_timeout=None, callback=None, error_callback=None, content_type=None, labels=None, add_to_finish_group=True, parse_response=True, parse_on_error=False): future = Future() request = frontik.util.make_delete_request(url, data, headers, content_type, connect_timeout, request_timeout) request._frontik_labels = labels self.fetch(request, partial(self._parse_response, future, callback, error_callback, parse_response, parse_on_error), add_to_finish_group=add_to_finish_group) return future
def test_notifications(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) not1 = ag.add_notification() not2 = ag.add_notification() self.assertEqual(ag._finish_cb_called, False) not1() self.assertEqual(ag._finish_cb_called, False) not2('params', are='ignored') self.assertEqual(ag._finish_cb_called, True) self.assertEqual(ag._aborted, False) self.assertEqual(f.result(), True)
def test_nested_future_error_node(self): j = JsonBuilder() f1 = Future() f2 = Future() f1.set_result({'nested': f2}) j.put(f1) self.assertEqual(j.to_string(), """{"nested": null}""") result = RequestResult() result.set_exception(FailedRequestException(reason='error', code='code')) f2.set_result( {'a': result} ) self.assertEqual( j.to_string(), """{"nested": {"a": {"error": {"reason": "error", "code": "code"}}}}""" )
def test_callbacks(self): result = 'result' f = Future() state = { 'callback1': False, 'callback2': False, 'callback3': False, } def callback1(future): state['callback1'] = True self.assertEqual(future.result(), result) def callback2(future): state['callback2'] = True self.assertEqual(future.result(), result) def callback3(future): state['callback3'] = True self.assertEqual(future.result(), result) f.add_done_callback(callback1) f.add_done_callback(callback2) self.assertFalse(state['callback1']) self.assertFalse(state['callback2']) self.assertFalse(state['callback3']) f.set_result(result) self.assertTrue(state['callback1']) self.assertTrue(state['callback2']) self.assertFalse(state['callback3']) f.add_done_callback(callback3) self.assertTrue(state['callback1']) self.assertTrue(state['callback2']) self.assertTrue(state['callback3'])
def test_single_data_set(self): f = Future() f.set_result('first') self.assertRaises(FutureStateException, f.set_result, 'second')
def test_nested_future(self): j = JsonBuilder() f1 = Future() f2 = Future() f3 = Future() f1.set_result({'nested': f2}) j.put(f1) self.assertEqual(j.to_string(), """{"nested": null}""") f2.set_result({'a': f3}) f3.set_result(['b', 'c']) self.assertEqual(j.to_string(), """{"nested": {"a": ["b", "c"]}}""")