在携程使用 TestNG/JUnit 并行跑 Appium 有两个关键的地方:
- 独立使用的 Appium Server Wrapper
- 控制并行的 Device Store Queue
Word is Cheap,示例代码如下 (公布这些代码是希望大家更多的精力放到改进 Appium 本身):
public abstract class AbstractDeviceStore<T extends Device> implements
DeviceStore<T> {
protected static final Logger log = LoggerFactory
.getLogger(AbstractDeviceStore.class);
protected final ReentrantLock lock;
protected final Condition notEmpty;
protected final TreeSet<Device> devicesInUse = new TreeSet<>();
protected final Set<T> devices = new TreeSet<>();
protected AbstractDeviceStore() {
lock = new ReentrantLock(true);
notEmpty = lock.newCondition();
}
@Override
public Device take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
// log.debug("devices in use size {}", devicesInUse);
while (devicesInUse.isEmpty()) {
log.debug("take need wait ....");
notEmpty.await();
}
return devicesInUse.pollFirst();
} finally {
lock.unlock();
}
}
@Override
public void put(Device device) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
devicesInUse.add(device);
notEmpty.signal();
} finally {
lock.unlock();
}
}
@Override
public Set<T> getDevices() {
return new TreeSet<T>(devices);
}
}