gpt4 book ai didi

java - 如何使用 cassandra spring 单元执行多个测试

转载 作者:行者123 更新时间:2023-12-05 07:47:48 24 4
gpt4 key购买 nike

我的测试看起来像:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:testContext.xml")
@TestExecutionListeners(listeners = { CassandraUnitDependencyInjectionTestExecutionListener.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
@CassandraDataSet(value = "create_schema.cql", keyspace = "myKeyspace")
@EmbeddedCassandra(timeout = 20000)
public class MyItest {

protected final Logger logger = LogManager.getLogger(getClass());

@Test
public void test1() throws Exception {
System.out.println("Start1");
...
System.out.println("End1");
}

@Test
public void test2() throws Exception {
System.out.println("Start2");
...
System.out.println("End2");
}

当我在我的 STS 中一项一项地运行这些测试时,它工作正常。但是当我一起运行这两个测试时,我得到了异常:

2016-08-31 13:15:37,962 [HANDSHAKE-/127.0.0.1] INFO  - OutboundTcpConnection      - Handshaking version with /127.0.0.1
Start1
2016-08-31 13:15:39,326 [cassandraCluster-worker-5] WARN - Cluster - Re-preparing already prepared query is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once. Query='...'
2016-08-31 13:15:39,331 [cassandraCluster-worker-6] WARN - Cluster - Re-preparing already prepared query is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once. Query='...'
End1
2016-08-31 13:15:39,348 [cassandraCluster-worker-1] WARN - Cluster - Re-preparing already prepared query is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once. Query='...'
2016-08-31 13:15:41,401 [cassandraCluster-worker-3] WARN - Cluster - Re-preparing already prepared query is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once. Query='...'
2016-08-31 13:15:42,174 [main] INFO - DCAwareRoundRobinPolicy - Using data-center name 'datacenter1' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
2016-08-31 13:15:42,174 [main] INFO - Cluster - New Cassandra host localhost/127.0.0.1:9142 added
2016-08-31 13:15:42,191 [SharedPool-Worker-3] INFO - MigrationManager - Drop Keyspace 'rob'
2016-08-31 13:15:54,227 [main] ERROR - TestContextManager - Caught exception while allowing TestExecutionListener [org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener@1a9e7c8] to prepare test instance [cz.isvs.reg.rob.ws.repository.dao.MyITest@6e505c]
com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9142 (com.datastax.driver.core.exceptions.OperationTimedOutException: [localhost/127.0.0.1:9142] Timed out waiting for server response))
at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:84) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:37) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.DriverThrowables.propagateCause(DriverThrowables.java:37) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:245) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:64) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:39) ~[cassandra-driver-core-3.0.3.jar:?]
at org.cassandraunit.CQLDataLoader.initKeyspaceContext(CQLDataLoader.java:62) ~[cassandra-unit-3.0.0.1.jar:?]
at org.cassandraunit.CQLDataLoader.load(CQLDataLoader.java:31) ~[cassandra-unit-3.0.0.1.jar:?]
at org.cassandraunit.spring.AbstractCassandraUnitTestExecutionListener.startServer(AbstractCassandraUnitTestExecutionListener.java:70) ~[cassandra-unit-spring-3.0.0.1.jar:?]
at org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener.prepareTestInstance(CassandraUnitDependencyInjectionTestExecutionListener.java:21) ~[cassandra-unit-spring-3.0.0.1.jar:?]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:?]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:?]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:?]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:?]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:?]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:?]
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9142 (com.datastax.driver.core.exceptions.OperationTimedOutException: [localhost/127.0.0.1:9142] Timed out waiting for server response))
at com.datastax.driver.core.RequestHandler.reportNoMoreHosts(RequestHandler.java:207) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.RequestHandler.access$1000(RequestHandler.java:43) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.RequestHandler$SpeculativeExecution.sendRequest(RequestHandler.java:273) ~[cassandra-driver-core-3.0.3.jar:?]
at com.datastax.driver.core.RequestHandler$SpeculativeExecution$1.run(RequestHandler.java:396) ~[cassandra-driver-core-3.0.3.jar:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[?:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[?:1.8.0_66]
at java.lang.Thread.run(Thread.java:745) ~[?:1.8.0_66]
2016-08-31 13:15:54,280 [main] INFO - CassandraHostRetryService - Downed Host Retry service started with queue size -1 and retry delay 10s
2016-08-31 13:15:54,282 [main] INFO - JmxMonitor - Registering JMX me.prettyprint.cassandra.service_TestCluster:ServiceType=hector,MonitorType=hector
2016-08-31 13:15:54,355 [Thrift:2] INFO - MigrationManager - Drop Keyspace 'rob'
2016-08-31 13:16:03,475 [StorageServiceShutdownHook] INFO - ThriftServer - Stop listening to thrift clients
2016-08-31 13:16:03,476 [Thread-2] INFO - GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@153d0f5: startup date [Wed Aug 31 13:15:37 CEST 2016]; root of context hierarchy

在日志中,我可以看到它在 1 次成功测试后删除了 Keyspace,然后出现了一些超时和 NoHostAvailableException。我应该如何修复此错误?

编辑:

CassandraUnitDependencyInjectionTestExecutionListener 中我可以看到:

if (Boolean.TRUE.equals(testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Cleaning and reloading server for test context [" + testContext + "].");
}
cleanServer();
startServer(testContext);
}

如何将此属性更改为 false ?

最佳答案

这是 cassandra-unit 的默认行为。它在每次测试执行后调用 CassandraUnitTestExecutionListener.cleanServer() 并删除所有非系统键空间。您可以在每次测试之前创建键空间,也可以使用 @CassandraDataSet 注释。希望这可以帮助。

编辑:更多细节

我正在使用 cassandra-unit-spring-2.1.9.2,我能够执行该类的所有测试。您遇到的 NoHostAvailableException 问题是未决问题。请检查

https://github.com/jsevellec/cassandra-unit/issues/158

https://github.com/jsevellec/cassandra-unit/issues/154

关于java - 如何使用 cassandra spring 单元执行多个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39249224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com