@ -2,57 +2,50 @@ package com.alibaba.cloud.stream.binder.rocketmq;
import java.util.HashMap ;
import java.util.Map ;
import java.util.concurrent.BlockingQueue ;
import com.alibaba.cloud.rocketmq.SimpleMsg ;
import com.alibaba.cloud.stream.binder.rocketmq.autoconfigurate.RocketMQBinderAutoConfiguration ;
import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQBinderConfigurationProperties ;
import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQConsumerProperties ;
import com.alibaba.cloud.stream.binder.rocketmq.support.MessageCollector ;
import com.alibaba.cloud.testsupport.SpringCloudAlibaba ;
import com.alibaba.cloud.testsupport.TestExtend ;
import org.apache.rocketmq.common.message.MessageConst ;
import org.junit.jupiter.api.Assertions ;
import org.junit.jupiter.api.BeforeAll ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.junit.platform.commons.util.StringUtils ;
import org.junit.runner.RunWith ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.b oot.ApplicationRunn er;
import org.springframework.b eans.factory.annotation.Qualifi er;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration ;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration ;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration ;
import org.springframework.boot.test.context.SpringBootTest ;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration ;
import org.springframework.cloud.stream.annotation.EnableBinding ;
import org.springframework.cloud.stream.annotation.Input ;
import org.springframework.cloud.stream.annotation.Output ;
import org.springframework.cloud.stream.annotation.StreamListener ;
import org.springframework.cloud.stream.binder.PollableMessageSource ;
import org.springframework.cloud.stream.messaging.Processor ;
import org.springframework.cloud.stream.messaging.Sink ;
import org.springframework.cloud.stream.messaging.Source ;
import org.springframework.context.annotation.Bean ;
import org.springframework.cloud.stream.provisioning.ConsumerDestination ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.DependsOn ;
import org.springframework.context.annotation.Profile ;
import org.springframework.core.ParameterizedTypeReference ;
import org.springframework.messaging.Message ;
import org.springframework.messaging.MessageChannel ;
import org.springframework.messaging.SubscribableChannel ;
import org.springframework.messaging.support.GenericMessage ;
import org.springframework.messaging.support.MessageBuilder ;
import org.springframework.test.annotation.DirtiesContext ;
import org.springframework.test.context.junit4.SpringRunner ;
import static com.alibaba.cloud.testsupport.Constant.TIME_OUT ;
import static org.hamcrest.CoreMatchers.is ;
import static org.junit.Assert.assertThat ;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE ;
@SpringCloudAlibaba ( composeFiles = "docker/rocket-compose-test.yml" , serviceName = "rocketmq-standalone" )
@TestExtend ( time = 10 * TIME_OUT )
@RunWith ( SpringRunner . class )
@EnableBinding ( { Processor . class , RocketmqProduceAndConsumerTests . PolledProcessor . class } )
@DirtiesContext
@TestExtend ( time = 6 * TIME_OUT )
//@EnableBinding({Processor.class, RocketmqProduceAndConsumerTests.PolledProcessor.class})
@SpringBootTest ( classes = RocketmqProduceAndConsumerTests . TestConfig . class , webEnvironment = NONE , properties = {
"spring.cloud.stream.rocketmq.binder.name-server=127.0.0.1:9876,127.0.0.1:9877" ,
"spring.cloud.stream.rocketmq.binder.group=flaky-group" ,
@ -64,15 +57,24 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.stream.bindings.input1.destination=TopicOrderTest" ,
"spring.cloud.stream.bindings.input1.content-type=application/json" ,
"spring.cloud.stream.bindings.input1.group=test-group1" ,
"spring.cloud.stream. rocketmq. bindings.input1.consumer.push.orderly=true",
"spring.cloud.stream. bindings.input1.consumer.push.orderly=true",
"spring.cloud.stream.bindings.input1.consumer.maxAttempts=1" , } )
public class RocketmqProduceAndConsumerTests {
@Autowired
private PolledProcessor processor ;
private MessageCollector collector ;
@Autowired
@Qualifier ( "input1" )
private MessageChannel input1 ;
@Autowired
@Qualifier ( "output" )
private MessageChannel output ;
@BeforeAll
public static void prepare ( ) {
}
@BeforeEach
@ -84,20 +86,23 @@ public class RocketmqProduceAndConsumerTests {
headers . put ( MessageConst . PROPERTY_TAGS , "TagA" ) ;
headers . put ( MessageConst . PROPERTY_ORIGIN_MESSAGE_ID , messageId ) ;
Message < SimpleMsg > msg = new GenericMessage ( new SimpleMsg ( "Hello RocketMQ" ) , headers ) ;
processor. output ( ) . send ( msg ) ;
input1 . send ( msg ) ;
}
@Test
@StreamListener ( PolledProcessor . CUSTOMIZE_OUTPUT )
public void testConsumeAndProduce ( ) throws InterruptedException {
public void testConsumeAndProduce ( ) throws Exception {
BlockingQueue < Message < ? > > messages = this . collector . forChannel ( this . output ) ;
processor. input ( ) . subscribe ( message - > Assertions . assertEquals ( message , "Hello RocketMQ" ) ) ;
assertThat( messages , is ( "Hello RocketMQ" ) ) ;
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration ( { AutoServiceRegistrationConfiguration . class ,
RocketMQBinderAutoConfiguration . class } )
@ImportAutoConfiguration ( value = { AutoServiceRegistrationConfiguration . class ,
RocketMQBinderAutoConfiguration . class } , exclude = {
DataSourceAutoConfiguration . class ,
TransactionAutoConfiguration . class ,
DataSourceTransactionManagerAutoConfiguration . class } )
public static class TestConfig {
}