Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable with statement #185

Merged
merged 7 commits into from
Oct 15, 2020
Merged

Conversation

lelutin
Copy link
Contributor

@lelutin lelutin commented Oct 10, 2020

WIth this change it becomes possible to enclose puppetdb calls inside a "with" block so that all HTTP connections get explicitely closed when we're done.

I've had to explicitly close connections when working with paramiko for tunneling puppetdb requests through ssh.

By implementing those two functions, the BaseAPI object can be used with
a 'with' statement like this:

    with pypuppetdb.connect() as session:
        # ...

This will ensure that all HTTP connections are explicitly closed when
exiting the "with" context, thus avoiding possible hangs because of the
open connects.

As stated in the comment, this can happen if our HTTP connections are
sent through a tunnel (for example an ssh tunnel) which will then wait
for connections to close before closing the tunnel.

Explicitly closing HTTP connections will indicate to any tunnel that
we're done with our work and that the tunnel can also be closed in turn.
@coveralls
Copy link

coveralls commented Oct 10, 2020

Coverage Status

Coverage increased (+0.1%) to 83.718% when pulling 45d1e79 on lelutin:enable_wth_statement into 3a5f5f7 on voxpupuli:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 83.419% when pulling 0e65ee7 on lelutin:enable_wth_statement into 3a5f5f7 on voxpupuli:master.

@kenyon kenyon changed the title Enable wth statement Enable with statement Oct 11, 2020
igalic
igalic previously requested changes Oct 11, 2020
Copy link
Contributor

@igalic igalic left a comment

Choose a reason for hiding this comment

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

Nice!
one minor request


def test_connect_with_statement():
with pypuppetdb.connect() as puppetdb:
assert puppetdb.version == 'v4'
Copy link
Contributor

Choose a reason for hiding this comment

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

can we also assert that the connection is closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's a pretty pertient request! I was wondering the same when writing the test and ended up with the interrogation "how can I check this with the current tooling?"

our best bet, I think, would be to mock out either the disconnection method or the requests.Session.close() method and verify that the mock is getting called.

related to this yesterday I was thinking that for folks who don't want to/can't use the "with" statement, should we also provide a .disconnect() method so that it's possible to call it manually? If we stub out requests.Session.close() we can test that disconnection happens as expected in the non-"with" test case, too.

(I'm just not exactly sure how to use the mock and verify that the mocked out function is getting called... but I guess I can search around in the library's documentation)

When we exit the "with" block used on the connect() function, we expect
HTTP sessions to be closed, so we should make sure that it is really
happening.
This new method can help ppl tearing things down without using the
"with" statement. Similarly to the motivation behind implmenting the
"with" statement, this can be useful for connections that were made
through a tunnel (for example ssh).

This method makes it possible for example to have a BaseAPI object
wrapped up in another class, and tear down connections only when that
wrapping class is all done with calls to the object.
This is similar to the test we are doing in the case of the "with"
statement, but we're making sure that calling .disconnect() directly
causes the same effect.
@lelutin
Copy link
Contributor Author

lelutin commented Oct 11, 2020

I've used "import mock" in the file test_connect.py since that's what was used in test_baseapi.py, but I was wondering something:

  • python-mock is, since python 3.3, just a shim for unittest.mock
    • is python < 3.3 supported by this library?
  • the rest of the test suite uses pytest, which provides pytest-mock. should we migrate to using objects from that library instead? (I'm not sure about the rationale for the change other than "everything else uses pytest", though)

@igalic igalic dismissed their stale review October 11, 2020 18:56

implemented.

@igalic
Copy link
Contributor

igalic commented Oct 11, 2020

I've used "import mock" in the file test_connect.py since that's what was used in test_baseapi.py, but I was wondering something:

* python-mock is, since python 3.3, just a shim for unittest.mock
  
  * is python < 3.3 supported by this library?

* the rest of the test suite uses pytest, which provides pytest-mock. should we migrate to using objects from that library instead? (I'm not sure about the rationale for the change other than "everything else uses pytest", though)

We should definitely be migrating to pytest, but that might be too much for this pr, or?

@lelutin
Copy link
Contributor Author

lelutin commented Oct 11, 2020

I've used "import mock" in the file test_connect.py since that's what was used in test_baseapi.py, but I was wondering something:

* python-mock is, since python 3.3, just a shim for unittest.mock
  
  * is python < 3.3 supported by this library?

* the rest of the test suite uses pytest, which provides pytest-mock. should we migrate to using objects from that library instead? (I'm not sure about the rationale for the change other than "everything else uses pytest", though)

We should definitely be migrating to pytest, but that might be too much for this pr, or?

I feel this way, too! It just felt important to mention why I chose the mocking library that I did and wanted to throw the migration ideas to assess a future direction.

But by all means, if we can avoid migrating to another mocking library in this PR, it would keep this patch set easier to review.

@igalic igalic merged commit e0dc369 into voxpupuli:master Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants