This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Remove redundant WrappedConnection #4409
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Remove redundant federation connection wrapping code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,11 @@ | |
|
||
from mock import Mock | ||
|
||
from zope.interface import implementer | ||
|
||
from twisted.internet.defer import TimeoutError | ||
from twisted.internet.error import ConnectingCancelledError, DNSLookupError | ||
from twisted.internet.interfaces import ITransport | ||
from twisted.web.client import ResponseNeverReceived | ||
from twisted.web.http import HTTPChannel | ||
|
||
|
@@ -44,7 +47,7 @@ def prepare(self, reactor, clock, homeserver): | |
|
||
def test_dns_error(self): | ||
""" | ||
If the DNS raising returns an error, it will bubble up. | ||
If the DNS lookup returns an error, it will bubble up. | ||
""" | ||
d = self.cl.get_json("testserv2:8008", "foo/bar", timeout=10000) | ||
self.pump() | ||
|
@@ -63,7 +66,7 @@ def test_client_never_connect(self): | |
self.pump() | ||
|
||
# Nothing happened yet | ||
self.assertFalse(d.called) | ||
self.assertNoResult(d) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this gives a slightly more helpful error when it fails. |
||
|
||
# Make sure treq is trying to connect | ||
clients = self.reactor.tcpClients | ||
|
@@ -72,7 +75,7 @@ def test_client_never_connect(self): | |
self.assertEqual(clients[0][1], 8008) | ||
|
||
# Deferred is still without a result | ||
self.assertFalse(d.called) | ||
self.assertNoResult(d) | ||
|
||
# Push by enough to time it out | ||
self.reactor.advance(10.5) | ||
|
@@ -94,7 +97,7 @@ def test_client_connect_no_response(self): | |
self.pump() | ||
|
||
# Nothing happened yet | ||
self.assertFalse(d.called) | ||
self.assertNoResult(d) | ||
|
||
# Make sure treq is trying to connect | ||
clients = self.reactor.tcpClients | ||
|
@@ -107,7 +110,7 @@ def test_client_connect_no_response(self): | |
client.makeConnection(conn) | ||
|
||
# Deferred is still without a result | ||
self.assertFalse(d.called) | ||
self.assertNoResult(d) | ||
|
||
# Push by enough to time it out | ||
self.reactor.advance(10.5) | ||
|
@@ -135,7 +138,7 @@ def test_client_gets_headers(self): | |
client.makeConnection(conn) | ||
|
||
# Deferred does not have a result | ||
self.assertFalse(d.called) | ||
self.assertNoResult(d) | ||
|
||
# Send it the HTTP response | ||
client.dataReceived(b"HTTP/1.1 200 OK\r\nServer: Fake\r\n\r\n") | ||
|
@@ -159,7 +162,7 @@ def test_client_headers_no_body(self): | |
client.makeConnection(conn) | ||
|
||
# Deferred does not have a result | ||
self.assertFalse(d.called) | ||
self.assertNoResult(d) | ||
|
||
# Send it the HTTP response | ||
client.dataReceived( | ||
|
@@ -195,3 +198,62 @@ def test_client_sends_body(self): | |
request = server.requests[0] | ||
content = request.content.read() | ||
self.assertEqual(content, b'{"a":"b"}') | ||
|
||
def test_closes_connection(self): | ||
d = self.cl.get_json("testserv:8008", "foo/bar") | ||
|
||
self.pump() | ||
|
||
# there should have been a call to connectTCP | ||
clients = self.reactor.tcpClients | ||
self.assertEqual(len(clients), 1) | ||
(_host, _port, factory, _timeout, _bindAddress) = clients[0] | ||
|
||
# complete the connection and wire it up to a fake transport | ||
client = factory.buildProtocol(None) | ||
conn = MockTransport() | ||
client.makeConnection(conn) | ||
|
||
# that should have made it send the request to the connection | ||
self.assertRegex(conn.written, b"^GET /foo/bar") | ||
|
||
# Send the HTTP response | ||
client.dataReceived( | ||
b"HTTP/1.1 200 OK\r\n" | ||
b"Content-Type: application/json\r\n" | ||
b"Content-Length: 2\r\n" | ||
b"\r\n" | ||
b"{}" | ||
) | ||
|
||
# We should get a successful response | ||
r = self.successResultOf(d) | ||
self.assertEqual(r, {}) | ||
|
||
self.assertFalse(conn.lostConnection) | ||
|
||
# wait for a while | ||
self.pump(120) | ||
|
||
self.assertTrue(conn.lostConnection) | ||
|
||
|
||
@implementer(ITransport) | ||
class MockTransport(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe https://twistedmatrix.com/documents/current/api/twisted.test.proto_helpers.StringTransport.html would reduce some duplication here? ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes :) |
||
def __init__(self): | ||
self.paused = False | ||
self.written = b'' | ||
self.lostConnection = False | ||
|
||
def pauseProducing(self): | ||
self.paused = True | ||
|
||
def resumeProducing(self): | ||
self.paused = False | ||
|
||
def writeSequence(self, data): | ||
for d in data: | ||
self.written += d | ||
|
||
def loseConnection(self): | ||
self.lostConnection = True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for some reason, the change makes this now throw some exceptions synchronously, hence moving it inside the try/catch. It should have been there anyway imho.