<s> from . import unittest <EOL> from twisted . internet import defer <EOL> from twisted . names import dns , error <EOL> from mock import Mock <EOL> from synapse . http . endpoint import resolve_service <EOL> class DnsTestCase ( unittest . TestCase ) : <EOL> @ defer . inlineCallbacks <EOL> def test_resolve ( self ) : <EOL> dns_client_mock = Mock ( ) <EOL> service_name = "<STR_LIT>" <EOL> host_name = "<STR_LIT>" <EOL> ip_address = "<STR_LIT:127.0.0.1>" <EOL> answer_srv = dns . RRHeader ( <EOL> type = dns . SRV , <EOL> payload = dns . Record_SRV ( <EOL> target = host_name , <EOL> ) <EOL> ) <EOL> answer_a = dns . RRHeader ( <EOL> type = dns . A , <EOL> payload = dns . Record_A ( <EOL> address = ip_address , <EOL> ) <EOL> ) <EOL> dns_client_mock . lookupService . return_value = ( [ answer_srv ] , None , None ) <EOL> dns_client_mock . lookupAddress . return_value = ( [ answer_a ] , None , None ) <EOL> cache = { } <EOL> servers = yield resolve_service ( <EOL> service_name , dns_client = dns_client_mock , cache = cache <EOL> ) <EOL> dns_client_mock . lookupService . assert_called_once_with ( service_name ) <EOL> dns_client_mock . lookupAddress . assert_called_once_with ( host_name ) <EOL> self . assertEquals ( len ( servers ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( servers , cache [ service_name ] ) <EOL> self . assertEquals ( servers [ <NUM_LIT:0> ] . host , ip_address ) <EOL> @ defer . inlineCallbacks <EOL> def test_from_cache ( self ) : <EOL> dns_client_mock = Mock ( ) <EOL> dns_client_mock . lookupService . return_value = defer . fail ( error . DNSServerError ( ) ) <EOL> service_name = "<STR_LIT>" <EOL> cache = { <EOL> service_name : [ object ( ) ] <EOL> } <EOL> servers = yield resolve_service ( <EOL> service_name , dns_client = dns_client_mock , cache = cache <EOL> ) <EOL> dns_client_mock . lookupService . assert_called_once_with ( service_name ) <EOL> self . assertEquals ( len ( servers ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( servers , cache [ service_name ] ) <EOL> @ defer . inlineCallbacks <EOL> def test_empty_cache ( self ) : <EOL> dns_client_mock = Mock ( ) <EOL> dns_client_mock . lookupService . return_value = defer . fail ( error . DNSServerError ( ) ) <EOL> service_name = "<STR_LIT>" <EOL> cache = { } <EOL> with self . assertRaises ( error . DNSServerError ) : <EOL> yield resolve_service ( <EOL> service_name , dns_client = dns_client_mock , cache = cache <EOL> ) <EOL> @ defer . inlineCallbacks <EOL> def test_name_error ( self ) : <EOL> dns_client_mock = Mock ( ) <EOL> dns_client_mock . lookupService . return_value = defer . fail ( error . DNSNameError ( ) ) <EOL> service_name = "<STR_LIT>" <EOL> cache = { } <EOL> servers = yield resolve_service ( <EOL> service_name , dns_client = dns_client_mock , cache = cache <EOL> ) <EOL> self . assertEquals ( len ( servers ) , <NUM_LIT:0> ) <EOL> self . assertEquals ( len ( cache ) , <NUM_LIT:0> ) </s>
