- 问题:
cancel
的参数mayInterruptIfRunning
是什么意思? 从父类cancel方法的注释中可以寻找到答案,如果是 true 的话,即代表尝试通过中断的方式来停止任务
If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (RESULT_UPDATER.compareAndSet(this, null, CANCELLATION_CAUSE_HOLDER)) {
if (checkNotifyWaiters()) {
notifyListeners();
}
return true;
}
return false;
}
复制代码
- 问题:怎么有点玄学了,还能不是
interrupt
机制? 在任务内尝试捕获一下看看:
public static void testExceptionCatch() throws InterruptedException {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> scheduledFuture = null;
System.out.println(" start : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
try {
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(() -> {
System.out.println(" work : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//throw new RuntimeException("");
}, 5, 5, TimeUnit.SECONDS);
}catch (Exception exp){
exp.printStackTrace();
}
TimeUnit.SECONDS.sleep(15);
scheduledFuture.cancel(true);
System.out.println("cancel : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
TimeUnit.SECONDS.sleep(30);
}
复制代码
结果中的信息 java.lang.InterruptedException: sleep interrupted
可以明确是任务内的逻辑是可通过中断机制实现的。
start : 2022-12-10T20:10:31.248
work : 2022-12-10T20:10:36.276
work : 2022-12-10T20:10:41.272
work : 2022-12-10T20:10:46.277
cancel : 2022-12-10T20:10:46.277
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.wushiyii.lock.ScheduleTest.lambda$testExceptionCatch$1(ScheduleTest.java:39)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
复制代码
- 问题:之前实例中取消任务时,外部也无异常信息,线程池内部留着这个异常干嘛了呢? 直接抛出异常试试看
public static void testExceptionCatch() throws InterruptedException {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> scheduledFuture = null;
System.out.println(" start : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
try {
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(() -> {
System.out.println(" work : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
throw new RuntimeException("just throw ");
//throw new RuntimeException("");
}, 5, 5, TimeUnit.SECONDS);
}catch (Exception exp){
exp.printStackTrace();
}
TimeUnit.SECONDS.sleep(15);
scheduledFuture.cancel(true);
System.out.println("cancel : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
TimeUnit.SECONDS.sleep(30);
}
复制代码
仔细观察能看出,结果变的有意思了,work只执行了一次,前文中的执行结果中work都执行了3次,这里却只执行了一次。
start : 2022-12-10T20:16:53.285
work : 2022-12-10T20:16:58.307
cancel : 2022-12-10T20:17:08.305
复制代码
-
问题:任务内抛出异常能导致定时任务失去定时执行的能力? 是的,使用
scheduleAtFixedRate
有以下几个情况必须注意: -
任务逻辑中未捕获的异常能导致本该定时执行的任务,后续不再执行。
- 任务逻辑中未捕获的异常不会外抛,外部感知不到。
- 任务逻辑中的异常,需在任务逻辑内捕获并记录,否则无处可知。
看起来定时任务的使用的确是不能随心所欲的,毕竟大美也总是会说:

Original: https://blog.csdn.net/m0_74931226/article/details/128419044
Author: 财高八斗者
Title: 【自省】线程池里的定时任务跑的可欢了,可咋停掉特定的任务?
相关阅读
Title: 自动化测试框架unittest和pytest的区别
关键词:
unitest vs pytest

; unittest vs pytest
写这篇文章的目的不是在两者之间选择,谁是好的,哪个是坏的。而是对这两个框架进行比较,吸收它们的优秀特点和思想。
[En]
The purpose of writing this article is not to choose between the two, which is good and which is bad. But to compare the two frameworks and absorb their excellent characteristics and ideas.
unittest 和 pytest,是python里面两个测试框架。
unittest和pytest都是用例执行引擎。
如果让我选的话,我肯定选择unittest,但是没有任何黑pytest的意思。不知道你更喜欢哪个呢?欢迎评论告诉我。
我有100万个测试用例需要分发到100台服务器上运行。
[En]
I have 1 million test cases that need to be distributed to a hundred servers to run.
unittest可以分布式测试用例执行吗?
pytest可以分布式测试用例执行吗?
我关心的是什么?
哪些要点可以作为比较两个测试框架的注解?
[En]
Which points can be used as annotations to compare the two test frameworks?
一条用例要怎么写?
多条用例要怎么组织?
有哪些可用的钩子?
断言
用例失败的方式
用例通过的方式
它是可以并发执行还是分布式执行
[En]
Whether it can be executed concurrently or distributed
报告怎么样?
用例状态有哪些?
重试机制?
有没有类似于testng的监听器?
测试框架是否有侵入测试代码?unitest的测试用例需要继承对应的Class
如何控制用例的执行顺序?
[En]
How to control the execution order of use cases?
如何处理用例的依赖关系?
[En]
How to deal with the dependencies of use cases?
如何报告检测结果?
[En]
How to report the test results?
如何发现测试用例?
unittest 介绍
unittest是python的内置的一个模块。
The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.
To achieve this, unittest supports some important concepts in an object-oriented way:
test fixture
A test fixture represents the preparation needed to perform one or more tests, and any associated cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
test case
A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases.
test suite
A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
test runner
A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
unittest.TestCase类
留给使用者的一些钩子
setUp 每个用例(可以理解为方法)执行前运行
tearDown 每个用例执行后运行
在类级别的钩子
setUpClass 类执行前运行
tearDownClass 类执行后运行

Test Code example
unittest代码演示:


https://blog.csdn.net/lineuman/article/details/51854817
; pytest介绍
我对unittest比较熟悉,pytest才刚开始使用,等我使用过一段时间再来补充这篇文章。
人们喜欢pytest的原因之一是,当写pytest的测试用例的时候,不需要继承任何基类。
pytest: helps you write better programs.
The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.
pytest是一个三方包,需要pip install pytest
pytest的测试用例演示:

pytest or unittest?
unittest好用还是pytest好用?
这个问题没有标准的答案。不同的条件和不同的用户可能会给出不同的答案。
[En]
There is no standard answer to the question. Different conditions and different users may give different answers.
参考:
https://knapsackpro.com/testing_frameworks/difference_between/unittest/vs/pytest
https://www.pythonpool.com/python-unittest-vs-pytest/#:~:text=%20Unittest%20is%20the%20testing%20framework%20set%20in,require%20less%20piece%20of%20code%20compared%20to%20unittest.
Original: https://blog.csdn.net/lineuman/article/details/123668872
Author: 叶常落
Title: 自动化测试框架unittest和pytest的区别
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/350377/
转载文章受原作者版权保护。转载请注明原作者出处!