-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #389 from Limmen/grpc
unit test grpc_util
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
simulation-system/libs/csle-common/tests/test_grpc_util.py
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,40 @@ | ||
import grpc | ||
from unittest.mock import patch, MagicMock | ||
from csle_common.util.grpc_util import GrpcUtil | ||
|
||
|
||
class TestGrpcUtilSuite: | ||
""" | ||
Test suite for grpc_util | ||
""" | ||
|
||
@patch("grpc.channel_ready_future") | ||
def test_grpc_server_on(self, mock_channel_ready_future) -> None: | ||
""" | ||
Test utility function to test if a given gRPC channel is working or not | ||
:param mock_channel_ready_future: mock_channel_ready_future | ||
:return: None | ||
""" | ||
mock_future = MagicMock() | ||
mock_channel_ready_future.return_value = mock_future | ||
result = GrpcUtil.grpc_server_on(mock_channel_ready_future) | ||
mock_future.result.assert_called() | ||
assert result | ||
|
||
@patch("grpc.channel_ready_future") | ||
def test_grpc_server_on_timeout(self, mock_channel_ready_future) -> None: | ||
""" | ||
Test utility function to test if a given gRPC channel is not working | ||
:param mock_channel_ready_future: mock_channel_ready_future | ||
:return: None | ||
""" | ||
mock_future = MagicMock() | ||
mock_future.result.side_effect = grpc.FutureTimeoutError() | ||
mock_channel_ready_future.return_value = mock_future | ||
result = GrpcUtil.grpc_server_on(mock_channel_ready_future) | ||
mock_future.result.assert_called() | ||
assert not result |