aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-12-16 09:50:52 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2024-12-16 09:50:52 +0100
commitefd5318b2d9132996bf35a6af2e82706665890ff (patch)
treed92946126d43258b86a67c655353dba22f8982e0
parent0740a54fe0f4d4264875ae1dc3334e33e27f4ff9 (diff)
downloadlanes-efd5318b2d9132996bf35a6af2e82706665890ff.tar.gz
lanes-efd5318b2d9132996bf35a6af2e82706665890ff.tar.bz2
lanes-efd5318b2d9132996bf35a6af2e82706665890ff.zip
Unit tests for thread name
-rw-r--r--src/lane.cpp12
-rw-r--r--unit_tests/lane_tests.cpp61
2 files changed, 62 insertions, 11 deletions
diff --git a/src/lane.cpp b/src/lane.cpp
index 63efdb6..7664dd6 100644
--- a/src/lane.cpp
+++ b/src/lane.cpp
@@ -88,12 +88,12 @@ static LUAG_FUNC(lane_threadname)
88 Lane* const _lane{ luaG_tolightuserdata<Lane>(L_, StackIndex{ lua_upvalueindex(1) }) }; 88 Lane* const _lane{ luaG_tolightuserdata<Lane>(L_, StackIndex{ lua_upvalueindex(1) }) };
89 LUA_ASSERT(L_, L_ == _lane->L); // this function is exported in a lane's state, therefore it is callable only from inside the Lane's state 89 LUA_ASSERT(L_, L_ == _lane->L); // this function is exported in a lane's state, therefore it is callable only from inside the Lane's state
90 if (lua_gettop(L_) == 1) { 90 if (lua_gettop(L_) == 1) {
91 lua_settop(L_, 1); 91 lua_settop(L_, 1);
92 STACK_CHECK_START_REL(L_, 0); 92 STACK_CHECK_START_REL(L_, 0);
93 _lane->storeDebugName(luaG_tostring(L_, kIdxTop)); 93 _lane->storeDebugName(luaG_tostring(L_, kIdxTop));
94 _lane->applyDebugName(); 94 _lane->applyDebugName();
95 STACK_CHECK(L_, 0); 95 STACK_CHECK(L_, 0);
96 return 0; 96 return 0;
97 } else if (lua_gettop(L_) == 0) { 97 } else if (lua_gettop(L_) == 0) {
98 luaG_pushstring(L_, _lane->getDebugName()); 98 luaG_pushstring(L_, _lane->getDebugName());
99 return 1; 99 return 1;
diff --git a/unit_tests/lane_tests.cpp b/unit_tests/lane_tests.cpp
index 14a327f..8b22fcd 100644
--- a/unit_tests/lane_tests.cpp
+++ b/unit_tests/lane_tests.cpp
@@ -104,17 +104,16 @@ TEST_F(LaneTests, UncooperativeShutdown)
104 lua_pushcfunction(S, _finallyCB); 104 lua_pushcfunction(S, _finallyCB);
105 lua_setglobal(S, "finallyCB"); 105 lua_setglobal(S, "finallyCB");
106 // start a lane that lasts a long time 106 // start a lane that lasts a long time
107 std::string_view const script{ 107 std::string_view const _script{
108 " lanes.finally(finallyCB)" 108 " lanes.finally(finallyCB)"
109 " print ('in Master')" 109 " g = lanes.gen('*',"
110 " f = lanes.gen('*',"
111 " {name = 'auto'}," 110 " {name = 'auto'},"
112 " function()" 111 " function()"
113 " for i = 1,1e37 do end" // no cooperative cancellation checks here! 112 " for i = 1,1e37 do end" // no cooperative cancellation checks here!
114 " end)" 113 " end)"
115 " f()" 114 " g()"
116 }; 115 };
117 ASSERT_EQ(S.doString(script), LuaError::OK) << S; 116 ASSERT_EQ(S.doString(_script), LuaError::OK) << S;
118 // close the state before the lane ends. 117 // close the state before the lane ends.
119 // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point 118 // since we don't wait at all, it is possible that the OS thread for the lane hasn't even started at that point
120 S.close(); 119 S.close();
@@ -124,6 +123,58 @@ TEST_F(LaneTests, UncooperativeShutdown)
124} 123}
125 124
126// ################################################################################################# 125// #################################################################################################
126
127TEST_F(LaneTests, DefaultThreadNameIsUnnamed)
128{
129 std::string_view const _script{
130 " g = lanes.gen('*',"
131 " function()"
132 " return lane_threadname()"
133 " end)"
134 " h = g()"
135 " local tn = h[1]"
136 " assert(tn == h:get_threadname())"
137 " return tn"
138 };
139 ASSERT_EQ(S.doStringAndRet(_script), "<unnamed>") << S;
140}
141
142// #################################################################################################
143
144TEST_F(LaneTests, UserThreadNameFromGenerator)
145{
146 std::string_view const _script{
147 " g = lanes.gen('*',"
148 " { name = 'user name'},"
149 " function()"
150 " return lane_threadname()"
151 " end)"
152 " h = g()"
153 " local tn = h[1]"
154 " assert(tn == h:get_threadname())"
155 " return tn"
156 };
157 ASSERT_EQ(S.doStringAndRet(_script), "user name") << S;
158}
159
160// #################################################################################################
161
162TEST_F(LaneTests, UserThreadNameFromInside)
163{
164 std::string_view const _script{
165 " g = lanes.gen('*',"
166 " function()"
167 " lane_threadname('user name')"
168 " return true"
169 " end)"
170 " h = g()"
171 " h:join()"
172 " return h:get_threadname()"
173 };
174 ASSERT_EQ(S.doStringAndRet(_script), "user name") << S;
175}
176
177// #################################################################################################
127// ################################################################################################# 178// #################################################################################################
128 179
129class LaneCancel : public ::testing::Test 180class LaneCancel : public ::testing::Test