我有一个连接到MongoAtlas上运行的Mongo群集的服务。目前,该测试套件连接到集群会运行许多测试,破坏创建的数据并关闭连接,至少那是应该做的。
测试套件的一部分:
class BandFeatureTests extends FeatureTest with BeforeAndAfter{
override val server = new EmbeddedHttpServer(
new GorillaServer{
override val overrideModules: Seq[TwitterModule] = Seq(
new DatabaseProviderTestModule()
)
override def warmup(): Unit = {/*no op*/}
},
flags = Map(
"dbscope" -> "testbands"
)
)
val mockDBProvider: MongoTestSpec = server.injector
.instance[MongoDataBaseConnector]
.asInstanceOf[MongoTestSpec]
override def afterAll(): Unit = {
mockDBProvider.close
}
test("Should Get Bands") {
server.httpGet(
path = "/bands",
andExpect = Status.Ok)
}
使用 dbscope 标志 来命名数据库,以便不同的测试套件可以启动不同的dbs并并行运行而不会删除彼此的数据
DatabaseTestProviderTestModule:
object DatabaseProviderTestModule extends TwitterModule {
flag(name="dbscope", default="KRONOSPROD", help = "Set The Database Scope")
@Provides
@Singleton
def providesDbscope(@Flag("dbscope") dbscope: String) : MongoDataBaseConnector = {
new MongoDataBaseConnector(dbscope)
}
}
MongoDatabaseConnector:
class MongoDataBaseConnector(dbscope: String) {
System.setProperty("org.mongodb.async.type", "netty")
val conf: Config = ConfigFactory.load()
val dbConfig: Config = conf.getConfig("db")
val user : String = dbConfig.getString("properties.username")
val pwd: String = dbConfig.getString("properties.pwd")
var url: String = s"mongodb+srv://$user:$pwd@cluster0-vrzo2.mongodb.net/test?retryWrites=true&w=majority"
val mongoClient: MongoClient = MongoClient(url)
val database: MongoDatabase = mongoClient.getDatabase(DATA_BASE_NAME)
def close = {
mongoClient.close()
}
//FOLLOWED BY ALL THE DOC DEFINITIONS AND CODECS
}
我注意到在套件完成并且所有测试通过之后,Mongo群集继续打印到sbt控制台:
23:41:43.182 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-01-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Sending command '{ "ismaster" : 1, "$db" : "admin", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1562715686, "i" : 2 } }, "signature" : { "hash" : { "$binary" : { "base64" : "fSDq9nBlwYNI+v2jd9bMV4HJzQI=", "subType" : "00" } }, "keyId" : 6709470559735906305 } } }' with request id 33 to database admin on connection [connectionId{localValue:1, serverValue:62629}] to server cluster0-shard-00-01-vrzo2.mongodb.net:27017
23:41:43.185 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-00-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.cluster - Checking status of cluster0-shard-00-00-vrzo2.mongodb.net:27017
23:41:43.187 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-00-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Sending command '{ "ismaster" : 1, "$db" : "admin", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1562715686, "i" : 2 } }, "signature" : { "hash" : { "$binary" : { "base64" : "fSDq9nBlwYNI+v2jd9bMV4HJzQI=", "subType" : "00" } }, "keyId" : 6709470559735906305 } } }' with request id 34 to database admin on connection [connectionId{localValue:3, serverValue:83104}] to server cluster0-shard-00-00-vrzo2.mongodb.net:27017
23:41:43.187 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-02-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.cluster - Checking status of cluster0-shard-00-02-vrzo2.mongodb.net:27017
23:41:43.189 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-02-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Sending command '{ "ismaster" : 1, "$db" : "admin", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1562715686, "i" : 2 } }, "signature" : { "hash" : { "$binary" : { "base64" : "fSDq9nBlwYNI+v2jd9bMV4HJzQI=", "subType" : "00" } }, "keyId" : 6709470559735906305 } } }' with request id 35 to database admin on connection [connectionId{localValue:4, serverValue:84492}] to server cluster0-shard-00-02-vrzo2.mongodb.net:27017
23:41:43.200 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-01-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 33 completed successfully in 18.31 ms on connection [connectionId{localValue:1, serverValue:62629}] to server cluster0-shard-00-01-vrzo2.mongodb.net:27017
23:41:43.201 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-01-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to {type=REPLICA_SET, servers=[{address=cluster0-shard-00-01-vrzo2.mongodb.net:27017, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=58.6 ms, state=CONNECTED}, {address=cluster0-shard-00-02-vrzo2.mongodb.net:27017, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=55.3 ms, state=CONNECTED}, {address=cluster0-shard-00-00-vrzo2.mongodb.net:27017, type=REPLICA_SET_PRIMARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=69.8 ms, state=CONNECTED}]
23:41:43.208 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-00-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 34 completed successfully in 21.74 ms on connection [connectionId{localValue:3, serverValue:83104}] to server cluster0-shard-00-00-vrzo2.mongodb.net:27017
23:41:43.209 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-00-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to {type=REPLICA_SET, servers=[{address=cluster0-shard-00-01-vrzo2.mongodb.net:27017, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=58.6 ms, state=CONNECTED}, {address=cluster0-shard-00-02-vrzo2.mongodb.net:27017, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=55.3 ms, state=CONNECTED}, {address=cluster0-shard-00-00-vrzo2.mongodb.net:27017, type=REPLICA_SET_PRIMARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=60.4 ms, state=CONNECTED}]
23:41:43.219 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-02-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 35 completed successfully in 30.67 ms on connection [connectionId{localValue:4, serverValue:84492}] to server cluster0-shard-00-02-vrzo2.mongodb.net:27017
23:41:43.219 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-02-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to {type=REPLICA_SET, servers=[{address=cluster0-shard-00-01-vrzo2.mongodb.net:27017, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=58.6 ms, state=CONNECTED}, {address=cluster0-shard-00-02-vrzo2.mongodb.net:27017, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=50.6 ms, state=CONNECTED}, {address=cluster0-shard-00-00-vrzo2.mongodb.net:27017, type=REPLICA_SET_PRIMARY, TagSet{[Tag{name='region', value='US_EAST_1'}, Tag{name='provider', value='AWS'}, Tag{name='nodeType', value='ELECTABLE'}]}, roundTripTime=60.4 ms, state=CONNECTED}]
23:41:53.203 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-01-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.cluster - Checking status of cluster0-shard-00-01-vrzo2.mongodb.net:27017
23:41:53.204 [cluster-ClusterId{value='5d25262bba2897b3082238a4', description='null'}-cluster0-shard-00-01-vrzo2.mongodb.net:27017] DEBUG org.mongodb.driver.protocol.command - Sending command '{ "ismaster" : 1, "$db" : "admin", "
现在,坦率地说,我不确定问题出在哪里,但是我做了几件事,将我的猜测偏向于Mongo。
我已在调试模式下确认src代码中被击中的 MongoDataBaseProvider 类的对象ID与被被击中的对象ID相同。 > mockDbProvider.close 我想知道Finatra注入器是否由于某种原因创建了一个单独的实例。事实并非如此。
我在afterEach函数中添加了一个额外的.close()。
override def afterEach(): Unit = {
mockDBProvider.close
}
通常添加此内容将导致afterAll()调用以出错。因为不会有打开的连接才能关闭。令我惊讶的是,事实并非如此。因此,我登录了MongoAtlas,以查看是否有任何指标。我意识到连接数为负,如下图所示。对此的合理解释是执行了第二个.close()调用,从而在每次测试运行时都将指标降至零以下。
我很好奇我是否错误地终止了它,或者是否有人遇到了这个问题。
P.S。有关此问题,请联系Mongo支持部门
答案 0 :(得分:0)
我愚蠢。原来,问题主要出在Finatra上(尽管我不确定为什么Mongo不会抛出任何错误)。
问题是由启动测试套件而不是功能测试引起的。我以为启动测试将“正常”关闭嵌入式服务器并触发清理。事实并非如此。添加此功能可解决启动测试中的问题
override protected def afterAll() = {
server.close()
}