3131import org .junit .jupiter .api .condition .OS ;
3232import org .junit .jupiter .params .ParameterizedTest ;
3333import org .junit .jupiter .params .provider .ValueSource ;
34+ import org .junitpioneer .jupiter .SetEnvironmentVariable ;
3435import org .mockito .MockedConstruction ;
3536import org .mockito .MockedStatic ;
3637import org .mockito .invocation .InvocationOnMock ;
37- import uk .org .webcompere .systemstubs .environment .EnvironmentVariables ;
3838
3939class GrpcConnectorTest {
4040
41+ public static final String HOST = "server.com" ;
42+ public static final int PORT = 4321 ;
43+ public static final String SOCKET_PATH = "/some/other/path" ;
44+
4145 @ ParameterizedTest
4246 @ ValueSource (ints = {1 , 2 , 3 })
4347 void validate_retry_calls (int retries ) throws Exception {
@@ -330,39 +334,39 @@ void host_and_port_arg_should_build_tcp_socket() {
330334 }
331335
332336 @ Test
337+ @ SetEnvironmentVariable (key = "FLAGD_HOST" , value = HOST )
338+ @ SetEnvironmentVariable (key = "FLAGD_PORT" , value = "" + PORT )
333339 void no_args_host_and_port_env_set_should_build_tcp_socket () throws Exception {
334- final String host = "server.com" ;
335- final int port = 4321 ;
336- final String targetUri = String .format ("%s:%s" , host , port );
340+ final String targetUri = String .format ("%s:%s" , HOST , PORT );
337341
338- new EnvironmentVariables ("FLAGD_HOST" , host , "FLAGD_PORT" , String .valueOf (port )).execute (() -> {
339- ServiceGrpc .ServiceBlockingStub mockBlockingStub = mock (ServiceGrpc .ServiceBlockingStub .class );
340- ServiceGrpc .ServiceStub mockStub = createServiceStubMock ();
341- NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
342+ ServiceGrpc .ServiceBlockingStub mockBlockingStub = mock (ServiceGrpc .ServiceBlockingStub .class );
343+ ServiceGrpc .ServiceStub mockStub = createServiceStubMock ();
344+ NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
342345
343- try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
344- mockStaticService
345- .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
346- .thenReturn (mockBlockingStub );
347- mockStaticService .when (() -> ServiceGrpc .newStub (any ())).thenReturn (mockStub );
346+ try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
347+ mockStaticService
348+ .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
349+ .thenReturn (mockBlockingStub );
350+ mockStaticService .when (() -> ServiceGrpc .newStub (any ())).thenReturn (mockStub );
348351
349- try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder =
350- mockStatic (NettyChannelBuilder .class )) {
352+ try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (NettyChannelBuilder .class )) {
351353
352- mockStaticChannelBuilder
353- .when (() -> NettyChannelBuilder .forTarget (anyString ()))
354- .thenReturn (mockChannelBuilder );
354+ mockStaticChannelBuilder
355+ .when (() -> NettyChannelBuilder .forTarget (anyString ()))
356+ .thenReturn (mockChannelBuilder );
355357
356- new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
358+ new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
357359
358- // verify host/port matches & called times(= 1 as we rely on reusable channel)
359- mockStaticChannelBuilder .verify (() -> NettyChannelBuilder .forTarget (targetUri ), times (1 ));
360- }
360+ // verify host/port matches & called times(= 1 as we rely on reusable channel)
361+ mockStaticChannelBuilder .verify (() -> NettyChannelBuilder .forTarget (targetUri ), times (1 ));
361362 }
362- });
363+ }
363364 }
364365
365- /** OS Specific test - This test is valid only on Linux system as it rely on epoll availability */
366+ /**
367+ * OS Specific test - This test is valid only on Linux system as it rely on
368+ * epoll availability
369+ */
366370 @ Test
367371 @ EnabledOnOs (OS .LINUX )
368372 void path_arg_should_build_domain_socket_with_correct_path () {
@@ -390,7 +394,7 @@ void path_arg_should_build_domain_socket_with_correct_path() {
390394 // verify path matches
391395 mockStaticChannelBuilder .verify (
392396 () -> NettyChannelBuilder .forAddress (argThat ((DomainSocketAddress d ) -> {
393- assertEquals (d .path (), path ); // path should match
397+ assertEquals (path , d .path ()); // path should match
394398 return true ;
395399 })),
396400 times (1 ));
@@ -399,44 +403,45 @@ void path_arg_should_build_domain_socket_with_correct_path() {
399403 }
400404 }
401405
402- /** OS Specific test - This test is valid only on Linux system as it rely on epoll availability */
406+ /**
407+ * OS Specific test - This test is valid only on Linux system as it rely on
408+ * epoll availability
409+ */
403410 @ Test
404411 @ EnabledOnOs (OS .LINUX )
412+ @ SetEnvironmentVariable (key = "FLAGD_SOCKET_PATH" , value = SOCKET_PATH )
405413 void no_args_socket_env_should_build_domain_socket_with_correct_path () throws Exception {
406- final String path = "/some/other/path" ;
407-
408- new EnvironmentVariables ("FLAGD_SOCKET_PATH" , path ).execute (() -> {
409- ServiceBlockingStub mockBlockingStub = mock (ServiceBlockingStub .class );
410- ServiceStub mockStub = mock (ServiceStub .class );
411- NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
412-
413- try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
414- mockStaticService
415- .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
416- .thenReturn (mockBlockingStub );
417- mockStaticService .when (() -> ServiceGrpc .newStub (any ())).thenReturn (mockStub );
418-
419- try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder =
420- mockStatic (NettyChannelBuilder .class )) {
421-
422- try (MockedConstruction <EpollEventLoopGroup > mockEpollEventLoopGroup =
423- mockConstruction (EpollEventLoopGroup .class , (mock , context ) -> {})) {
424- mockStaticChannelBuilder
425- .when (() -> NettyChannelBuilder .forAddress (any (DomainSocketAddress .class )))
426- .thenReturn (mockChannelBuilder );
427-
428- new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
429-
430- // verify path matches & called times(= 1 as we rely on reusable channel)
431- mockStaticChannelBuilder .verify (
432- () -> NettyChannelBuilder .forAddress (argThat ((DomainSocketAddress d ) -> {
433- return d .path () == path ;
434- })),
435- times (1 ));
436- }
414+
415+ ServiceBlockingStub mockBlockingStub = mock (ServiceBlockingStub .class );
416+ ServiceStub mockStub = mock (ServiceStub .class );
417+ NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
418+
419+ try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
420+ mockStaticService
421+ .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
422+ .thenReturn (mockBlockingStub );
423+ mockStaticService .when (() -> ServiceGrpc .newStub (any ())).thenReturn (mockStub );
424+
425+ try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (NettyChannelBuilder .class )) {
426+
427+ try (MockedConstruction <EpollEventLoopGroup > mockEpollEventLoopGroup =
428+ mockConstruction (EpollEventLoopGroup .class , (mock , context ) -> {})) {
429+ mockStaticChannelBuilder
430+ .when (() -> NettyChannelBuilder .forAddress (any (DomainSocketAddress .class )))
431+ .thenReturn (mockChannelBuilder );
432+
433+ new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
434+
435+ // verify path matches & called times(= 1 as we rely on reusable channel)
436+ mockStaticChannelBuilder .verify (
437+ () -> NettyChannelBuilder .forAddress (argThat ((DomainSocketAddress d ) -> {
438+ assertEquals (SOCKET_PATH , d .path ()); // path should match
439+ return true ;
440+ })),
441+ times (1 ));
437442 }
438443 }
439- });
444+ }
440445 }
441446
442447 @ Test
0 commit comments