diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2025-03-12 09:11:42 +0100 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2025-03-12 09:11:42 +0100 |
| commit | a2580b819c17fe2a65ffc2be1e2bc28d7b5470ec (patch) | |
| tree | 7617cba47a4b024295ca7a4d0377a01584b19878 /unit_tests | |
| parent | 3ce42e54e5d093c3fd6136f6a9bfc9398f328bce (diff) | |
| download | lanes-a2580b819c17fe2a65ffc2be1e2bc28d7b5470ec.tar.gz lanes-a2580b819c17fe2a65ffc2be1e2bc28d7b5470ec.tar.bz2 lanes-a2580b819c17fe2a65ffc2be1e2bc28d7b5470ec.zip | |
Unit tests for lanes.sleep
Diffstat (limited to 'unit_tests')
| -rw-r--r-- | unit_tests/lane_tests.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/unit_tests/lane_tests.cpp b/unit_tests/lane_tests.cpp index 0f66251..196b31d 100644 --- a/unit_tests/lane_tests.cpp +++ b/unit_tests/lane_tests.cpp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | // ################################################################################################# | 4 | // ################################################################################################# |
| 5 | // ################################################################################################# | 5 | // ################################################################################################# |
| 6 | |||
| 6 | TEST_CASE("lanes.nameof") | 7 | TEST_CASE("lanes.nameof") |
| 7 | { | 8 | { |
| 8 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | 9 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; |
| @@ -32,6 +33,70 @@ TEST_CASE("lanes.nameof") | |||
| 32 | // ################################################################################################# | 33 | // ################################################################################################# |
| 33 | // ################################################################################################# | 34 | // ################################################################################################# |
| 34 | 35 | ||
| 36 | TEST_CASE("lanes.sleep.argument validation") | ||
| 37 | { | ||
| 38 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | ||
| 39 | S.requireSuccess("lanes = require 'lanes'.configure()"); | ||
| 40 | |||
| 41 | // anything not a number is no good | ||
| 42 | S.requireFailure("lanes.sleep(true)"); | ||
| 43 | S.requireFailure("lanes.sleep({})"); | ||
| 44 | S.requireFailure("lanes.sleep('a string')"); | ||
| 45 | S.requireFailure("lanes.sleep(lanes.null)"); | ||
| 46 | S.requireFailure("lanes.sleep(print)"); | ||
| 47 | |||
| 48 | // negative durations are not supported | ||
| 49 | S.requireFailure("lanes.sleep(-1)"); | ||
| 50 | |||
| 51 | // no duration is supported (same as 0) | ||
| 52 | S.requireSuccess("lanes.sleep()"); | ||
| 53 | S.requireSuccess("lanes.sleep(0)"); | ||
| 54 | } | ||
| 55 | |||
| 56 | // ################################################################################################# | ||
| 57 | |||
| 58 | TEST_CASE("lanes.sleep.check durations") | ||
| 59 | { | ||
| 60 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | ||
| 61 | S.requireSuccess("lanes = require 'lanes'.configure()"); | ||
| 62 | |||
| 63 | // requesting to sleep some duration should result in sleeping for that duration | ||
| 64 | auto const _before{ std::chrono::steady_clock::now() }; | ||
| 65 | S.requireSuccess("lanes.sleep(1)"); | ||
| 66 | CHECK(std::chrono::steady_clock::now() - _before >= 1000ms); | ||
| 67 | CHECK(std::chrono::steady_clock::now() - _before < 1100ms); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | // ################################################################################################# | ||
| 72 | |||
| 73 | TEST_CASE("lanes.sleep.interactions with timers") | ||
| 74 | { | ||
| 75 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | ||
| 76 | S.requireSuccess("lanes = require 'lanes'.configure{with_timers = true}"); | ||
| 77 | |||
| 78 | std::string_view const _script{ | ||
| 79 | // start a 10Hz timer | ||
| 80 | " local l = lanes.linda()" | ||
| 81 | " lanes.timer(l, 'gluh', 0.1, 0.1)" | ||
| 82 | // launch a lane that is supposed to sleep forever | ||
| 83 | " local g = lanes.gen('*', lanes.sleep)" | ||
| 84 | " local h = g('indefinitely')" | ||
| 85 | // sleep 1 second (this uses the timer linda) | ||
| 86 | " lanes.sleep(1)" | ||
| 87 | // shutdown should be able to cancel the lane and stop it instantly | ||
| 88 | " return 'SUCCESS'" | ||
| 89 | }; | ||
| 90 | // running the script should take about 1 second | ||
| 91 | auto const _before{ std::chrono::steady_clock::now() }; | ||
| 92 | S.requireReturnedString(_script, "SUCCESS"); | ||
| 93 | CHECK(std::chrono::steady_clock::now() - _before >= 1000ms); | ||
| 94 | CHECK(std::chrono::steady_clock::now() - _before < 1100ms); | ||
| 95 | } | ||
| 96 | |||
| 97 | // ################################################################################################# | ||
| 98 | // ################################################################################################# | ||
| 99 | |||
| 35 | TEST_CASE("lanes.gen") | 100 | TEST_CASE("lanes.gen") |
| 36 | { | 101 | { |
| 37 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | 102 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; |
