diff options
Diffstat (limited to 'unit_tests/init_and_shutdown.cpp')
-rw-r--r-- | unit_tests/init_and_shutdown.cpp | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/unit_tests/init_and_shutdown.cpp b/unit_tests/init_and_shutdown.cpp index 83c0350..78c472f 100644 --- a/unit_tests/init_and_shutdown.cpp +++ b/unit_tests/init_and_shutdown.cpp | |||
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | // ################################################################################################# | 4 | // ################################################################################################# |
5 | 5 | ||
6 | TEST_CASE("require 'lanes'") | 6 | TEST_CASE("lanes.require 'lanes'") |
7 | { | 7 | { |
8 | LuaState L{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; | 8 | LuaState L{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; |
9 | 9 | ||
@@ -668,7 +668,7 @@ TEST_CASE("lanes.finally") | |||
668 | { | 668 | { |
669 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | 669 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; |
670 | // we need Lanes to be up. Since we run several 'scripts', we store it as a global | 670 | // we need Lanes to be up. Since we run several 'scripts', we store it as a global |
671 | S.requireSuccess("lanes = require 'lanes'"); | 671 | S.requireSuccess("lanes = require 'lanes'.configure()"); |
672 | // we can set a function | 672 | // we can set a function |
673 | S.requireSuccess("lanes.finally(function() end)"); | 673 | S.requireSuccess("lanes.finally(function() end)"); |
674 | // we can clear it | 674 | // we can clear it |
@@ -689,7 +689,7 @@ TEST_CASE("lanes.finally") | |||
689 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; | 689 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
690 | 690 | ||
691 | // we need Lanes to be up. Since we run several 'scripts', we store it as a global | 691 | // we need Lanes to be up. Since we run several 'scripts', we store it as a global |
692 | S.requireSuccess("lanes = require 'lanes'"); | 692 | S.requireSuccess("lanes = require 'lanes'.configure()"); |
693 | // works because we have package.preload.fixture = luaopen_fixture | 693 | // works because we have package.preload.fixture = luaopen_fixture |
694 | S.requireSuccess("fixture = require 'fixture'"); | 694 | S.requireSuccess("fixture = require 'fixture'"); |
695 | // set our detectable finalizer | 695 | // set our detectable finalizer |
@@ -699,6 +699,38 @@ TEST_CASE("lanes.finally") | |||
699 | // the finalizer should be called | 699 | // the finalizer should be called |
700 | REQUIRE(S.finalizerWasCalled); | 700 | REQUIRE(S.finalizerWasCalled); |
701 | } | 701 | } |
702 | |||
703 | // --------------------------------------------------------------------------------------------- | ||
704 | |||
705 | SECTION("shutdown with an uncooperative lane") | ||
706 | { | ||
707 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; | ||
708 | S.requireSuccess("lanes = require 'lanes'.configure()"); | ||
709 | |||
710 | // prepare a callback for lanes.finally() | ||
711 | static bool _wasCalled{}; | ||
712 | static bool _allLanesTerminated{}; | ||
713 | auto _finallyCB{ +[](lua_State* const L_) { _wasCalled = true; _allLanesTerminated = lua_toboolean(L_, 1); return 0; } }; | ||
714 | lua_pushcfunction(S, _finallyCB); | ||
715 | lua_setglobal(S, "finallyCB"); | ||
716 | // start a lane that lasts a long time | ||
717 | std::string_view const _script{ | ||
718 | " lanes.finally(finallyCB)" | ||
719 | " g = lanes.gen('*'," | ||
720 | " {name = 'auto'}," | ||
721 | " function()" | ||
722 | " for i = 1,1e37 do end" // no cooperative cancellation checks here! | ||
723 | " end)" | ||
724 | " g()" | ||
725 | }; | ||
726 | S.requireSuccess(_script); | ||
727 | // close the state before the lane ends. | ||
728 | // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point | ||
729 | S.close(); | ||
730 | // the finally handler should have been called, and told all lanes are stopped | ||
731 | REQUIRE(_wasCalled); | ||
732 | REQUIRE(_allLanesTerminated); | ||
733 | } | ||
702 | } | 734 | } |
703 | 735 | ||
704 | // ################################################################################################# | 736 | // ################################################################################################# |
@@ -719,7 +751,7 @@ namespace | |||
719 | 751 | ||
720 | // ################################################################################################# | 752 | // ################################################################################################# |
721 | 753 | ||
722 | TEST_CASE("on_state_create setting") | 754 | TEST_CASE("lanes.on_state_create setting") |
723 | { | 755 | { |
724 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; | 756 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; |
725 | 757 | ||