异步校验工具

引入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) {


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值