<NME> tests.py
<BEF> #!/usr/bin/env python

import asyncio
import ipaddress
except ImportError:
    import trollius as asyncio
import unittest

import aiodns
import pycares
class DNSTest(unittest.TestCase):
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.addCleanup(self.loop.close)
        self.resolver = aiodns.DNSResolver(loop=self.loop, timeout=5.0)
        self.resolver.nameservers = ['8.8.8.8']

    def tearDown(self):
        self.resolver = None

    def test_query_a(self):
        f = self.resolver.query('google.com', 'A')
        result = self.loop.run_until_complete(f)

    def test_query_async_await(self):
        async def f():
            return await self.resolver.query('google.com', 'A')
        result = self.loop.run_until_complete(f())
        self.assertTrue(result)

    def test_query_a_bad(self):
        f = self.resolver.query('hgf8g2od29hdohid.com', 'A')
        try:
            self.loop.run_until_complete(f)
        except aiodns.error.DNSError as e:
            self.assertEqual(e.args[0], aiodns.error.ARES_ENOTFOUND)

    def test_query_aaaa(self):
        f = self.resolver.query('ipv6.google.com', 'AAAA')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_cname(self):
        f = self.resolver.query('www.amazon.com', 'CNAME')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_mx(self):
        f = self.resolver.query('google.com', 'MX')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_ns(self):
        f = self.resolver.query('google.com', 'NS')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_txt(self):
        f = self.resolver.query('google.com', 'TXT')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_soa(self):
        f = self.resolver.query('google.com', 'SOA')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_srv(self):
        f = self.resolver.query('_xmpp-server._tcp.jabber.org', 'SRV')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_naptr(self):
        f = self.resolver.query('sip2sip.info', 'NAPTR')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_ptr(self):
        ip = '8.8.8.8'
        f = self.resolver.query(ipaddress.ip_address(ip).reverse_pointer, 'PTR')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_bad_type(self):
        self.assertRaises(ValueError, self.resolver.query, 'google.com', 'XXX')

    def test_query_txt_chaos(self):
        self.resolver = aiodns.DNSResolver(loop=self.loop)
        self.resolver.nameservers = ['1.1.1.1']
        f = self.resolver.query('id.server', 'TXT', 'CHAOS')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_query_bad_class(self):
        self.assertRaises(ValueError, self.resolver.query, 'google.com', 'A', "INVALIDCLASS")

    def test_query_timeout(self):
        self.resolver = aiodns.DNSResolver(timeout=0.1, loop=self.loop)
        self.resolver.nameservers = ['1.2.3.4']
        f = self.resolver.query('google.com', 'A')
        try:
            self.loop.run_until_complete(f)
        except aiodns.error.DNSError as e:
            self.assertEqual(e.args[0], aiodns.error.ARES_ETIMEOUT)

    def test_query_cancel(self):
        f = self.resolver.query('google.com', 'A')
        self.resolver.cancel()
#        except asyncio.CancelledError as e:
#            self.assertTrue(e)


if __name__ == '__main__':
    unittest.main(verbosity=2)

    def test_gethostbyaddr(self):
        f = self.resolver.gethostbyaddr('127.0.0.1')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_gethostbyname_ipv6(self):
        f = self.resolver.gethostbyname('ipv6.google.com', socket.AF_INET6)
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)

    def test_gethostbyname_bad_family(self):
        f = self.resolver.gethostbyname('ipv6.google.com', -1)
        with self.assertRaises(aiodns.error.DNSError):
            self.loop.run_until_complete(f)

    def test_query_bad_chars(self):
        f = self.resolver.query('xn--cardeosapeluqueros-r0b.com', 'MX')
        result = self.loop.run_until_complete(f)
        self.assertTrue(result)


if __name__ == '__main__':
    unittest.main(verbosity=2)

<MSG> Fixed stopping watching file descriptor

<DFF> @@ -5,6 +5,7 @@ try:
 except ImportError:
     import trollius as asyncio
 import unittest
+import sys
 
 import aiodns
 import pycares
@@ -109,6 +110,27 @@ class DNSTest(unittest.TestCase):
 #        except asyncio.CancelledError as e:
 #            self.assertTrue(e)
 
+    def test_query_twice(self):
+        if sys.version[:3] >= '3.3':
+            exec('''if 1:
+            @asyncio.coroutine
+            def coro(self, host, qtype, n=2):
+                for i in range(n):
+                    result = yield from self.resolver.query(host, qtype)
+                    self.assertTrue(result)
+            ''')
+
+        else:
+            exec('''if 1:
+            @asyncio.coroutine
+            def coro(self, host, qtype, n=2):
+                for i in range(n):
+                    result = yield asyncio.From(self.resolver.query(host, qtype))
+                    self.assertTrue(result)
+            ''')
+
+        self.loop.run_until_complete(locals()['coro'](self, 'gmail.com', 'MX'))
+
 
 if __name__ == '__main__':
     unittest.main(verbosity=2)

