有时候,需要对一些异步方法进行单元测试(尽管不推荐这样做),传统的assert只能在调用线程抛出AssertException,而测试框架往往只能监测主线程上的AssertException(JUnit),所以需要在主线程上等待异步调用结束,再去assert结果。
利用Java8的CompletableFuture
public class AsyncTestHelper<T> {
private CompletableFuture<T> mFuture = new CompletableFuture<>();
public CompletableFuture<T> getFuture() {
return mFuture;
}
public void assertBlock(Callable<T> callable) {
try {
T result = callable.call();
complete(result);
} catch (Throwable e) {
completeExceptionally(e);
}
}
public void complete(T value) {
mFuture.complete(value);
}
public void completeExceptionally(Throwable throwable) {
mFuture.completeExceptionally(throwable);
}
public T waitForCompletion() throws Throwable {
return waitForCompletion(0);
}
public T waitForCompletion(int timeout) throws Throwable {
T result = null;
try {
if (timeout <= 0) {
result = mFuture.get();
} else {
result = mFuture.get(timeout
, TimeUnit.MILLISECONDS);
}
} catch (Throwable ex) {
if (ex instanceof ExecutionException) {
throw ex.getCause();
} else {
throw ex;
}
}
return result;
}
}
使用例子如下所示:
@Test
public void assertGetDeviceInfo() throws Throwable {
XLinkCoreDevice nullDevice = createTestDevice();
final AsyncTestHelper<XLinkCoreDeviceInfo> helper = new AsyncTestHelper<>();
XLinkCoreSDK.getInstance().getDeviceInfo(
nullDevice,
new XLinkCoreSDK.Callback<XLinkCoreDeviceInfo>() {
@Override
public void onSuccess(final XLinkCoreDeviceInfo result) {
println("onSuccess() called with: result = [" + result + "]");
// 异步回调
helper.assertBlock(new Callable<XLinkCoreDeviceInfo>() {
@Override
public XLinkCoreDeviceInfo call() throws Exception {
assertEquals(true, result.isBound());
assertEquals(5, result.getProtocolVersion());
assertArrayEquals(ByteUtil.hexToBytes(TEST_DEVICE_MAC), result.getMac());
assertEquals(TEST_DEVICE_PRODUCT_ID, result.getPid());
return result;
}
});
}
@Override
public void onFail(XLinkCoreException exception) {
println("onFail() called with: exception = [" + exception + "]");
helper.completeExceptionally(exception);
}
}
);
helper.waitForCompletion(); // 等待结果,并抛出原有的Exception
}
欢迎提出任何建议~
我对spring roo 感兴趣,想与你沟通一下。11730177
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.binance.com/ar/register?ref=V2H9AFPY
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Your article helped me a lot, is there any more related content? Thanks!
Those extra skilled and have previously launched their our bodies to Anavar would possibly operate in greater dosage
territory, typically hovering across the 50mg per day mark.
Amongst the opposite advantages, it also brings to the table its ability to enhance muscular endurance.
When the purpose is a lean and defined physique, workouts usually contain high repetition ranges and shorter relaxation intervals.
Anavar, with its ability to spice up muscular endurance, allows you to thrive via these exercises, empowering you to
stay on observe along with your cutting section goals whereas lending more definition to your muscle tissue.
Test also increases purple blood cell production, thereby
improving oxygen supply to muscular tissues and enhancing endurance.
These are just a number of the advantages that make Testosterone a cornerstone in anabolic steroid
cycles. Its popularity is due to its relatively gentle side effects and its ability to advertise
muscle growth and energy gains. Primobolan is most commonly used as an injectable
steroid but can also be taken in capsule kind.
However, Anavar isn’t thought-about an especially potent bulking agent for male bodybuilders and it’s far more popular as a chopping agent.
When it comes to constructing a massive and ripped physique,
the mixture of Anavar and Testosterone offers you with a potent combination of compounds!
Even though Primobolan is often used for cutting – it’s not the only anabolic compound with the ability to
shred fat and protect muscle. When used
by itself, a typical Primobolan cycle for girls can final
weeks and even a few months – if structured safely and
underneath the watchful eye of an expert.
Yes, we will reiterate another time how essential it is to not depend on drugs alone to higher your
physique. You can find loads of Anavar evaluations that preach how good
it could be, however solely when you’re eating clean, coaching onerous and have been understanding for years.
Assuming you put in a robust total effort, then you’re going
to get great results with Anavar. Like most oral steroids, Anavar may give you
some loopy pumps in sure muscle groups – sometimes so dangerous that
you could’t even finish a exercise. Clearly this isn’t one of the good Anavar
outcomes, so what you want to do is take some taurine when you’re prone
to harsh pumps. This level has been touched on earlier than, but it’s price reiterating that
Anavar tablets can undoubtedly provide an excellent energy enhance.
Obviously that is dependent upon the person and you are not going to break the world
bench-press report just because you do an Anavar cycle.
Everybody is genetically different and may have a singular response, as far as the benefits and unwanted effects
of steroids are concerned. With that being mentioned, ladies ought to pay attention to the attainable androgenic unwanted aspect
effects that Primobolan may cause. This eliminates the chance of
suffering from high blood pressure and water retention. It is rumored that Primobolan was truly certainly one of Arnold Schwarzenegger’s favourite steroids, during
the peak of his bodybuilding career. Even although the
aim of this article is that of discussing one of the best Primobolan cycle for women; we should first understand how the peculiar steroid works.
If combined with other steroids, the probabilities of
unwanted effects, including liver damage, rise considerably.
anavar cycle results also can influence blood sugar levels, so those with diabetes should exercise warning.
Always inform your doctor about any medicines or supplements you take to avoid harmful interactions.
Anavar is a mild compound, thus it’s uncommon for users to
note any main side effects (when used in moderate doses for brief intervals
of time). Anavar (along with all different DHTs)
have an inability to convert into estrogen. This means that whereas positive aspects
from Anavar will be quite lean and dry, they will
not be as powerful as others, because the strategy of muscle development is
massively aided by the addition of estrogen. Your menstrual cycle is greater than “just a interval.” It’s an important
sign…a key piece of details about your body’s total well
being.
Overdoing it, ignoring early unwanted aspect effects, or stacking
it with harsher compounds can flip a strategic edge into a long-term legal
responsibility. Nevertheless, non-medical use for physique or efficiency enhancement is prohibited in most nations and is banned
by all major sports activities organizations, including the World Anti-Doping Agency (WADA).
The Swole Kitchen CoachingTailored diet and restoration protocols designed specifically
for female physiology post-cycle.
Tanveer Quraishi, writer of Steroids a hundred and one has in depth expertise within the field of bodybuilding and has been writing on-line on various
muscle-building and different health topics for many
years now. He isn’t just thinking about bodybuilding but is a great
soccer participant too. When he isn’t writing for his web site or training
at the gym, he likes to spend his time with this wife and youngsters.
This table offers real-world examples of the changes individuals have skilled through
the use of Anavar. Nevertheless, the period could vary depending on particular
person objectives and tolerance levels. Remember, it is essential to consult with a
healthcare skilled or experienced coach before starting any Anavar routine.