Skip to content

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Sep 29, 2025

Remove MockClock()

Spawning from adding some logcontext debug logs in #18966 and since we're not logging at the set_current_context(...) level (see reasoning there), this removes some usage of set_current_context(...).

Specifically, MockClock.call_later(...) doesn't handle logcontexts correctly. It uses the calling logcontext as the callback context (wrong, as the logcontext could finish before the callback finishes) and it didn't reset back to the sentinel context before handing back to the reactor. It was like this since it was introduced 10+ years ago. Instead of fixing the implementation which would just be a copy of our normal Clock, we can just remove MockClock

synapse/tests/utils.py

Lines 284 to 300 in 84e1d15

def call_later(
self,
delay: float,
callback: Callable[P, object],
*args: P.args,
**kwargs: P.kwargs,
) -> Timer:
ctx = current_context()
def wrapped_callback() -> None:
set_current_context(ctx)
callback(*args, **kwargs)
t = Timer(self.now + delay, wrapped_callback, False)
self.timers.append(t)
return t

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)


class E2eRoomKeysHandlerTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
return self.setup_test_homeserver(replication_layer=mock.Mock())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I was dealing with setup_test_homeserver so much, I noticed this unused mock

extra_homeserver_attributes["clock"] = Clock(reactor, server_name=server_name)

if "clock" not in kwargs:
kwargs["clock"] = MockClock()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactoring away MockClock

Making all of these arguments way more clear to follow.

synapse.server.HomeServer
"""
kwargs = dict(kwargs)
kwargs.update(self._hs_args)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this more straight-forward by removing the self._hs_args and generic kwargs concept in favor of direct keyword-args.

@MadLittleMods MadLittleMods marked this pull request as ready for review September 29, 2025 23:25
@MadLittleMods MadLittleMods requested a review from a team as a code owner September 29, 2025 23:25
Comment on lines +102 to +103
logger = logging.getLogger(__name__)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left-over from debugging?

Suggested change
logger = logging.getLogger(__name__)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really care about getting rid of these? I always leave them around because it's annoying to re-add them later and our linting doesn't complain about them being unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging but happy to remove in a follow-up when you reply

@MadLittleMods MadLittleMods merged commit 5adb08f into develop Sep 30, 2025
107 of 112 checks passed
@MadLittleMods MadLittleMods deleted the madlittlemods/remove-mock-clock branch September 30, 2025 16:27
@MadLittleMods
Copy link
Contributor Author

Thanks for the review @anoadragon453 🐣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants