← Back to team overview

launchpad-reviewers team mailing list archive

Re: [Merge] lp:~julian-edwards/launchpad/buildd-manager-parallel-scan into lp:launchpad/devel

 

On Mon, Aug 2, 2010 at 4:37 PM, Julian Edwards
<julian.edwards@xxxxxxxxxxxxx> wrote:
> On Monday 02 August 2010 16:01:42 Jonathan Lange wrote:
...
>> When you do want to mix things up, you just combine the two. Say for
>> example you want to write a function that returns a Deferred that will
>> fire in a given number of seconds from now, and you want to test it:
>>
>> def test_run_later_success(self):
>>     clock = Clock()
>>     d = run_later(clock, 5, math.sqrt, 49)
>>     d.addCallback(self.assertEqual, 7)
>>     clock.advance(7)
>>     return d
>>
>> (There's also another trick you can do here, but you're probably bored by
>> now).
>
> Not at all.  I am enjoying the useful advice.
>

Thank you for saying so.

So here's the other trick.

In the case where you have a Deferred that fires when the clock hits a
point, and you've advanced the clock past that point, then you *know*
that Deferred has fired. This is very, very rarely the case in actual
production code, but is sometimes the case in test code.

Because you know the Deferred has fired, you can also do something
naughty like this:
  d = run_later(clock, 5, math.sqrt, 49)
  clock.advance(7)
  placeholder_list = []
  # This callback will be run instantly because we know the Deferred has fired.
  d.addCallback(placeholder_list.append)
  self.assertEqual([7], placeholder_list)

Then you don't need to return the Deferred.

We use a list because it's the easiest way to have a function mutate
something in the current scope.

Launchpad has a helper that does this dance, see
lp.services.twistedsupport.extract_result.

jml
-- 
https://code.launchpad.net/~julian-edwards/launchpad/buildd-manager-parallel-scan/+merge/30672
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~julian-edwards/launchpad/buildd-manager-parallel-scan into lp:launchpad/devel.



References