'A callback added to a L{Deferred} after a previous callback attached
another L{Deferred} as a result is run after the callbacks of the other
L{Deferred} are run.'

def test_callbackOrderPreserved(self):

    results = []
    failures = []
    a = defer.Deferred()
    def cb(result):
        results.append(('cb', result))
        d = defer.Deferred()
        def firstCallback(ignored):
            results.append(('firstCallback', ignored))
            return defer.gatherResults([a])
        def secondCallback(result):
            results.append(('secondCallback', result))
        d.addCallback(firstCallback)
        d.addCallback(secondCallback)
        d.addErrback(failures.append)
        d.callback(None)
        return d
    outer = defer.Deferred()
    outer.addCallback(cb)
    outer.addCallback((lambda x: results.append('final')))
    outer.addErrback(failures.append)
    outer.callback('outer')
    self.assertEqual([('cb', 'outer'), ('firstCallback', None)], results)
    a.callback('withers')
    self.assertEqual([], failures)
    self.assertEqual(results, [('cb', 'outer'), ('firstCallback', None), ('secondCallback', ['withers']), 'final'])
