'L{Deferred}s can have callbacks that themselves return L{Deferred}s.
These L{Deferred}s can have other callbacks added before they are
returned, which subtly changes the callback chain. When these "inner"
L{Deferred}s fire (even asynchronously), the outer callback chain
continues.'

def test_nestedAsynchronousChainedDeferredsWithExtraCallbacks(self):

    results = []
    failures = []
    inner = defer.Deferred()
    def cb(result):
        results.append(('start-of-cb', result))
        d = defer.succeed('inner')
        def firstCallback(ignored):
            results.append(('firstCallback', ignored))
            return inner.addCallback((lambda x: [x]))
        def secondCallback(result):
            results.append(('secondCallback', result))
            return (result * 2)
        d.addCallback(firstCallback)
        d.addCallback(secondCallback)
        d.addErrback(failures.append)
        return d
    outer = defer.succeed('outer')
    outer.addCallback(cb)
    outer.addCallback(results.append)
    self.assertEqual(results, [('start-of-cb', 'outer'), ('firstCallback', 'inner')])
    inner.callback('withers')
    outer.addErrback(failures.append)
    inner.addErrback(failures.append)
    self.assertEqual([], failures, "Got    errbacks    but    wasn't    expecting    any.")
    self.assertEqual(results, [('start-of-cb', 'outer'), ('firstCallback', 'inner'), ('secondCallback', ['withers']), ['withers', 'withers']])
