![](/img/trans.png)
[英]How do I unit test a Spring 4 DAO method with Spring Security?
[英]How do I Restart a shutdown embeddedKafkaServer in a Spring Unit Test?
我有一個 Spring-boot 單元測試,它在主 Kafka 集群上線時測試我的應用程序的切換回功能。
當主要離線時,應用程序成功切換到次要。 現在我們添加了在計時器上切換回主服務器而不是失敗的功能。
我的測試方法看起來像這樣:
//Rochelle = Primary BootStrapServers
//Hudson = Secondary BootStrapServers
@Test
public void send_switchback() throws Exception
{
//Get ABSwitchCluster to check failover details
KafkaSwitchCluster ktSwitch = (KafkaSwitchCluster)
((BootStrapExposerProducerFactory)
kafkaTemplate.getProducerFactory()).getBootStrapSupplier();
assertThat(ktSwitch, notNullValue());
assertThat(ktSwitch.get(), is(Rochelle));
assertThat(ktSwitch.isPrimary(), is(true));
assertThat(getBootStrapServersList(), is(Rochelle));
log.info("Shutdown Broker to test Failover.");
//Shutdown Primary Servers to simulate disconnection
shutdownBroker_primary();
//Allow for fail over to happen
if ( ktSwitch.isPrimary() )
{
try
{
synchronized (lock)
{ //pause to give Idle Event a chance to fire
for (int i = 0; i <= timeOut && ktSwitch.isPrimary(); ++i)
//while ( ktSwitch.isPrimary() )
{ //poll for cluster switch
lock.wait(Duration.ofSeconds(15).toMillis());
}
}
}
catch (InterruptedException IGNORE)
{ fail("Unable to wait for cluster switch. " + IGNORE.getMessage()); }
}
//Confirm Failover has happened
assertThat(ktSwitch.get(), is(Hudson));
assertThat(ktSwitch.isPrimary(), is(false));
assertThat(getBootStrapServersList(), is(Hudson));
assertThat(kafkaSwitchCluster.get(), is(Hudson));
assertThat(kafkaSwitchCluster.isPrimary(), is(false));
//Send a message on backup server
String message = "Test Failover";
send(message);
String msg = records.poll(10, TimeUnit.SECONDS);
assertThat(msg, notNullValue());
assertThat(msg, is(message));
startup_primary();
//embeddedKafkaRule.getEmbeddedKafka();
assertThat(embeddedKafka.getBrokersAsString(), is(Rochelle));
String brokers = embeddedKafka.getBrokersAsString();
if ( !kafkaProducerErrorHandler.areBrokersUp(brokers) )
{
synchronized (lock)
{
for ( int i=0;
i <= 15 && !kafkaProducerErrorHandler.areBrokersUp(brokers)
&& registry.isRunning();
++i )
{ lock.wait(Duration.ofSeconds(1).toMillis()); }
}
}
//TODO: test Scheduled Fire
kafkaProducerErrorHandler.primarySwitch();
if ( !kafkaSwitchCluster.isPrimary() )
{
try
{
synchronized (lock)
{ //pause to give Idle Event a chance to fire
for (int i = 0; i <= timeOut && !kafkaSwitchCluster.isPrimary(); ++i)
//while ( !ktSwitch.isPrimary() )
{ //poll for cluster switch
lock.wait(Duration.ofSeconds(15).toMillis());
}
}
}
catch (InterruptedException IGNORE)
{ fail("Unable to wait for cluster switch. " + IGNORE.getMessage()); }
}
assertThat(brokers, anyOf(is(Rochelle), is(Hudson))); //port didn't change
assertThat(brokers, is(Rochelle)); //is primary
assertThat(kafkaSwitchCluster.isPrimary(), is(true));
//assertThat(ktSwitch.isPrimary(), is(true));
assertThat(ktSwitch.get(), is(brokers));
assertThat(kafkaProducerErrorHandler.areBrokersUp(brokers), is(true));
assertThat(kafkaProducerErrorHandler.areBrokersUp(Rochelle), is(true));
assertThat(ktSwitch.isPrimary(), is(true));
//assertThat(ktSwitch.get(), not(anyOf(is(Hudson), is(Rochelle))));
assertThat(ktSwitch.get(), is(embeddedKafka.getBrokersAsString()));
//Send a message on backup server
message = "Test newPrimary";
send(message);
msg = records.poll(10, TimeUnit.SECONDS);
assertThat(msg, notNullValue());
assertThat(msg, is(message));
log.info("Test is finished");
}
我正在使用這種方法來關閉我的主要嵌入式 Kafka
public void shutdownBroker_primary()
{
for(KafkaServer ks : embeddedKafka.getKafkaServers())
{ ks.shutdown(); }
for(KafkaServer ks : embeddedKafka.getKafkaServers())
{ ks.awaitShutdown(); }
}
我正在使用它來重新啟動 Kafka:
public void startup_primary()
{
//registry.stop();
//kafkaSwitchCluster.Rochelle = embeddedKafka.getBrokersAsString();
for(KafkaServer ks : embeddedKafka.getKafkaServers()) { ks.startup(); }
registry.start();
}
primarySwitch()
是一個調度事件,用於將集群切換回主集群。 在測試中直接調用。 它是同一代碼的包裝器,可在 Kafka 宕機時切換使用中的集群。
如何在關閉主嵌入式 Kafka 集群后使其成功啟動,以便我可以證明應用程序可以在它再次可用后成功移回主集群?
更新:
我已經在 Github 上創建了代碼示例,到目前為止我所擁有的: https : //github.com/raystorm/Kafka-Example 。
它並不是真正為這個用例設計的,但是只要您不需要在代理實例之間保留數據,以下工作就可以了……
@SpringBootTest
@EmbeddedKafka(topics = "so64145670", bootstrapServersProperty = "spring.kafka.bootstrap-servers")
class So64145670ApplicationTests {
@Autowired
private EmbeddedKafkaBroker broker;
@Test
void restartBroker(@Autowired KafkaTemplate<String, String> template) throws Exception {
SendResult<String, String> sendResult = template.send("so64145670", "foo").get(10, TimeUnit.SECONDS);
System.out.println("+++" + sendResult.getRecordMetadata());
this.broker.destroy();
// restart
this.broker.afterPropertiesSet();
sendResult = template.send("so64145670", "bar").get(10, TimeUnit.SECONDS);
System.out.println("+++" + sendResult.getRecordMetadata());
}
}
編輯
這是一個有兩個經紀人的...
@SpringBootTest(classes = { So64145670Application.class, So64145670ApplicationTests.Config.class })
@EmbeddedKafka(topics = "so64145670", bootstrapServersProperty = "spring.kafka.bootstrap-servers")
class So64145670ApplicationTests {
@Autowired
private EmbeddedKafkaBroker embeddedKafka;
@Autowired
private EmbeddedKafkaBroker secondBroker;
@Test
void restartBroker(@Autowired KafkaTemplate<String, String> template,
@Autowired ProducerFactory<String, String> pf) throws Exception {
SendResult<String, String> sendResult = template.send("so64145670", "foo").get(10, TimeUnit.SECONDS);
System.out.println("+++" + sendResult.getRecordMetadata());
KafkaTemplate<String, String> secondTemplate = new KafkaTemplate<>(pf,
Map.of(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.secondBroker.getBrokersAsString()));
sendResult = secondTemplate.send("so64145670-1", "foo").get(10, TimeUnit.SECONDS);
System.out.println("+++" + sendResult.getRecordMetadata());
this.embeddedKafka.destroy();
this.secondBroker.destroy();
// restart
this.embeddedKafka.afterPropertiesSet();
this.secondBroker.afterPropertiesSet();
sendResult = template.send("so64145670", "bar").get(10, TimeUnit.SECONDS);
System.out.println("+++" + sendResult.getRecordMetadata());
sendResult = secondTemplate.send("so64145670-1", "bar").get(10, TimeUnit.SECONDS);
System.out.println("+++" + sendResult.getRecordMetadata());
}
@Configuration
public static class Config {
@Bean
EmbeddedKafkaBroker secondBroker() {
return new EmbeddedKafkaBroker(1, true, "so64145670-1")
.brokerListProperty("spring.kafka.second.server");
}
}
}
+++so64145670-1@0
+++so64145670-1-0@0
+++so64145670-1@0
+++so64145670-1-0@0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.