44"""Test cases for the channel module."""
55
66from dataclasses import dataclass
7+ from typing import NotRequired , TypedDict
78from unittest import mock
89
910import pytest
2930]
3031
3132
33+ class _CreateChannelKwargs (TypedDict ):
34+ default_port : NotRequired [int ]
35+
36+
3237@pytest .mark .parametrize ("uri, host, port, ssl" , VALID_URLS )
38+ @pytest .mark .parametrize (
39+ "default_port" , [None , 9090 , 1234 ], ids = lambda x : f"default_port={ x } "
40+ )
3341def test_grpclib_parse_uri_ok (
3442 uri : str ,
3543 host : str ,
3644 port : int ,
3745 ssl : bool ,
46+ default_port : int | None ,
3847) -> None :
3948 """Test successful parsing of gRPC URIs using grpclib."""
4049
@@ -44,24 +53,31 @@ class _FakeChannel:
4453 port : int
4554 ssl : bool
4655
56+ kwargs = _CreateChannelKwargs ()
57+ if default_port is not None :
58+ kwargs ["default_port" ] = default_port
59+
60+ expected_port = port if f":{ port } " in uri or default_port is None else default_port
4761 with mock .patch (
4862 "frequenz.client.base.channel._grpchacks.grpclib_create_channel" ,
4963 return_value = _FakeChannel (host , port , ssl ),
50- ):
51- channel = parse_grpc_uri (uri , _grpchacks .GrpclibChannel )
64+ ) as create_channel_mock :
65+ channel = parse_grpc_uri (uri , _grpchacks .GrpclibChannel , ** kwargs )
5266
5367 assert isinstance (channel , _FakeChannel )
54- assert channel .host == host
55- assert channel .port == port
56- assert channel .ssl == ssl
68+ create_channel_mock .assert_called_once_with (host , expected_port , ssl )
5769
5870
5971@pytest .mark .parametrize ("uri, host, port, ssl" , VALID_URLS )
72+ @pytest .mark .parametrize (
73+ "default_port" , [None , 9090 , 1234 ], ids = lambda x : f"default_port={ x } "
74+ )
6075def test_grpcio_parse_uri_ok (
6176 uri : str ,
6277 host : str ,
6378 port : int ,
6479 ssl : bool ,
80+ default_port : int | None ,
6581) -> None :
6682 """Test successful parsing of gRPC URIs using grpcio."""
6783 expected_channel = mock .MagicMock (
@@ -70,6 +86,11 @@ def test_grpcio_parse_uri_ok(
7086 expected_credentials = mock .MagicMock (
7187 name = "mock_credentials" , spec = _grpchacks .GrpcioChannel
7288 )
89+ expected_port = port if f":{ port } " in uri or default_port is None else default_port
90+
91+ kwargs = _CreateChannelKwargs ()
92+ if default_port is not None :
93+ kwargs ["default_port" ] = default_port
7394
7495 with (
7596 mock .patch (
@@ -85,10 +106,10 @@ def test_grpcio_parse_uri_ok(
85106 return_value = expected_credentials ,
86107 ) as ssl_channel_credentials_mock ,
87108 ):
88- channel = parse_grpc_uri (uri , _grpchacks .GrpcioChannel )
109+ channel = parse_grpc_uri (uri , _grpchacks .GrpcioChannel , ** kwargs )
89110
90111 assert channel == expected_channel
91- expected_target = f"{ host } :{ port } "
112+ expected_target = f"{ host } :{ expected_port } "
92113 if ssl :
93114 ssl_channel_credentials_mock .assert_called_once_with ()
94115 secure_channel_mock .assert_called_once_with (
0 commit comments