Exemple #1
0
class TestFailedRequests(unittest.TestCase):
    """Test for various conditions, that might result in the L{RequestFailed}-derived
    exceptions being raised.
    
    """
    
    def setUp(self):
        self.texpect = TExpect()
    
    def test_connection_loss_during_request(self):
        """
        The request is in progress and connection is (suddenly) broken
        
        """
        def on_failure(failure):
            self.assertIsInstance(failure.value, RequestInterruptedByConnectionLoss)
            self.assertEqual(failure.value.data, 'something')
            self.assertEqual(failure.value.promise.expecting, [re.compile('foo')])
            return True
        self.texpect._buf = 'something'
        d = self.texpect.expect(['foo'])
        dc = reactor.callLater(1, self.texpect.connectionLost, ConnectionDone())
        d.addErrback(on_failure)
        return d
    
    def test_request_timeout(self):
        """
        The request timed out, perhaps the system on the other side
        didn't produce what we expected
        
        """
        def on_failure(failure):
            self.assertIsInstance(failure.value, RequestTimeout)
            self.assertEqual(failure.value.data, 'something')
            self.assertEqual(failure.value.promise.expecting, [re.compile('foo')])
            return True
        self.texpect._buf = 'something'
        d = self.texpect.expect(['foo'], timeout=0.5)
        d.addErrback(on_failure)
        return d
    
    def test_hopeless_connection(self):
        """
        Connection is already closed by the time this request is made
        and the request cannot be completed at the moment. Since the connection is
        closed, it cannot be completed at all.
        
        """
        def on_failure(failure):
            self.assertIsInstance(failure.value, ConnectionAlreadyClosed)
            self.assertEqual(failure.value.data, 'something')
            self.assertEqual(failure.value.promise.expecting, [re.compile('foo')])
            return True
        self.texpect.eof = True
        self.texpect._buf = 'something'
        d = self.texpect.expect(['foo'])
        d.addErrback(on_failure)
        return d
Exemple #2
0
class TestSequence(unittest.TestCase):
    
    def setUp(self):
        self.texpect = TExpect()
        self.texpect.transport = StringTransport()
        self.texpect._buf = 'Welcome to Windows. Press Ctrl-Alt-Delete to begin'
    
    def test_double_read_1(self):
        """
        If the argument of the first request was in the buffer, the errback
        on the second one would've never been called
        (see L{test_wrong_sequence_no_failure}). As the documentation states,
        in such cases the behaviour is undefined
        
        """
        d1 = self.texpect.read_until('toz')
        d2 = self.texpect.read_until('Press')
        return self.failUnlessFailure(d2, OutOfSequenceError)
    
    def test_double_read_2(self):
        d1 = self.texpect.read_until('foobar')
        d2 = self.texpect.expect(['to'])
        return self.failUnlessFailure(d2, OutOfSequenceError)

    def test_wrong_sequence_no_failure(self):
        d1 = self.texpect.read_until('to')
        d2 = self.texpect.read_until('Press')
        
    def test_wrong_sequence_delayed(self):
        """
        Unlike L{test_wrong_sequence_no_failure}, this (rightfully) fails, because by the time
        data arrives, the erroneous second call to L{read_until} was already made
        
        """
        self.texpect._buf = ''
        dc = reactor.callLater(1, self.texpect.applicationDataReceived, 
                          'Welcome to Windows. Press Ctrl-Alt-Delete to begin')
        d1 = self.texpect.read_until('to')
        d2 = self.texpect.read_until('Press')
        self.addCleanup(dc.cancel)
        return self.failUnlessFailure(d2, OutOfSequenceError)
    
    def test_write_after_read(self):
        d1 = self.texpect.read_until('not-in-buffer')
        d2 = self.texpect.write('something')
        return self.failUnlessFailure(d2, OutOfSequenceError)
    
    def test_correct_sequence(self):
        d1 = self.texpect.read_until('to')
        d1.addCallback(lambda res: self.texpect.read_until('Press'))