Demystify and deprecate HomeserverTestCase.pump() (Twisted reactor/clock)#19602
Demystify and deprecate HomeserverTestCase.pump() (Twisted reactor/clock)#19602MadLittleMods merged 12 commits intodevelopfrom
HomeserverTestCase.pump() (Twisted reactor/clock)#19602Conversation
tests/unittest.py
Outdated
| specified (defaults to `0`, for some reason we also multiply by 100, so | ||
| `pump(1)` is 100 seconds in 1 second increments) and run whatever tasks are |
There was a problem hiding this comment.
for some reason we also multiply by 100, so
pump(1)is 100 seconds in 1 second increments
It looks like pump(...) was first introduced as self.reactor.pump([0.0] * 100) (matrix-org/synapse#3725) without explanation. I can only assume this was done so that when you create a deferred which creates more deferreds, it tries to run the whole chain to completion.
Then was updated to self.reactor.pump([by] * 100) (matrix-org/synapse#3740) without explanation. I assume some scheduled calls were in the mix and this was the patch.
There was a problem hiding this comment.
Being enough to run the full chain has always been my understanding (or should I say assumption)
There was a problem hiding this comment.
Added this assumed context to the docstring.
HomeserverTestCase.pump()HomeserverTestCase.pump() (Twisted reactor/clock)
tests/unittest.py
Outdated
|
|
||
| So for example, if you have some Synapse code that does | ||
| `clock.call_later(Duration(seconds=2), callback)`, then calling | ||
| `self.pump(0.02)` will advance time by 2 seconds, which is enough for that |
There was a problem hiding this comment.
true, but relying on the multiplication factor being 100 and writing this to meet a specific deadline feels pretty dirty.
(Doesn't mean we don't do it)
I'm pretty sure there is also .advance somewhere that does a one-off advance with the given duration, that feels a little more sensible and maybe using pump everywhere is an antipattern
There was a problem hiding this comment.
👍 on using self.reactor.advance(...) directly.
I can call out this function as deprecated and point out the alternative. Updated ✅
There was a problem hiding this comment.
Now wondering if we ought to add a self.advance so it's as easy/obvious as self.pump. I think the advance name also makes it sound less magical, but could be imaginary
There was a problem hiding this comment.
I could see as it as being useful to add our own docstring context to similarly demystify but we already self.reactor.advance(...) in many places and I'd rather not create another abstraction at this point.
tests/unittest.py
Outdated
| specified (defaults to `0`, for some reason we also multiply by 100, so | ||
| `pump(1)` is 100 seconds in 1 second increments) and run whatever tasks are |
There was a problem hiding this comment.
Being enough to run the full chain has always been my understanding (or should I say assumption)
HomeserverTestCase.pump() (Twisted reactor/clock)HomeserverTestCase.pump() (Twisted reactor/clock)
reivilibre
left a comment
There was a problem hiding this comment.
honestly anything you write is going to be better than what was there, just you opening this PR is giving me time to reflect deeper on this, which is not a bad thing :)
| def pump(self, by: float = 0.0) -> None: | ||
| """ | ||
| Pump the reactor enough that Deferreds will fire. | ||
| XXX: Deprecated: This method is deprecated. Use `self.reactor.advance(...)` |
There was a problem hiding this comment.
though even if we signpost advance more, maybe deprecating pump is a bit far. Making people write for _ in range(5): self.advance(0) in places may not necessarily make things more readable. Though in that case we could have a more explicit alternative advance_many where the 100 is replaced with an actual number.
Hard to say.
There was a problem hiding this comment.
My suspicion is that most pump() usage is bogus. And in the cases where it does something, we really just need to advance time for a scheduled thing to happen.
I'm ok with moving forward with deprecating as it encourages bad behavior.
There was a problem hiding this comment.
I'm coming around to this, yes let's deprecate it. Advancing 100 times internally 'just because' feels like bad practice to me.
I'd rather have a wrapper around advance
def advance(by = 0.0, require_calls = True)or some such.
require_calls (true by default) would assert that there is actually something to progress. I am not sure whether to define that to mean 'there is something on the queue' or (stronger) 'there is something on the queue that actually gets executed by this advance call'.
But this is derailing this PR, so let's leave it for later (maybe I have a crack at this later today for the curiosity)
tests/unittest.py
Outdated
| Pump the reactor enough that scheduled Deferreds will fire. | ||
|
|
||
| To demystify this function, it simply advances time by the number of seconds |
There was a problem hiding this comment.
| Pump the reactor enough that scheduled Deferreds will fire. | |
| To demystify this function, it simply advances time by the number of seconds | |
| Advances the test reactor's time by the number of seconds |
Just thinking defining 'pump' as 'Pump' is not helping anyone, let's just skip to the chase and tell the reader what this does.
There was a problem hiding this comment.
"Pump" is fine but it just wasn't clear "what happens"
| def pump(self, by: float = 0.0) -> None: | ||
| """ | ||
| Pump the reactor enough that Deferreds will fire. | ||
| XXX: Deprecated: This method is deprecated. Use `self.reactor.advance(...)` |
There was a problem hiding this comment.
I'm coming around to this, yes let's deprecate it. Advancing 100 times internally 'just because' feels like bad practice to me.
I'd rather have a wrapper around advance
def advance(by = 0.0, require_calls = True)or some such.
require_calls (true by default) would assert that there is actually something to progress. I am not sure whether to define that to mean 'there is something on the queue' or (stronger) 'there is something on the queue that actually gets executed by this advance call'.
But this is derailing this PR, so let's leave it for later (maybe I have a crack at this later today for the curiosity)
|
Thanks for the review @reivilibre 🦜 Comment is even more clear now 🧊 |
Demystify and deprecate
HomeserverTestCase.pump()(Twisted reactor/clock)Spawning from #18416 (comment)
Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.