Skip to content

Commit 16245a8

Browse files
authored
Merge pull request #389 from Limmen/grpc
unit test grpc_util
2 parents 698357e + 9b77ab3 commit 16245a8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import grpc
2+
from unittest.mock import patch, MagicMock
3+
from csle_common.util.grpc_util import GrpcUtil
4+
5+
6+
class TestGrpcUtilSuite:
7+
"""
8+
Test suite for grpc_util
9+
"""
10+
11+
@patch("grpc.channel_ready_future")
12+
def test_grpc_server_on(self, mock_channel_ready_future) -> None:
13+
"""
14+
Test utility function to test if a given gRPC channel is working or not
15+
16+
:param mock_channel_ready_future: mock_channel_ready_future
17+
18+
:return: None
19+
"""
20+
mock_future = MagicMock()
21+
mock_channel_ready_future.return_value = mock_future
22+
result = GrpcUtil.grpc_server_on(mock_channel_ready_future)
23+
mock_future.result.assert_called()
24+
assert result
25+
26+
@patch("grpc.channel_ready_future")
27+
def test_grpc_server_on_timeout(self, mock_channel_ready_future) -> None:
28+
"""
29+
Test utility function to test if a given gRPC channel is not working
30+
31+
:param mock_channel_ready_future: mock_channel_ready_future
32+
33+
:return: None
34+
"""
35+
mock_future = MagicMock()
36+
mock_future.result.side_effect = grpc.FutureTimeoutError()
37+
mock_channel_ready_future.return_value = mock_future
38+
result = GrpcUtil.grpc_server_on(mock_channel_ready_future)
39+
mock_future.result.assert_called()
40+
assert not result

0 commit comments

Comments
 (0)