1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <chrono>
#include <thread>
#include "ErrorList.h"
#include "gtest/gtest.h"
#include "mozilla/MozPromise.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsISupportsImpl.h"
#include "nsThreadUtils.h"
using namespace mozilla;
// Simple function to be able to distinguish threads in output
size_t tid() {
return std::hash<std::thread::id>{}(std::this_thread::get_id());
}
// Invoking something on a background thread, but getting the completion on the
// main thread.
TEST(MozPromiseExamples, InvokeAsync)
{
bool done = false;
InvokeAsync(
GetCurrentSerialEventTarget(), __func__,
[]() {
printf("[%zu] Doing some work on a background thread...\n", tid());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
printf("[%zu] Done...\n", tid());
// Simulate various outcomes:
srand(getpid());
switch (rand() % 4) {
case 0:
return GenericPromise::CreateAndResolve(true, __func__);
case 1:
return GenericPromise::CreateAndResolve(false, __func__);
case 2:
return GenericPromise::CreateAndReject(NS_ERROR_OUT_OF_MEMORY,
__func__);
default:
return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
}
})
->Then(
GetMainThreadSerialEventTarget(), __func__,
[&done](GenericPromise::ResolveOrRejectValue&& aResult) {
if (aResult.IsReject()) {
printf("[%zu] Back on the main thread, the task failed: 0x%x\n",
tid(), (unsigned int)aResult.RejectValue());
done = true;
return;
}
printf("[%zu] back on the main thread, sucess, return value: %s\n",
tid(), aResult.ResolveValue() ? "true" : "false");
done = true;
});
// Process all events and check that `done` was effectively set to true. This
// is just for the purpose of this test.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(MozPromiseExamples, OneOff)"_ns, [&done]() { return done; }));
EXPECT_TRUE(done);
}
class Something final {
public:
explicit Something(uint32_t aMilliseconds = 100)
: mMilliseconds(aMilliseconds) {}
RefPtr<GenericPromise> DoIt() {
// Do no dispatch the async task twice if still underway.
if (mPromise) {
return mPromise;
}
mPromise = mHolder.Ensure(__func__);
// Kick off some work to another thread...
std::thread([self = RefPtr{this}, this] {
printf("[%zu] Working...\n", tid());
std::this_thread::sleep_for(std::chrono::milliseconds(mMilliseconds));
printf("[%zu] Resolving from background thread\n", tid());
self->mHolder.Resolve(true, __func__);
}).detach();
return mPromise;
}
private:
~Something() = default;
const uint32_t mMilliseconds;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Something)
RefPtr<GenericPromise> mPromise;
MozPromiseHolder<GenericPromise> mHolder{};
};
// Waiting for something asynchronous to complete, from outside the instance
TEST(MozPromiseExamples, OneOff)
{
RefPtr<Something> thing(new Something);
bool done = false;
thing->DoIt()->Then(
GetCurrentSerialEventTarget(), __func__,
[&done, thing](bool aResult) {
printf("[%zu] Success: %s\n", tid(), aResult ? "true" : "false");
done = true;
},
[&done](nsresult aError) {
printf("[%zu] Failure: 0x%x\n", tid(), (unsigned)aError);
done = true;
});
// Process all events and check that `done` was effectively set to true. This
// is just for the purpose of this test.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(MozPromiseExamples, OneOff)"_ns, [&done]() { return done; }));
}
class SomethingCancelable final {
public:
RefPtr<GenericPromise> DoIt() {
if (mPromise) {
return mPromise;
}
mPromise = mHolder.Ensure(__func__);
// Kick off some work to another thread...
std::thread([self = RefPtr{this}] {
printf("[%zu] Working...\n", tid());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// This is printed: despite being canceled, the thread runs normally and
// resolves its promise.
printf("[%zu] Resolving from background thread\n", tid());
self->mHolder.Resolve(true, __func__);
}).detach();
return mPromise;
}
private:
~SomethingCancelable() = default;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SomethingCancelable)
MozPromiseHolder<GenericPromise> mHolder{};
RefPtr<GenericPromise> mPromise;
MozPromiseRequestHolder<GenericPromise> mRequest;
};
// Kick of an asynchronous job, and cancel it
TEST(MozPromiseExamples, OneOffCancelable)
{
RefPtr<SomethingCancelable> thing(new SomethingCancelable);
// Start a job that takes 100ms
MozPromiseRequestHolder<GenericPromise> holder;
thing->DoIt()
->Then(GetCurrentSerialEventTarget(), __func__,
[&holder] {
holder.Complete();
// This is never printed: in this example we disconnect the
// request before completion.
printf("[%zu] Async work finished", tid());
})
->Track(holder);
// But cancel it after just 10ms
std::this_thread::sleep_for(std::chrono::milliseconds(10));
holder.Disconnect();
}
// Waiting for multiple asynchronous tasks to complete, from outside the
// instance
TEST(MozPromiseExamples, MultipleWaits)
{
nsTArray<RefPtr<Something>> things;
uint32_t count = 10;
while (count--) {
things.AppendElement(new Something(count * 10));
}
bool done = false;
nsTArray<RefPtr<GenericPromise>> promises;
for (auto& thing : things) {
promises.AppendElement(thing->DoIt());
}
GenericPromise::All(GetCurrentSerialEventTarget(), promises)
->Then(
GetCurrentSerialEventTarget(), __func__,
[&done](nsTArray<bool>&& aResults) {
nsCString results;
for (auto b : aResults) {
results.AppendPrintf("%s, ", b ? "true" : "false");
}
printf("[%zu] All succeeded: %s\n", tid(), results.get());
done = true;
},
[&done](nsresult aError) {
printf("[%zu] One failed: 0x%x\n", tid(), (unsigned)aError);
done = true;
});
// Process all events and check that `done` was effectively set to true. This
// is just for the purpose of this test.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(MozPromiseExamples, OneOff)"_ns, [&done]() { return done; }));
}
RefPtr<GenericPromise> SyncOperation(uint32_t aConstraint) {
printf("[%zu] SyncOperation(%" PRIu32 ")\n", tid(), aConstraint);
if (aConstraint > 5) {
return GenericPromise::CreateAndReject(NS_ERROR_UNEXPECTED, __func__);
}
return GenericPromise::CreateAndResolve(true, __func__);
}
// This test uses various MozPromise facilities and prints a message to the
// console, to show how the scheduling works.
TEST(MozPromiseExamples, SyncReturn)
{
bool done = false;
// Dispatch a runnable to the current even loop, for the sole purpose of
// understanding ordering.
NS_DispatchToCurrentThread(NS_NewRunnableFunction("Initial runnable", [] {
printf("[%zu] Dispatched before sync promise operation\n", tid());
}));
// SyncOperation synchronously returns a resolved promise. However, `Then`
// works by dispatching so the printf will happen after InitialRunnable.
SyncOperation(3)->Then(
GetCurrentSerialEventTarget(), __func__,
[](bool aResult) {
printf("[%zu] Sync promise value: %s\n", tid(),
aResult ? "true" : "false");
},
[](nsresult aError) {
printf("[%zu] Error: 0x%x\n", tid(), (unsigned)aError);
});
// Now call the same method, but invoke it async on the current event queue.
// The resolve will also be in its own event loop task. It follows that this
// will be printed after the "Final Runnable" below.
// MozPromise can be put in tail dispatch mode,or sync mode, and in those
// cases, the ordering will be different.
InvokeAsync(GetCurrentSerialEventTarget(), __func__,
[]() { return SyncOperation(4); })
->Then(
GetCurrentSerialEventTarget(), __func__,
[&done](bool aResult) {
printf("[%zu] Sync promise value (InvokeAsync): %s\n", tid(),
aResult ? "true" : "false");
done = true;
},
[](nsresult aError) {
printf("[%zu] Error (InvokeAsync): 0x%x\n", tid(),
(unsigned)aError);
});
// Dispatch a runnable to the current even loop, for the sole purpose of
// understanding ordering.
NS_DispatchToCurrentThread(NS_NewRunnableFunction("Final runnable", [] {
printf("[%zu] Dispatched after sync promise operation\n", tid());
}));
// The output will be as such (omitting the thread ids):
// [...] SyncOperation(3)
// [...] Dispatched before sync promise operation
// [...] Sync promise value: true
// [...] SyncOperation(4)
// [...] Dispatched after sync promise operation
// [...] Sync promise value (InvokeAsync): true
// Process all events and check that `done` was effectively set to true. This
// is just for the purpose of this test.
MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
"xpcom:TEST(MozPromiseExamples, OneOff)"_ns, [&done]() { return done; }));
}
using IntPromise = MozPromise<int, nsresult, true>;
using UintPromise = MozPromise<unsigned, nsresult, true>;
class SomethingSync {
public:
RefPtr<GenericPromise> DoSomethingSync() {
return GenericPromise::CreateAndResolve(true, "Returning true");
}
};
TEST(MozPromiseExamples, Chaining)
{
bool done = false;
SomethingSync s;
// Do something that returns a bool, then chain it to a promise that returns
// an int, then to a promise that returns an unsigned.
s.DoSomethingSync()
->Then(GetCurrentSerialEventTarget(), __func__,
[](GenericPromise::ResolveOrRejectValue&& aValue) {
if (aValue.IsResolve()) {
// Depending on the value of the bool, find the proper signed
// integer value.
return IntPromise::CreateAndResolve(
aValue.ResolveValue() ? 3 : 5,
"Example IntPromise Resolver");
}
return IntPromise::CreateAndReject(
aValue.RejectValue(), "Example IntPromise Rejecter");
})
->Then(GetCurrentSerialEventTarget(), __func__,
[&done](IntPromise::ResolveOrRejectValue&& aValue) {
if (aValue.IsResolve()) {
// Depending on the value of the signed integer, find the
// proper unsigned integer value.
done = true;
return UintPromise::CreateAndResolve(
static_cast<unsigned>(aValue.ResolveValue()),
"Example UintPromise Resolver");
}
return UintPromise::CreateAndReject(
aValue.RejectValue(), "Example UintPromise Rejecter");
});
// Process all events and check that `done` was effectively set to true. This
// is just for the purpose of this test.
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil("xpcom:TEST(MozPromiseExamples, Chaining)"_ns,
[&done]() { return done; }));
}
// - converting an async legacy callback interface to a modern MozPromise
// version with MozPromiseHolder.
|