|
1 | 1 | #include <userver/utest/utest.hpp> |
2 | 2 |
|
| 3 | +#include <functional> |
3 | 4 | #include <optional> |
4 | 5 | #include <utility> |
5 | 6 |
|
6 | 7 | #include <userver/engine/async.hpp> |
7 | 8 | #include <userver/engine/single_use_event.hpp> |
8 | 9 | #include <userver/engine/sleep.hpp> |
| 10 | +#include <userver/engine/task/current_task.hpp> |
| 11 | +#include <userver/engine/task/inherited_variable.hpp> |
9 | 12 | #include <userver/engine/task/local_variable.hpp> |
10 | 13 | #include <userver/utils/async.hpp> |
11 | 14 |
|
| 15 | +#include <engine/task/task_processor.hpp> |
| 16 | + |
12 | 17 | USERVER_NAMESPACE_BEGIN |
13 | 18 |
|
14 | 19 | namespace { |
@@ -198,4 +203,203 @@ UTEST(TaskLocalVariable, WaitInDestructorCancelled) { |
198 | 203 | UEXPECT_THROW(task.Get(), engine::TaskCancelledException); |
199 | 204 | } |
200 | 205 |
|
| 206 | +namespace { |
| 207 | + |
| 208 | +struct DtorCallback final { |
| 209 | + std::function<void()> on_destroy; |
| 210 | + |
| 211 | + DtorCallback() = default; |
| 212 | + DtorCallback(const DtorCallback&) = delete; |
| 213 | + DtorCallback& operator=(const DtorCallback&) = delete; |
| 214 | + |
| 215 | + ~DtorCallback() { |
| 216 | + if (on_destroy) { |
| 217 | + on_destroy(); |
| 218 | + } |
| 219 | + } |
| 220 | +}; |
| 221 | + |
| 222 | +engine::TaskLocalVariable<DtorCallback> kDtorCallback; |
| 223 | +engine::TaskLocalVariable<int> kObservedInt; |
| 224 | + |
| 225 | +} // namespace |
| 226 | + |
| 227 | +// Situation 1a: GetOptional on a live (not yet destroyed) variable. |
| 228 | +UTEST(TaskLocalVariable, GetOptionalAlive) { |
| 229 | + EXPECT_EQ(kObservedInt.GetOptional(), nullptr); |
| 230 | + |
| 231 | + *kObservedInt = 42; |
| 232 | + ASSERT_NE(kObservedInt.GetOptional(), nullptr); |
| 233 | + EXPECT_EQ(*kObservedInt.GetOptional(), 42); |
| 234 | +} |
| 235 | + |
| 236 | +// Situation 1b: a variable that is initialized earlier (thus destroyed later) |
| 237 | +// is still observable from the destructor of another task-local variable. |
| 238 | +UTEST(TaskLocalVariable, GetOptionalAliveFromOtherVariableDestructor) { |
| 239 | + bool checked = false; |
| 240 | + |
| 241 | + engine::AsyncNoTracing([&] { |
| 242 | + // kObservedInt is initialized first => destroyed after kDtorCallback. |
| 243 | + *kObservedInt = 42; |
| 244 | + kDtorCallback->on_destroy = [&] { |
| 245 | + auto* const observed = kObservedInt.GetOptional(); |
| 246 | + ASSERT_NE(observed, nullptr); |
| 247 | + EXPECT_EQ(*observed, 42); |
| 248 | + checked = true; |
| 249 | + }; |
| 250 | + }).Get(); |
| 251 | + |
| 252 | + EXPECT_TRUE(checked); |
| 253 | +} |
| 254 | + |
| 255 | +// Situation 2: GetOptional on an already-destroyed variable must return |
| 256 | +// nullptr, not a dangling pointer. |
| 257 | +UTEST(TaskLocalVariable, GetOptionalAfterVariableDestroyed) { |
| 258 | + bool checked = false; |
| 259 | + |
| 260 | + engine::AsyncNoTracing([&] { |
| 261 | + kDtorCallback->on_destroy = [&] { |
| 262 | + EXPECT_EQ(kObservedInt.GetOptional(), nullptr); |
| 263 | + checked = true; |
| 264 | + }; |
| 265 | + // kObservedInt is initialized after kDtorCallback => destroyed before |
| 266 | + // it, so ~DtorCallback observes an already-destroyed variable. |
| 267 | + *kObservedInt = 42; |
| 268 | + }).Get(); |
| 269 | + |
| 270 | + EXPECT_TRUE(checked); |
| 271 | +} |
| 272 | + |
| 273 | +// Situation 3: GetOptional on a variable whose destructor is currently |
| 274 | +// running must return nullptr (POSIX pthread_getspecific-style: the variable |
| 275 | +// is unset before its destructor is invoked). The destructor itself can use |
| 276 | +// `this` if needed; other code must not observe a half-destroyed object. |
| 277 | +UTEST(TaskLocalVariable, GetOptionalDuringOwnDestruction) { |
| 278 | + bool checked = false; |
| 279 | + |
| 280 | + engine::AsyncNoTracing([&] { |
| 281 | + kDtorCallback->on_destroy = [&] { |
| 282 | + EXPECT_EQ(kDtorCallback.GetOptional(), nullptr); |
| 283 | + checked = true; |
| 284 | + }; |
| 285 | + }).Get(); |
| 286 | + |
| 287 | + EXPECT_TRUE(checked); |
| 288 | +} |
| 289 | + |
| 290 | +namespace { |
| 291 | + |
| 292 | +struct CallbackHolder final { |
| 293 | + std::function<void()> on_destroy; |
| 294 | + |
| 295 | + explicit CallbackHolder(std::function<void()> cb) |
| 296 | + : on_destroy(std::move(cb)) |
| 297 | + {} |
| 298 | + |
| 299 | + CallbackHolder(const CallbackHolder&) = delete; |
| 300 | + CallbackHolder& operator=(const CallbackHolder&) = delete; |
| 301 | + |
| 302 | + ~CallbackHolder() { |
| 303 | + if (on_destroy) { |
| 304 | + on_destroy(); |
| 305 | + } |
| 306 | + } |
| 307 | +}; |
| 308 | + |
| 309 | +engine::TaskInheritedVariable<CallbackHolder> kInheritedCallback; |
| 310 | + |
| 311 | +} // namespace |
| 312 | + |
| 313 | +// A task-local variable initialized from a destructor of another task-local |
| 314 | +// variable is destroyed after that destructor completes. Same as for |
| 315 | +// `thread_local`, [basic.stc.thread]/2: "If a variable with thread storage |
| 316 | +// duration is initialized after a thread-local destructor has started |
| 317 | +// executing, its destructor is scheduled to run after that destructor |
| 318 | +// completes". |
| 319 | +UTEST(TaskLocalVariable, DtorInitializesLocalVariable) { |
| 320 | + std::string order; |
| 321 | + |
| 322 | + engine::AsyncNoTracing([&] { |
| 323 | + kDtorCallback->on_destroy = [&] { |
| 324 | + kGuardX->emplace(order, "x"); |
| 325 | + order += "a"; |
| 326 | + }; |
| 327 | + }).Get(); |
| 328 | + |
| 329 | + // "a" (the initializing destructor completes) strictly before "x" |
| 330 | + EXPECT_EQ(order, "ax"); |
| 331 | +} |
| 332 | + |
| 333 | +// A task-inherited variable initialized from a destructor of a task-local |
| 334 | +// variable is destroyed after that destructor completes. |
| 335 | +UTEST(TaskLocalVariable, LocalDtorInitializesInheritedVariable) { |
| 336 | + std::string order; |
| 337 | + |
| 338 | + engine::AsyncNoTracing([&] { |
| 339 | + kDtorCallback->on_destroy = [&] { |
| 340 | + kInheritedCallback.Emplace([&order] { order += "i"; }); |
| 341 | + order += "a"; |
| 342 | + }; |
| 343 | + }).Get(); |
| 344 | + |
| 345 | + EXPECT_EQ(order, "ai"); |
| 346 | +} |
| 347 | + |
| 348 | +// A task-local variable initialized from a destructor of a task-inherited |
| 349 | +// variable is destroyed after that destructor completes. |
| 350 | +UTEST(TaskLocalVariable, InheritedDtorInitializesLocalVariable) { |
| 351 | + std::string order; |
| 352 | + |
| 353 | + engine::AsyncNoTracing([&] { |
| 354 | + kInheritedCallback.Emplace([&] { |
| 355 | + kGuardX->emplace(order, "x"); |
| 356 | + order += "a"; |
| 357 | + }); |
| 358 | + }).Get(); |
| 359 | + |
| 360 | + EXPECT_EQ(order, "ax"); |
| 361 | +} |
| 362 | + |
| 363 | +// A task-inherited variable initialized from a destructor of another |
| 364 | +// task-inherited variable is destroyed after that destructor completes. |
| 365 | +UTEST(TaskLocalVariable, InheritedDtorInitializesInheritedVariable) { |
| 366 | + std::string order; |
| 367 | + |
| 368 | + engine::AsyncNoTracing([&] { |
| 369 | + kInheritedCallback.Emplace([&] { |
| 370 | + kInheritedCallback.Emplace([&order] { order += "i"; }); |
| 371 | + order += "a"; |
| 372 | + }); |
| 373 | + }).Get(); |
| 374 | + |
| 375 | + EXPECT_EQ(order, "ai"); |
| 376 | +} |
| 377 | + |
| 378 | +// A child task inherits a variable on construction and keeps it alive even if: |
| 379 | +// - the parent erases the variable before the child starts, |
| 380 | +// - the child is cancelled before start (finishes as State::kCancelled). |
| 381 | +// The variable is destroyed only after the child task finishes. |
| 382 | +UTEST(TaskInheritedVariable, ErasedInParentWhileChildCancelledBeforeStart) { |
| 383 | + // With more than 1 worker thread the child task would start running |
| 384 | + // concurrently, racing with RequestCancel and Erase below. |
| 385 | + ASSERT_EQ(engine::current_task::GetTaskProcessor().GetWorkerCount(), 1); |
| 386 | + |
| 387 | + bool destroyed = false; |
| 388 | + kInheritedCallback.Emplace([&destroyed] { destroyed = true; }); |
| 389 | + |
| 390 | + // The child inherits the variable on construction... |
| 391 | + auto task = utils::Async("child", [] { FAIL() << "the task must not start"; }); |
| 392 | + // ...and is cancelled before it gets a chance to start. |
| 393 | + task.RequestCancel(); |
| 394 | + |
| 395 | + // Hide the variable from the parent. The child still holds a reference. |
| 396 | + kInheritedCallback.Erase(); |
| 397 | + EXPECT_FALSE(destroyed); |
| 398 | + |
| 399 | + task.Wait(); |
| 400 | + EXPECT_EQ(task.GetState(), engine::TaskBase::State::kCancelled); |
| 401 | + // The child dropped the last reference when it finished. |
| 402 | + EXPECT_TRUE(destroyed); |
| 403 | +} |
| 404 | + |
201 | 405 | USERVER_NAMESPACE_END |
0 commit comments