This is an archive of the discontinued Mercurial Phabricator instance.

util: add a file object proxy that can notify observers
ClosedPublic

Authored by indygreg on Feb 26 2018, 4:16 PM.

Details

Summary

There are various places in Mercurial where we may want to
instrument low-level I/O. The use cases I can think of all
involve development-type activities like monitoring the raw
bytes passing through a file (for testing and debugging),
counting the number of I/O function calls (for performance
monitoring), and changing the behavior of I/O function calls
(e.g. simulating a failure) (to facilitate testing).

This commit invents a mechanism to wrap a file object so we
can observe activity on it. We have similar functionality in
badserverext.py. But that's a test-only extension and is pretty
specific to the HTTP server. I would like a mechanism in core
that is sufficiently generic so it can be used by multiple
consumers, including hg debug* commands.

The added code consists of a proxy type for file objects.
It is bound to an "observer," which receives callbacks whenever
I/O methods are called.

We also add an implementation of an observer that logs specific
I/O events. This observer will be used in an upcoming commit
to record low-level wire protocol activity.

A helper function to convert a file object into an observed
file object has also been implemented.

I don't anticipate any critical functionality in core using
these types. So I don't think explicit test coverage is
worth implementing.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

indygreg created this revision.Feb 26 2018, 4:16 PM
lothiraldan added inline comments.
mercurial/util.py
535

Could we define the ours set at the class level so an extension/subclass could easily update it if needed?

indygreg added inline comments.Feb 27 2018, 4:46 PM
mercurial/util.py
535

That's a good idea. I can do it as a follow-up. There are already a ton of commits in this series and I want to avoid more churn.

durin42 accepted this revision.Mar 1 2018, 4:13 PM
This revision is now accepted and ready to land.Mar 1 2018, 4:13 PM
This revision was automatically updated to reflect the committed changes.