英文:
Writing concurrent JUnit for Non-Concurrent Program
问题
我们有一个用于生成JWT令牌的“非并发”Java代码片段,现在需要为其编写JUnit测试。
关于此事有一些疑问:
- 是否可以为“非并发代码”编写“并发JUnit”测试?
- 如果是的话,除了正常流程以外,我应该尝试在其中进行JUnit测试吗?因为没有“共享/可变”属性。
英文:
We have a non-concurrent
java snippet to generate jwt token and now need to write JUnit
for the same.
There are few queries about this :
- Is it valid to write
concurrent junit
fornon-concurrent code
? - If yes, what I should try to
JUnit
in that except the normal flow? as there is notshared/mutable
property.
答案1
得分: 4
我认为你要寻找的术语可能是 - '线程安全'。
如果你需要证明代码在多线程情况下正常工作
-
是的,应该进行测试。
-
如果代码(类/对象)是 '无状态' 的 - 测试应该更简单 - 验证输出仅为输入或非共享的基于时间/nonce 的内容。
(你没有分享代码 - 所以这是一个通用的回答。
在你的情况下,如果正在创建 JWT 令牌 - 意味着并发调用将返回不同的令牌值。因此断言应该确认相同。)
英文:
I think the term you are looking for is probably - 'thread safety'.
if you need to prove the code works in a multi-threaded scenario
-
yes it should be tested
-
if the code (class / object) is 'stateless' - the tests should be simpler -validate that output is only input or non-shared time/nonce based.
(you haven't shared code - so this is a general answer.
In your case if jwt token is being created - implies that - concurrent invocation will returns different token values. hence the assertions should confirm the same. )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论