{'commit': "diff --git a/aiodns/__init__.py b/aiodns/__init__.py\nindex 928a024..568835a 100644\n--- a/aiodns/__init__.py\n+++ b/aiodns/__init__.py\n@@ -38,7 +38,8 @@ class DNSResolver(object):\n         self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)\n         if nameservers:\n             self.nameservers = nameservers\n-        self._fds = set()\n+        self._read_fds = set()\n+        self._write_fds = set()\n         self._timer = None\n \n     @property\n@@ -72,15 +73,23 @@ class DNSResolver(object):\n         if readable or writable:\n             if readable:\n                 self.loop.add_reader(fd, self._handle_event, fd, READ)\n+                self._read_fds.add(fd)\n             if writable:\n                 self.loop.add_writer(fd, self._handle_event, fd, WRITE)\n-            self._fds.add(fd)\n+                self._write_fds.add(fd)\n             if self._timer is None:\n                 self._timer = self.loop.call_later(1.0, self._timer_cb)\n         else:\n             # socket is now closed\n-            self._fds.discard(fd)\n-            if not self._fds and self._timer is not None:\n+            if fd in self._read_fds:\n+                self._read_fds.discard(fd)\n+                self.loop.remove_reader(fd)\n+\n+            if fd in self._write_fds:\n+                self._write_fds.discard(fd)\n+                self.loop.remove_writer(fd)\n+\n+            if not self._read_fds and not self._write_fds and self._timer is not None:\n                 self._timer.cancel()\n                 self._timer = None\n \n@@ -94,7 +103,7 @@ class DNSResolver(object):\n         self._channel.process_fd(read_fd, write_fd)\n \n     def _timer_cb(self):\n-        if self._fds:\n+        if self._read_fds or self._write_fds:\n             self._channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)\n             self._timer = self.loop.call_later(1.0, self._timer_cb)\n         else:\ndiff --git a/tests.py b/tests.py\nindex de57cf8..9e78d54 100755\n--- a/tests.py\n+++ b/tests.py\n@@ -5,6 +5,7 @@ try:\n except ImportError:\n     import trollius as asyncio\n import unittest\n+import sys\n \n import aiodns\n import pycares\n@@ -109,6 +110,27 @@ class DNSTest(unittest.TestCase):\n #        except asyncio.CancelledError as e:\n #            self.assertTrue(e)\n \n+    def test_query_twice(self):\n+        if sys.version[:3] >= '3.3':\n+            exec('''if 1:\n+            @asyncio.coroutine\n+            def coro(self, host, qtype, n=2):\n+                for i in range(n):\n+                    result = yield from self.resolver.query(host, qtype)\n+                    self.assertTrue(result)\n+            ''')\n+\n+        else:\n+            exec('''if 1:\n+            @asyncio.coroutine\n+            def coro(self, host, qtype, n=2):\n+                for i in range(n):\n+                    result = yield asyncio.From(self.resolver.query(host, qtype))\n+                    self.assertTrue(result)\n+            ''')\n+\n+        self.loop.run_until_complete(locals()['coro'](self, 'gmail.com', 'MX'))\n+\n \n if __name__ == '__main__':\n     unittest.main(verbosity=2)\n", 'commit_subject': 'Fixed stopping watching file descriptor', 'repo_name': 'saghul/aiodns', 'committer': {'name': 'Saúl Ibarra Corretgé', 'email': '8a7991f6950ea189abf159d6ec98d2fcc13dc5f6@gmail.com', 'time_sec': '1411585194', 'tz_offset': '120', 'date': {'seconds': '1411585194'}}, 'commit_tree': '2e69a1fcdb5ea45bf7ba877ff217a134be159272', 'commit_message': 'Fixed stopping watching file descriptor\n', 'language_name': 'Python', 'license': 'mit', 'hunks': "@@ -5,6 +5,7 @@ try:\n except ImportError:\n     import trollius as asyncio\n import unittest\n+import sys\n \n import aiodns\n import pycares\n@@ -109,6 +110,27 @@ class DNSTest(unittest.TestCase):\n #        except asyncio.CancelledError as e:\n #            self.assertTrue(e)\n \n+    def test_query_twice(self):\n+        if sys.version[:3] >= '3.3':\n+            exec('''if 1:\n+            @asyncio.coroutine\n+            def coro(self, host, qtype, n=2):\n+                for i in range(n):\n+                    result = yield from self.resolver.query(host, qtype)\n+                    self.assertTrue(result)\n+            ''')\n+\n+        else:\n+            exec('''if 1:\n+            @asyncio.coroutine\n+            def coro(self, host, qtype, n=2):\n+                for i in range(n):\n+                    result = yield asyncio.From(self.resolver.query(host, qtype))\n+                    self.assertTrue(result)\n+            ''')\n+\n+        self.loop.run_until_complete(locals()['coro'](self, 'gmail.com', 'MX'))\n+\n \n if __name__ == '__main__':\n     unittest.main(verbosity=2)\n", 'addition_count': 22, 'deletion_count': 0, 'src_file': 'a/tests.py', 'tgt_file': 'b/tests.py', 'file_extension': '.py', 'before_file': '#!/usr/bin/env python\n\nimport asyncio\nimport ipaddress\nexcept ImportError:\n    import trollius as asyncio\nimport unittest\n\nimport aiodns\nimport pycares\nclass DNSTest(unittest.TestCase):\n    def setUp(self):\n        self.loop = asyncio.new_event_loop()\n        self.addCleanup(self.loop.close)\n        self.resolver = aiodns.DNSResolver(loop=self.loop, timeout=5.0)\n        self.resolver.nameservers = [\'8.8.8.8\']\n\n    def tearDown(self):\n        self.resolver = None\n\n    def test_query_a(self):\n        f = self.resolver.query(\'google.com\', \'A\')\n        result = self.loop.run_until_complete(f)\n\n    def test_query_async_await(self):\n        async def f():\n            return await self.resolver.query(\'google.com\', \'A\')\n        result = self.loop.run_until_complete(f())\n        self.assertTrue(result)\n\n    def test_query_a_bad(self):\n        f = self.resolver.query(\'hgf8g2od29hdohid.com\', \'A\')\n        try:\n            self.loop.run_until_complete(f)\n        except aiodns.error.DNSError as e:\n            self.assertEqual(e.args[0], aiodns.error.ARES_ENOTFOUND)\n\n    def test_query_aaaa(self):\n        f = self.resolver.query(\'ipv6.google.com\', \'AAAA\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_cname(self):\n        f = self.resolver.query(\'www.amazon.com\', \'CNAME\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_mx(self):\n        f = self.resolver.query(\'google.com\', \'MX\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_ns(self):\n        f = self.resolver.query(\'google.com\', \'NS\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_txt(self):\n        f = self.resolver.query(\'google.com\', \'TXT\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_soa(self):\n        f = self.resolver.query(\'google.com\', \'SOA\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_srv(self):\n        f = self.resolver.query(\'_xmpp-server._tcp.jabber.org\', \'SRV\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_naptr(self):\n        f = self.resolver.query(\'sip2sip.info\', \'NAPTR\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_ptr(self):\n        ip = \'8.8.8.8\'\n        f = self.resolver.query(ipaddress.ip_address(ip).reverse_pointer, \'PTR\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_bad_type(self):\n        self.assertRaises(ValueError, self.resolver.query, \'google.com\', \'XXX\')\n\n    def test_query_txt_chaos(self):\n        self.resolver = aiodns.DNSResolver(loop=self.loop)\n        self.resolver.nameservers = [\'1.1.1.1\']\n        f = self.resolver.query(\'id.server\', \'TXT\', \'CHAOS\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_query_bad_class(self):\n        self.assertRaises(ValueError, self.resolver.query, \'google.com\', \'A\', "INVALIDCLASS")\n\n    def test_query_timeout(self):\n        self.resolver = aiodns.DNSResolver(timeout=0.1, loop=self.loop)\n        self.resolver.nameservers = [\'1.2.3.4\']\n        f = self.resolver.query(\'google.com\', \'A\')\n        try:\n            self.loop.run_until_complete(f)\n        except aiodns.error.DNSError as e:\n            self.assertEqual(e.args[0], aiodns.error.ARES_ETIMEOUT)\n\n    def test_query_cancel(self):\n        f = self.resolver.query(\'google.com\', \'A\')\n        self.resolver.cancel()\n#        except asyncio.CancelledError as e:\n#            self.assertTrue(e)\n\n\nif __name__ == \'__main__\':\n    unittest.main(verbosity=2)\n\n    def test_gethostbyaddr(self):\n        f = self.resolver.gethostbyaddr(\'127.0.0.1\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_gethostbyname_ipv6(self):\n        f = self.resolver.gethostbyname(\'ipv6.google.com\', socket.AF_INET6)\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n    def test_gethostbyname_bad_family(self):\n        f = self.resolver.gethostbyname(\'ipv6.google.com\', -1)\n        with self.assertRaises(aiodns.error.DNSError):\n            self.loop.run_until_complete(f)\n\n    def test_query_bad_chars(self):\n        f = self.resolver.query(\'xn--cardeosapeluqueros-r0b.com\', \'MX\')\n        result = self.loop.run_until_complete(f)\n        self.assertTrue(result)\n\n\nif __name__ == \'__main__\':\n    unittest.main(verbosity=2)\n', 'message': 'Fixed stopping watching file descriptor\n', 'author_name': 'Saúl Ibarra Corretgé', 'lang': 'py'}