Fix unstable case caused by using latch
@@ -10,6 +10,7 @@ import org.zstack.testlib.Test
import org.zstack.utils.data.SizeUnit
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
+import java.util.concurrent.atomic.AtomicInteger
/**
* Created by lining on 2018/1/14.
@@ -130,10 +131,12 @@ class BatchCreateVmFailOnLocalStorageCase extends SubCase{
ImageInventory image = env.inventoryByName("iso")
DiskOfferingInventory diskOffering = env.inventoryByName("diskOffering")
- int errorNum = 0
+ AtomicInteger errorNum = new AtomicInteger(0)
+
+ def threads = []
- final CountDownLatch latch = new CountDownLatch(10)
for (int i = 0; i < 10; i++) {
+ def thread = Thread.start {
CreateVmInstanceAction action = new CreateVmInstanceAction(
name : "test-" + i,
instanceOfferingUuid : instanceOffering.uuid,
@@ -144,25 +147,24 @@ class BatchCreateVmFailOnLocalStorageCase extends SubCase{
sessionId: Test.currentEnvSpec.session.uuid
)
- action.call(new Completion<CreateVmInstanceAction.Result>() {
- @Override
- void complete(CreateVmInstanceAction.Result ret) {
- latch.countDown()
+ CreateVmInstanceAction.Result ret = action.call()
if (ret.error != null) {
- errorNum ++
+ errorNum.incrementAndGet()
}
}
- })
+
+ threads.add(thread)
}
- latch.await(15, TimeUnit.SECONDS)
- assert errorNum >= 0
+ threads.each { it.join() }
+
+ assert errorNum.get() >= 0
ps = queryPrimaryStorage {
conditions=["uuid=${ps.uuid}".toString()]
}[0]
- assert SizeUnit.GIGABYTE.toByte(errorNum) == ps.availableCapacity
+ assert SizeUnit.GIGABYTE.toByte(errorNum.get()) == ps.availableCapacity
}
}
