2121import com .navercorp .pinpoint .pubsub .endpoint .PubSubServerFactory ;
2222import com .navercorp .pinpoint .pubsub .endpoint .PubSubServiceDescriptor ;
2323import com .navercorp .pinpoint .redis .stream .RedisStreamConfig ;
24+ import org .junit .jupiter .api .BeforeAll ;
2425import org .junit .jupiter .api .DisplayName ;
2526import org .junit .jupiter .api .Test ;
26- import org .springframework .context .ApplicationContext ;
27- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
27+ import org .junit .jupiter .api .extension .ExtendWith ;
28+ import org .springframework .beans .factory .annotation .Autowired ;
29+ import org .springframework .test .context .ContextConfiguration ;
30+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
2831import org .testcontainers .containers .GenericContainer ;
32+ import org .testcontainers .junit .jupiter .Container ;
33+ import org .testcontainers .junit .jupiter .Testcontainers ;
2934import org .testcontainers .utility .DockerImageName ;
3035import reactor .core .publisher .Flux ;
3136import reactor .core .publisher .Mono ;
3843/**
3944 * @author youngjin.kim2
4045 */
41- @ DisplayName ("req/res based on redis" )
42- public class RedisReqResTest {
46+ @ DisplayName ("req/res based on redis stream" )
47+ @ ExtendWith (SpringExtension .class )
48+ @ ContextConfiguration (classes = {RedisStreamConfig .class })
49+ @ Testcontainers
50+ public class RedisStreamReqResTest {
4351
44- @ DisplayName ("req/res based on redis pubsub" )
45- @ Test
46- public void testRedisPubSub () {
47- testConfigClass (RedisPubSubConfig .class );
52+ @ Container
53+ @ SuppressWarnings ("resource" )
54+ private static final GenericContainer <?> redisContainer = new GenericContainer <>(DockerImageName .parse ("redis:7.0" ))
55+ .withExposedPorts (6379 )
56+ .withReuse (true );
57+
58+ @ Autowired
59+ private PubSubServerFactory serverFactory ;
60+
61+ @ Autowired
62+ private PubSubClientFactory clientFactory ;
63+
64+ @ BeforeAll
65+ public static void beforeAll () {
66+ System .setProperty ("spring.data.redis.host" , redisContainer .getHost ());
67+ System .setProperty ("spring.redis.host" , redisContainer .getHost ());
68+ System .setProperty ("spring.data.redis.port" , redisContainer .getMappedPort (6379 ).toString ());
69+ System .setProperty ("spring.redis.port" , redisContainer .getMappedPort (6379 ).toString ());
4870 }
4971
5072 @ DisplayName ("req/res based on redis stream" )
5173 @ Test
5274 public void testRedisStreamPubSub () {
53- testConfigClass (RedisStreamConfig .class );
54- }
55-
56- private void testConfigClass (Class <?> configClass ) {
57- runWithRedisContainer (() -> {
58- final ApplicationContext context = new AnnotationConfigApplicationContext (configClass );
59- testServerClientFactory (
60- context .getBean (PubSubServerFactory .class ),
61- context .getBean (PubSubClientFactory .class )
62- );
63- });
64- }
65-
66- @ SuppressWarnings ("resource" )
67- private void runWithRedisContainer (Runnable r ) {
68- try (final GenericContainer <?> redisContainer = new GenericContainer <>(DockerImageName .parse ("redis:7.0" ))
69- .withExposedPorts (6379 )
70- .withReuse (true )
71- ) {
72- redisContainer .start ();
73- System .setProperty ("spring.data.redis.host" , redisContainer .getHost ());
74- System .setProperty ("spring.redis.host" , redisContainer .getHost ());
75- System .setProperty ("spring.data.redis.port" , redisContainer .getMappedPort (6379 ).toString ());
76- System .setProperty ("spring.redis.port" , redisContainer .getMappedPort (6379 ).toString ());
77-
78- r .run ();
79-
80- redisContainer .stop ();
81- }
75+ testPubSubServerClient (this .serverFactory , this .clientFactory );
8276 }
8377
84- private void testServerClientFactory (
85- PubSubServerFactory serverFactory ,
86- PubSubClientFactory clientFactory
87- ) {
78+ static void testPubSubServerClient (PubSubServerFactory serverFactory , PubSubClientFactory clientFactory ) {
8879 final PubSubMonoServiceDescriptor <String , String > greeterService =
8980 PubSubServiceDescriptor .mono ("greeter" , String .class , String .class );
9081 serverFactory .build (name -> Mono .just ("Hello, " + name ), greeterService ).afterPropertiesSet ();
@@ -99,9 +90,10 @@ private void testServerClientFactory(
9990 PubSubServiceDescriptor .flux ("range" , Integer .class , Integer .class );
10091 serverFactory .build (el -> Flux .range (0 , el ), rangeService ).afterPropertiesSet ();
10192 assertThat (syncRequestFlux (clientFactory , rangeService , 5 )).isEqualTo (List .of (0 , 1 , 2 , 3 , 4 ));
93+ assertThat (syncRequestFlux (clientFactory , rangeService , 3 )).isEqualTo (List .of (0 , 1 , 2 ));
10294 }
10395
104- private <D , S > S syncRequestMono (
96+ static <D , S > S syncRequestMono (
10597 PubSubClientFactory clientFactory ,
10698 PubSubMonoServiceDescriptor <D , S > descriptor ,
10799 D demand
@@ -111,7 +103,7 @@ private <D, S> S syncRequestMono(
111103 .block ();
112104 }
113105
114- private <D , S > List <S > syncRequestFlux (
106+ static <D , S > List <S > syncRequestFlux (
115107 PubSubClientFactory clientFactory ,
116108 PubSubFluxServiceDescriptor <D , S > descriptor ,
117109 D demand
0 commit comments