3737import io .netty .channel .EventLoopGroup ;
3838import io .netty .channel .epoll .EpollEventLoopGroup ;
3939import io .netty .channel .unix .DomainSocketAddress ;
40- import uk . org .webcompere . systemstubs . environment . EnvironmentVariables ;
40+ import org .junitpioneer . jupiter . SetEnvironmentVariable ;
4141
4242class GrpcConnectorTest {
4343
44+ public static final String HOST = "server.com" ;
45+ public static final int PORT = 4321 ;
46+ public static final String SOCKET_PATH = "/some/other/path" ;
47+
4448 @ ParameterizedTest
45- @ ValueSource (ints = { 1 , 2 , 3 })
49+ @ ValueSource (ints = {1 , 2 , 3 })
4650 void validate_retry_calls (int retries ) throws Exception {
4751 final int backoffMs = 100 ;
4852
@@ -136,14 +140,14 @@ void stream_does_not_fail_on_first_error() {
136140 // pass true in connected lambda
137141 final GrpcConnector connector = new GrpcConnector (FlagdOptions .builder ().build (), cache ,
138142 () -> {
139- try {
140- Thread .sleep (100 );
141- return true ;
142- } catch (Exception e ) {
143- }
144- return false ;
145-
146- },
143+ try {
144+ Thread .sleep (100 );
145+ return true ;
146+ } catch (Exception e ) {
147+ }
148+ return false ;
149+
150+ },
147151 onConnectionEvent );
148152
149153 assertDoesNotThrow (connector ::initialize );
@@ -325,35 +329,33 @@ void host_and_port_arg_should_build_tcp_socket() {
325329 }
326330
327331 @ Test
332+ @ SetEnvironmentVariable (key = "FLAGD_HOST" , value = HOST )
333+ @ SetEnvironmentVariable (key = "FLAGD_PORT" , value = "" + PORT )
328334 void no_args_host_and_port_env_set_should_build_tcp_socket () throws Exception {
329- final String host = "server.com" ;
330- final int port = 4321 ;
331- final String targetUri = String .format ("%s:%s" , host , port );
335+ final String targetUri = String .format ("%s:%s" , HOST , PORT );
332336
333- new EnvironmentVariables ("FLAGD_HOST" , host , "FLAGD_PORT" , String .valueOf (port )).execute (() -> {
334- ServiceGrpc .ServiceBlockingStub mockBlockingStub = mock (ServiceGrpc .ServiceBlockingStub .class );
335- ServiceGrpc .ServiceStub mockStub = createServiceStubMock ();
336- NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
337+ ServiceGrpc .ServiceBlockingStub mockBlockingStub = mock (ServiceGrpc .ServiceBlockingStub .class );
338+ ServiceGrpc .ServiceStub mockStub = createServiceStubMock ();
339+ NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
337340
338- try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
339- mockStaticService .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
340- .thenReturn (mockBlockingStub );
341- mockStaticService .when (() -> ServiceGrpc .newStub (any ()))
342- .thenReturn (mockStub );
341+ try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
342+ mockStaticService .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
343+ .thenReturn (mockBlockingStub );
344+ mockStaticService .when (() -> ServiceGrpc .newStub (any ()))
345+ .thenReturn (mockStub );
343346
344- try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (
345- NettyChannelBuilder .class )) {
347+ try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (
348+ NettyChannelBuilder .class )) {
346349
347- mockStaticChannelBuilder .when (() -> NettyChannelBuilder
348- .forTarget (anyString ())).thenReturn (mockChannelBuilder );
350+ mockStaticChannelBuilder .when (() -> NettyChannelBuilder
351+ .forTarget (anyString ())).thenReturn (mockChannelBuilder );
349352
350- new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
353+ new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
351354
352- // verify host/port matches & called times(= 1 as we rely on reusable channel)
353- mockStaticChannelBuilder .verify (() -> NettyChannelBuilder .forTarget (targetUri ), times (1 ));
354- }
355+ // verify host/port matches & called times(= 1 as we rely on reusable channel)
356+ mockStaticChannelBuilder .verify (() -> NettyChannelBuilder .forTarget (targetUri ), times (1 ));
355357 }
356- });
358+ }
357359 }
358360
359361 /**
@@ -388,7 +390,7 @@ void path_arg_should_build_domain_socket_with_correct_path() {
388390 // verify path matches
389391 mockStaticChannelBuilder .verify (() -> NettyChannelBuilder
390392 .forAddress (argThat ((DomainSocketAddress d ) -> {
391- assertEquals (d .path (), path ); // path should match
393+ assertEquals (path , d .path ()); // path should match
392394 return true ;
393395 })), times (1 ));
394396 }
@@ -402,42 +404,40 @@ void path_arg_should_build_domain_socket_with_correct_path() {
402404 */
403405 @ Test
404406 @ EnabledOnOs (OS .LINUX )
407+ @ SetEnvironmentVariable (key = "FLAGD_SOCKET_PATH" , value = SOCKET_PATH )
405408 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-
410- ServiceBlockingStub mockBlockingStub = mock (ServiceBlockingStub .class );
411- ServiceStub mockStub = mock (ServiceStub .class );
412- NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
413-
414- try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
415- mockStaticService .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
416- .thenReturn (mockBlockingStub );
417- mockStaticService .when (() -> ServiceGrpc .newStub (any ()))
418- .thenReturn (mockStub );
419-
420- try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (
421- NettyChannelBuilder .class )) {
422-
423- try (MockedConstruction <EpollEventLoopGroup > mockEpollEventLoopGroup = mockConstruction (
424- EpollEventLoopGroup .class ,
425- (mock , context ) -> {
426- })) {
427- mockStaticChannelBuilder .when (() -> NettyChannelBuilder
428- .forAddress (any (DomainSocketAddress .class ))).thenReturn (mockChannelBuilder );
429-
430- new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
431-
432- // verify path matches & called times(= 1 as we rely on reusable channel)
433- mockStaticChannelBuilder .verify (() -> NettyChannelBuilder
434- .forAddress (argThat ((DomainSocketAddress d ) -> {
435- return d .path () == path ;
436- })), times (1 ));
437- }
409+
410+ ServiceBlockingStub mockBlockingStub = mock (ServiceBlockingStub .class );
411+ ServiceStub mockStub = mock (ServiceStub .class );
412+ NettyChannelBuilder mockChannelBuilder = getMockChannelBuilderSocket ();
413+
414+ try (MockedStatic <ServiceGrpc > mockStaticService = mockStatic (ServiceGrpc .class )) {
415+ mockStaticService .when (() -> ServiceGrpc .newBlockingStub (any (Channel .class )))
416+ .thenReturn (mockBlockingStub );
417+ mockStaticService .when (() -> ServiceGrpc .newStub (any ()))
418+ .thenReturn (mockStub );
419+
420+ try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (
421+ NettyChannelBuilder .class )) {
422+
423+ try (MockedConstruction <EpollEventLoopGroup > mockEpollEventLoopGroup = mockConstruction (
424+ EpollEventLoopGroup .class ,
425+ (mock , context ) -> {
426+ })) {
427+ mockStaticChannelBuilder .when (() -> NettyChannelBuilder
428+ .forAddress (any (DomainSocketAddress .class ))).thenReturn (mockChannelBuilder );
429+
430+ new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
431+
432+ // verify path matches & called times(= 1 as we rely on reusable channel)
433+ mockStaticChannelBuilder .verify (() -> NettyChannelBuilder
434+ .forAddress (argThat ((DomainSocketAddress d ) -> {
435+ assertEquals (SOCKET_PATH , d .path ()); // path should match
436+ return true ;
437+ })), times (1 ));
438438 }
439439 }
440- });
440+ }
441441 }
442442
443443 @ Test
0 commit comments