31
31
import org .junit .jupiter .api .condition .OS ;
32
32
import org .junit .jupiter .params .ParameterizedTest ;
33
33
import org .junit .jupiter .params .provider .ValueSource ;
34
+ import org .junitpioneer .jupiter .SetEnvironmentVariable ;
34
35
import org .mockito .MockedConstruction ;
35
36
import org .mockito .MockedStatic ;
36
37
import org .mockito .invocation .InvocationOnMock ;
37
- import uk .org .webcompere .systemstubs .environment .EnvironmentVariables ;
38
38
39
39
class GrpcConnectorTest {
40
40
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
+
41
45
@ ParameterizedTest
42
46
@ ValueSource (ints = {1 , 2 , 3 })
43
47
void validate_retry_calls (int retries ) throws Exception {
@@ -330,39 +334,39 @@ void host_and_port_arg_should_build_tcp_socket() {
330
334
}
331
335
332
336
@ Test
337
+ @ SetEnvironmentVariable (key = "FLAGD_HOST" , value = HOST )
338
+ @ SetEnvironmentVariable (key = "FLAGD_PORT" , value = "" + PORT )
333
339
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 );
337
341
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 ();
342
345
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 );
348
351
349
- try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder =
350
- mockStatic (NettyChannelBuilder .class )) {
352
+ try (MockedStatic <NettyChannelBuilder > mockStaticChannelBuilder = mockStatic (NettyChannelBuilder .class )) {
351
353
352
- mockStaticChannelBuilder
353
- .when (() -> NettyChannelBuilder .forTarget (anyString ()))
354
- .thenReturn (mockChannelBuilder );
354
+ mockStaticChannelBuilder
355
+ .when (() -> NettyChannelBuilder .forTarget (anyString ()))
356
+ .thenReturn (mockChannelBuilder );
355
357
356
- new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
358
+ new GrpcConnector (FlagdOptions .builder ().build (), null , null , null );
357
359
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 ));
361
362
}
362
- });
363
+ }
363
364
}
364
365
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
+ */
366
370
@ Test
367
371
@ EnabledOnOs (OS .LINUX )
368
372
void path_arg_should_build_domain_socket_with_correct_path () {
@@ -390,7 +394,7 @@ void path_arg_should_build_domain_socket_with_correct_path() {
390
394
// verify path matches
391
395
mockStaticChannelBuilder .verify (
392
396
() -> NettyChannelBuilder .forAddress (argThat ((DomainSocketAddress d ) -> {
393
- assertEquals (d .path (), path ); // path should match
397
+ assertEquals (path , d .path ()); // path should match
394
398
return true ;
395
399
})),
396
400
times (1 ));
@@ -399,44 +403,45 @@ void path_arg_should_build_domain_socket_with_correct_path() {
399
403
}
400
404
}
401
405
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
+ */
403
410
@ Test
404
411
@ EnabledOnOs (OS .LINUX )
412
+ @ SetEnvironmentVariable (key = "FLAGD_SOCKET_PATH" , value = SOCKET_PATH )
405
413
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 ));
437
442
}
438
443
}
439
- });
444
+ }
440
445
}
441
446
442
447
@ Test
0 commit comments