'Testing whether downloading torrent metadata from another peer is successful'

@deferred(timeout=20)
def test_metadata_download(self):

    session1_port = self.session.config.get_dispersy_port()
    thumb_file = os.path.join(unicode(TESTS_DATA_DIR), u'41aea20908363a80d44234e8fef07fab506cd3b4', u'421px-Pots_10k_100k.jpeg')
    with open(thumb_file, 'rb') as f:
        self.thumb_data = f.read()
    thumb_hash = sha1(self.thumb_data).digest()
    thumb_hash_str = hexlify(thumb_hash)
    self.session.lm.metadata_store[thumb_hash_str] = self.thumb_data
    def start_download(_):
        candidate = Candidate(('127.0.0.1', session1_port), False)
        self.session2.lm.rtorrent_handler.download_metadata(candidate, thumb_hash, usercallback=do_check_download)
    def do_check_download(_):
        self.assertTrue(self.session2.lm.rtorrent_handler.has_metadata(thumb_hash))
        retrieved_data = self.session2.lm.rtorrent_handler.get_metadata(thumb_hash)
        self.assertEqual(retrieved_data, self.thumb_data, "metadata    doesn't    match")
        self.test_deferred.callback(None)
    self.setup_downloader().addCallback(start_download)
    return self.test_deferred
