写并发 JUnit 用于非并发程序

huangapple go评论57阅读模式
英文:

Writing concurrent JUnit for Non-Concurrent Program

问题

我们有一个用于生成JWT令牌的“非并发”Java代码片段,现在需要为其编写JUnit测试。

关于此事有一些疑问:

  1. 是否可以为“非并发代码”编写“并发JUnit”测试?
  2. 如果是的话,除了正常流程以外,我应该尝试在其中进行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 :

  1. Is it valid to write concurrent junit for non-concurrent code?
  2. If yes, what I should try to JUnit in that except the normal flow? as there is not shared/mutable property.

答案1

得分: 4

我认为你要寻找的术语可能是 - '线程安全'。

如果你需要证明代码在多线程情况下正常工作

  1. 是的,应该进行测试。

  2. 如果代码(类/对象)是 '无状态' 的 - 测试应该更简单 - 验证输出仅为输入或非共享的基于时间/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

  1. yes it should be tested

  2. 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. )

huangapple
  • 本文由 发表于 2020年9月7日 11:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定