class SiteStreamWaitLoopTests(unittest.TestCase): def setUp(self): self.stream = SiteStream(follow, consumer, token) self.stream.connection = MockConnection() def test_wait_for_response(self): """SiteStream.__wait_for_response should block until it returns a response object. """ resp = self.stream._SiteStream__wait_for_response() self.assertNotEqual(resp, None) self.assertEqual(self.stream.connection.getresponse_count, 5) def test_bad_response(self): """SiteStream.__wait_for_response should immediately exit immediately if it gets a response object with a status code other than 200. """ self.stream.connection.status = 401 resp = self.stream._SiteStream__wait_for_response() self.assertEqual(resp, None) def test_reset_throttles_on_response(self): """SiteStream.__wait_for_response should reset throttles when a valid response object is finally returned. """ self.stream.error_count = 10 resp = self.stream._SiteStream__wait_for_response() self.assertEqual(self.stream.error_count, 0) def test_disconnect_issued(self): """SiteStream.__wait_for_response should immediately exit if the disconnect flag has been set. """ self.stream.disconnect() resp = self.stream._SiteStream__wait_for_response() self.assertEqual(resp, None) self.assertEqual(self.stream.connection.getresponse_count, 0)
def setUp(self): self.stream = SiteStream(follow, consumer, token) self.stream.retry_time = 0 self.stream.retry_limit = 2
class SiteStreamConnectTests(unittest.TestCase): def setUp(self): self.stream = SiteStream(follow, consumer, token) self.stream.retry_time = 0 self.stream.retry_limit = 2 def test_real_http_connection(self): """Establish a connection the streaming endpoint via HTTP.""" from sitebucket import listener protocol = listener.PROTOCOL listener.PROTOCOL = "http://" httplib.HTTPSConnection = REAL_HTTPSConnection httplib.HTTPConnection = REAL_HTTPConnection resp = self.stream.connect() self.assertEqual(resp.status, 200) listener.PROTOCOL = protocol def test_real_https_connection(self): """Establish a connection the streaming endpoint via HTTP.""" from sitebucket import listener protocol = listener.PROTOCOL listener.PROTOCOL = "https://" httplib.HTTPSConnection = REAL_HTTPSConnection httplib.HTTPConnection = REAL_HTTPConnection resp = self.stream.connect() self.assertEqual(resp.status, 200) listener.PROTOCOL = protocol def test_timeout(self): """If httplib raises a timeout exception, it should retry retry_limit times and then fail and return none.""" from socket import timeout httplib.HTTPSConnection = Mock_HTTPConnection(conn_exception=timeout) httplib.HTTPConnection = Mock_HTTPConnection(conn_exception=timeout) self.stream.retry_limit = 2 resp = self.stream.connect() self.assertEqual(resp, None) self.assertEqual(self.stream.error_count, 2) def test_ssl_error(self): """If httplib raises an SSL exception, it should retry retry_limit times and then fail and return none.""" from ssl import SSLError httplib.HTTPSConnection = Mock_HTTPConnection(conn_exception=SSLError) httplib.HTTPConnection = Mock_HTTPConnection(conn_exception=SSLError) self.stream.retry_limit = 2 resp = self.stream.connect() self.assertEqual(resp, None) self.assertEqual(self.stream.error_count, 2) def test_non_200_resp(self): """If there is a non-200 response status, it should retry retry_limit times and then fail and return None.""" httplib.HTTPSConnection = Mock_HTTPConnection(status=401) httplib.HTTPConnection = Mock_HTTPConnection(status=401) self.stream.retry_limit = 2 resp = self.stream.connect() self.assertEqual(resp, None) self.assertEqual(self.stream.error_count, 2) def test_disconnect_issued(self): """If a disconnect event has been issued, the connect loop shouldn't even run. It should just return None. Note stream.connection will never be set in this case.""" httplib.HTTPSConnection = Mock_HTTPConnection() httplib.HTTPConnection = Mock_HTTPConnection() self.stream.disconnect() resp = self.stream.connect() self.assertEqual(resp, None) self.assertEqual(self.stream.connection, None) def retry_not_okay(self): """If retry_ok is false, then the connection loop should immediately exit and return None.""" self.stream.error_count = self.stream.retry_limit resp = self.stream.connect() self.assertEqual(resp, None) self.assertEqual(self.stream.connection, None) def test_unexpected_error(self): """If the connection loop encounters an unexpected error, it should immediately terminate.""" httplib.HTTPSConnection = Mock_HTTPConnection(conn_exception=AttributeError) httplib.HTTPConnection = httplib.HTTPSConnection resp = self.stream.connect() self.assertEqual(resp, None)
def setUp(self): self.stream = SiteStream(follow, consumer, token) self.stream.connection = MockConnection()