public void CleanDeadCoroutines() { using (var lua = new NLua.Lua()) { lua.LoadCoroutineManager(); lua.DoString("function test() return 1 end " + "function test2() coroutine.yield(1) return 2 end"); lua.CreateCoroutine("test"); lua.CreateCoroutine("test2"); lua.UpdateCoroutines(); lua.CleanDeadCoroutines(); Assert.IsTrue(!lua.ExistsCoroutine("test")); Assert.IsTrue(lua.ExistsCoroutine("test2")); lua.UpdateCoroutines(); lua.CleanDeadCoroutines(); Assert.IsTrue(!lua.ExistsCoroutine("test")); Assert.IsTrue(!lua.ExistsCoroutine("test2")); } }
public void UpdateCoroutines() { using (var lua = new NLua.Lua()) { lua.LoadCLRPackage(); lua.LoadCoroutineManager(); lua.DoString("function test() return 'hi' end " + "function test2() coroutine.yield() return true end " + "function test3() return 3,4 end"); lua.CreateCoroutine("test"); lua.CreateCoroutine("test2"); lua.CreateCoroutine("test3"); Dictionary<string, LuaCoroutineResult> results = lua.UpdateCoroutines(); Assert.IsTrue(results["test"].IsValid); Assert.IsTrue((string)results["test"].Results[0] == "hi"); Assert.IsTrue(lua.StatusCoroutine("test") == LuaCoroutineStatus.Dead); Assert.IsTrue(results["test2"].IsValid); Assert.IsTrue(lua.StatusCoroutine("test2") == LuaCoroutineStatus.Suspended); Assert.IsTrue(results["test3"].IsValid); Assert.IsTrue((int)(double)results["test3"].Results[0] == 3); Assert.IsTrue((int)(double)results["test3"].Results[1] == 4); Assert.IsTrue(lua.StatusCoroutine("test3") == LuaCoroutineStatus.Dead); results = lua.UpdateCoroutines(); Assert.IsFalse(results["test"].IsValid); Assert.IsTrue(lua.StatusCoroutine("test") == LuaCoroutineStatus.Dead); Assert.IsTrue(results["test2"].IsValid); Assert.IsTrue((bool)results["test2"].Results[0]); Assert.IsTrue(lua.StatusCoroutine("test2") == LuaCoroutineStatus.Dead); Assert.IsFalse(results["test3"].IsValid); Assert.IsTrue(lua.StatusCoroutine("test3") == LuaCoroutineStatus.Dead); } }
public void SleepCoroutine() { using (var lua = new NLua.Lua()) { lua.LoadCLRPackage(); lua.LoadCoroutineManager(); lua.DoString("function test() coroutine.manager.sleep(10, 'stopped 1') return 'hi' end " + "function test2() coroutine.manager.sleep(20, 'stopped 2') return 'hello' end "); lua.CreateCoroutine("test"); lua.CreateCoroutine("test2"); Dictionary<string, LuaCoroutineResult> results = lua.UpdateCoroutines(); Assert.IsTrue(results["test"].IsValid); Assert.IsTrue((string)results["test"].Results[0] == "stopped 1"); Assert.IsTrue(lua.StatusCoroutine("test") == LuaCoroutineStatus.Sleeping); Assert.IsTrue(results["test2"].IsValid); Assert.IsTrue((string)results["test2"].Results[0] == "stopped 2"); Assert.IsTrue(lua.StatusCoroutine("test2") == LuaCoroutineStatus.Sleeping); results = lua.UpdateCoroutines(10); Assert.IsTrue(results["test"].IsValid); Assert.IsTrue((string)results["test"].Results[0] == "hi"); Assert.IsTrue(lua.StatusCoroutine("test") == LuaCoroutineStatus.Dead); Assert.IsTrue(results["test2"].IsValid); Assert.IsTrue(lua.StatusCoroutine("test2") == LuaCoroutineStatus.Sleeping); results = lua.UpdateCoroutines(10); Assert.IsFalse(results["test"].IsValid); Assert.IsTrue(lua.StatusCoroutine("test") == LuaCoroutineStatus.Dead); Assert.IsTrue(results["test2"].IsValid); Assert.IsTrue((string)results["test2"].Results[0] == "hello"); Assert.IsTrue(lua.StatusCoroutine("test2") == LuaCoroutineStatus.Dead); } }