Ejemplo n.º 1
0
    def test_onerror_onsuccess_returning_false(self, mock_urlopen):
        """Tests onerror onsuccess returning False, i.e. onsuccess must be called once"""
        
        data = ['none', '', 'google', '']  # supply an empty string otherwise urllib.read does not stop
        a = mock.Mock()
        a.read.side_effect = data
        mock_urlopen.return_value = a

        self.onsuccess_return_value = self.onerror_return_value = False
        # self.urls has a valid url (which should execute onsuccess) and an invalid one
        # which should execute onerror)
        read_async(self.urls, self.onsuccess, self.onerror)

        assert len(self.successes) == 1  # or alternatively:
        assert self.onsuccess.call_count == 1
Ejemplo n.º 2
0
    def test_mocking_urlread(self, mock_urlopen):
        """Tests onsuccess. WE mock urllib2urlopen.read to return user defined strings"""
        
        data = ['none', '', 'google', '']  # supply an empty string otherwise urllib.read does not stop
        a = mock.Mock()
        a.read.side_effect = data  # returns each item in list
        mock_urlopen.return_value = a

        # self.urls has a valid url (which should execute onsuccess) and an invalid one
        # which should execute onerror)
        read_async(self.urls, self.onsuccess, self.onerror)

        assert len(self.successes) == 2
        assert sorted(data) == ['', ''] + sorted(self.successes)  # sort them as the order might differ
        assert a.read.call_count == len(data)
Ejemplo n.º 3
0
    def test_urlerrors(self, mock_urlopen):
        """Tests onerror. WE mock urllib2urlopen.read to raise an excpected Exception"""
        
        data = ['none', '', 'google', '']  # supply an empty string otherwise urllib.read does not stop
        a = mock.Mock()
        def _(*a, **v):
            raise urllib2.URLError("")
        a.read.side_effect = urllib2.URLError("")  # raises it
        mock_urlopen.return_value = a

        # self.urls has a valid url (which should execute onsuccess) and an invalid one
        # which should execute onerror)
        read_async(self.urls, self.onsuccess, self.onerror)

        assert len(self.errors) == 2
        assert a.read.call_count == len(self.urls)
Ejemplo n.º 4
0
 def read_async(self, *a, **v):
     for obj, result, exc, url in read_async(*a, **v):
         assert _ismainthread()
         self.progress += 1
         if exc:
             self.errors.append(exc)
         else:
             self.successes.append(result)
Ejemplo n.º 5
0
    def test_onerror_onsuccess_returning_false2(self, mock_urlopen):
        """Tests onerror onsuccess returning False, i.e. the they must be called N times, where
        N < len(self.urls). WE mock urllib2urlopen.read with a time to wait in order to be sure
        that urllib2urlopen.read is also called LESS times than len(self.urls)"""

        # I want to test the above, PLUS that urllib2.urlopen.read is not called the total
        # amount of time. For that, let's the working thread make some work, otherwise we do not
        # see the difference as all wrkers have finished before calling the first 'onsuccess'

        
        # increase the number of url. Doesnt matter their names, out an int:
        urls = [str(i) for i in xrange(10)]

        # we cannot set assert urlib.read.call_count < expected_urllib_read_call_count
        # BECAUSE it might be equal (if all read are executed before first call to onsuccess)
        # On the other hand, testing that urlib.read.call_count <= expected_urllib_read_call_count
        # returns True also if they are equal, which does not assures me the onsuccess function
        # return value + threading works as expected. So we do some heuristic: we set a combination
        # of urls each and urls lengths. We assert that for  *at least one* holds:
        # urlib.read.call_count < expected_urllib_read_call_count
        how_many_one_strictly_lower_than = 0
        for blocksize, reads_per_url in product([1, 1024], [1, 10, 100]):
            # needs this to rest:
            self.setUp()
            
            # set the expected times we weill call urllib2.read:
            expected_urllib_read_call_count = len(urls) * reads_per_url
            # build return values which satisfy: for each url, read must be called reads_per_url
            # times
            data = len(urls) * (['x' * blocksize] * (reads_per_url-1) + [''])  # supply an empty string otherwise urllib.read does not stop
            urllib2_urlopen_read = mock.Mock()
            urllib2_urlopen_read.read.side_effect = data  # _read
            mock_urlopen.return_value = urllib2_urlopen_read
    
            self.onsuccess_return_value = self.onerror_return_value = False
            # self.urls has a valid url (which should execute onsuccess) and an invalid one
            # which should execute onerror)
            read_async(urls, self.onsuccess, self.onerror, blocksize=blocksize)
    
            assert self.onsuccess.call_count == 1
            assert urllib2_urlopen_read.read.call_count <= expected_urllib_read_call_count
            if urllib2_urlopen_read.read.call_count < expected_urllib_read_call_count:
                how_many_one_strictly_lower_than +=1

        assert how_many_one_strictly_lower_than > 0
Ejemplo n.º 6
0
 def read_async_raise_exc_in_called_func(self, *a, **v):
     """it is easy to check what happens if an unknown exception is raised from urllib: just mock it
     but what about an exception raised in the caller body, if urlread is ok? Check it here
     """
     for obj, result, exc, url in read_async(*a, **v):
         assert _ismainthread()
         raise KeyboardInterrupt()
         self.progress += 1
         if exc:
             self.errors.append(exc)
         else:
             self.successes.append(result)