引入maven包
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.1.1</version>
</dependency>
测试示例
package demo.testng.retry;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Awaitility.with;
import static org.awaitility.Durations.ONE_HUNDRED_MILLISECONDS;
/**
* 异步校验工具 awaitility 快速入门:
* https://testerhome.com/topics/7408
*
* @author mcc
* @Date: 2022-02-14 11:37
*/
public class LoopDemo {
interface CounterService extends Runnable {
int getCount();
}
// 每隔1s, count累加1
class CounterServiceImpl implements CounterService {
private volatile int count = 0;
public void run() {
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int index = 0; index < 5; index++) {
Thread.sleep(1000);
count += 1;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
public int getCount() {
return count;
}
}
// 3-1.默认等待时间
@Test
public void testAsynchronousNormal() {
final CounterService service = new CounterServiceImpl();
service.run();
try {
// 默认10s, 如果在这时间段内,条件依然不满足,将抛出ConditionTimeoutException
await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return service.getCount() == 5;
}
});
} catch (Exception e) {
Assert.fail("测试代码运行异常:" + e.getMessage() + ",代码位置:" + e.getStackTrace()[0].toString());
}
}
// 3-2.最多等待
@Test
public void testAsynchronousAtMost(){
final CounterService service = new CounterServiceImpl();
service.run();
try{
// 指定超时时间3s, 如果在这时间段内,条件依然不满足,将抛出ConditionTimeoutException
await().atMost(3, SECONDS).until(() -> service.getCount() == 5);
} catch (Exception e) {
Assert.fail("测试代码运行异常:" + e.getMessage() + ",代码位置:" + e.getStackTrace()[0].toString());
}
}
// 3-3.至少等待
@Test
public void testAsynchronousAtLeast(){
final CounterService service = new CounterServiceImpl();
service.run();
try{
// 指定至少1s, 最多3s, 如果在这时间段内,条件依然不满足,将抛出ConditionTimeoutException
await().atLeast(1, SECONDS).and().atMost(3, SECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return service.getCount() == 20;
}
});
} catch (Exception e) {
Assert.fail("测试代码运行异常:" + e.getMessage() + ",代码位置:" + e.getStackTrace()[0].toString());
}
}
// 3-5.轮询
/**
* // 设置超时时间,6s
* atMost(6, SECONDS)
*
* // 设置间隔100ms
* pollInterval(ONE_HUNDRED_MILLISECONDS)
*
* // 设置延迟50ms
* pollDelay(50, MILLISECONDS)
*
* // 设置提示语
* await("count is greater 6")
*
* // 连接
* and()
*
* // 等待java.util.concurrent.Callable返回true
* until(
* new Callable<Boolean>() {
* @Override
* public Boolean call() throws Exception {
* return service.getCount() == 6;
* }
* });
*/
@Test
public void testAsynchronousPoll(){
final CounterService service = new CounterServiceImpl();
service.run();
try{
// 轮询查询,pollInterval每隔多少时间段轮询, pollDelay每次轮询间隔时间
with().pollInterval(ONE_HUNDRED_MILLISECONDS).atMost(60,SECONDS).and().with().pollDelay(50, MILLISECONDS).await("count is greater 3").until(
// new Callable<Boolean>() {
// @Override
// public Boolean call() throws Exception {
// return service.getCount() == 4;
// }
// });
() -> service.getCount() == 40);
} catch (Exception e) {
Assert.fail("测试代码运行异常:" + e.getMessage() + ",代码位置:" + e.getStackTrace()[0].toString());
}
}
// 3-6.Fibonacci 轮询
@Test
public void testAsynchronousFibonacciPoll(){
final CounterService service = new CounterServiceImpl();
service.run();
try{
// 使用fibonacci数作为间隔数1,1,2,3,5,8,..., 默认单位milliseconds with().pollInterval(fibonacci(SECONDS)).await("count is greater 3").until(
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return service.getCount() == 4;
}
};
} catch (Exception e) {
Assert.fail("测试代码运行异常:" + e.getMessage() + ",代码位置:" + e.getStackTrace()[0].toString());
}
}
public int longestSubStr(){
String s = "asschddf";
int n = s.length();
int rk = 0, ans = 0;
Set<Character> occ = new HashSet<>();
for (int i = 0; i < n; i++) {
if (i != 0) {
occ.remove(s.charAt(i-1));
}
while (rk<n&&!occ.contains(s.charAt(rk))){
occ.add(s.charAt(rk));
rk++;
}
ans=Math.max(ans,rk-i);
}
return ans;
}
public static void main(String[] args) {
}
}