1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import logging
1516from unittest import mock
1617
1718from pathwaysutils import profiling
@@ -32,13 +33,14 @@ def setUp(self):
3233
3334 @parameterized .parameters (8000 , 1234 )
3435 def test_collect_profile_port (self , port ):
35- profiling .collect_profile (
36+ result = profiling .collect_profile (
3637 port = port ,
3738 duration_ms = 1000 ,
3839 host = "127.0.0.1" ,
3940 log_dir = "gs://test_bucket/test_dir" ,
4041 )
4142
43+ self .assertTrue (result )
4244 self .mock_post .assert_called_once_with (
4345 f"http://127.0.0.1:{ port } /profiling" ,
4446 json = {
@@ -49,13 +51,14 @@ def test_collect_profile_port(self, port):
4951
5052 @parameterized .parameters (1000 , 1234 )
5153 def test_collect_profile_duration_ms (self , duration_ms ):
52- profiling .collect_profile (
54+ result = profiling .collect_profile (
5355 port = 8000 ,
5456 duration_ms = duration_ms ,
5557 host = "127.0.0.1" ,
5658 log_dir = "gs://test_bucket/test_dir" ,
5759 )
5860
61+ self .assertTrue (result )
5962 self .mock_post .assert_called_once_with (
6063 "http://127.0.0.1:8000/profiling" ,
6164 json = {
@@ -66,13 +69,14 @@ def test_collect_profile_duration_ms(self, duration_ms):
6669
6770 @parameterized .parameters ("127.0.0.1" , "localhost" , "192.168.1.1" )
6871 def test_collect_profile_host (self , host ):
69- profiling .collect_profile (
72+ result = profiling .collect_profile (
7073 port = 8000 ,
7174 duration_ms = 1000 ,
7275 host = host ,
7376 log_dir = "gs://test_bucket/test_dir" ,
7477 )
7578
79+ self .assertTrue (result )
7680 self .mock_post .assert_called_once_with (
7781 f"http://{ host } :8000/profiling" ,
7882 json = {
@@ -87,10 +91,11 @@ def test_collect_profile_host(self, host):
8791 "gs://test_bucket3/test/log/dir" ,
8892 )
8993 def test_collect_profile_log_dir (self , log_dir ):
90- profiling .collect_profile (
94+ result = profiling .collect_profile (
9195 port = 8000 , duration_ms = 1000 , host = "127.0.0.1" , log_dir = log_dir
9296 )
9397
98+ self .assertTrue (result )
9499 self .mock_post .assert_called_once_with (
95100 "http://127.0.0.1:8000/profiling" ,
96101 json = {
@@ -107,22 +112,27 @@ def test_collect_profile_log_dir_error(self, log_dir):
107112 )
108113
109114 @parameterized .parameters (
110- requests .exceptions .ConnectionError ,
111- requests .exceptions .Timeout ,
112- requests .exceptions .TooManyRedirects ,
113- requests .exceptions .RequestException ,
114- requests .exceptions .HTTPError ,
115+ requests .exceptions .ConnectionError ( "Connection error" ) ,
116+ requests .exceptions .Timeout ( "Timeout" ) ,
117+ requests .exceptions .TooManyRedirects ( "Too many redirects" ) ,
118+ requests .exceptions .RequestException ( "Request exception" ) ,
119+ requests .exceptions .HTTPError ( "HTTP error" ) ,
115120 )
116- def test_collect_profile_request_error (self , exception_type ):
117- self .mock_post .side_effect = exception_type
121+ def test_collect_profile_request_error (self , exception ):
122+ self .mock_post .side_effect = exception
123+
124+ with self .assertLogs (profiling ._logger , level = logging .ERROR ) as logs :
125+ result = profiling .collect_profile (
126+ port = 8000 ,
127+ duration_ms = 1000 ,
128+ host = "127.0.0.1" ,
129+ log_dir = "gs://test_bucket/test_dir" ,
130+ )
118131
119- result = profiling .collect_profile (
120- port = 8000 ,
121- duration_ms = 1000 ,
122- host = "127.0.0.1" ,
123- log_dir = "gs://test_bucket/test_dir" ,
132+ self .assertLen (logs .output , 1 )
133+ self .assertIn (
134+ f"Failed to collect profiling data: { exception } " , logs .output [0 ]
124135 )
125-
126136 self .assertFalse (result )
127137 self .mock_post .assert_called_once ()
128138
0 commit comments