changeset 562:75e3711c95f8

Tests: fix js-timer test and simplify it
author David Demelier <markand@malikania.fr>
date Sun, 26 Nov 2017 09:02:09 +0100
parents 957296d623e2
children 17891017c661
files libirccd-test/irccd/js_test.hpp tests/CMakeLists.txt tests/js-timer/main.cpp tests/js-timer/timer-pending.js tests/js-timer/timer-repeat.js tests/js-timer/timer-single.js tests/js-timer/timer.js
diffstat 7 files changed, 58 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd-test/irccd/js_test.hpp	Sun Nov 26 08:41:17 2017 +0100
+++ b/libirccd-test/irccd/js_test.hpp	Sun Nov 26 09:02:09 2017 +0100
@@ -26,6 +26,8 @@
 
 #include <boost/asio.hpp>
 
+#include <irccd/logger.hpp>
+
 #include <irccd/irccd.hpp>
 
 #include <irccd/js/js_plugin.hpp>
@@ -69,13 +71,12 @@
         : plugin_(new js_plugin("test", plugin_path))
         , server_(new journal_server(service_, "test"))
     {
+        log::set_logger(std::make_unique<log::silent_logger>());
+
         // Irccd is mandatory at the moment.
         add<irccd_jsapi>();
         add<Modules...>();
 
-        plugin_->open();
-        plugin_->on_load(irccd_);
-
         // Add some CMake variables.
         duk_push_string(plugin_->context(), CMAKE_BINARY_DIR);
         duk_put_global_string(plugin_->context(), "CMAKE_BINARY_DIR");
--- a/tests/CMakeLists.txt	Sun Nov 26 08:41:17 2017 +0100
+++ b/tests/CMakeLists.txt	Sun Nov 26 09:02:09 2017 +0100
@@ -68,7 +68,7 @@
         add_subdirectory(js-irccd)
         add_subdirectory(js-logger)
         add_subdirectory(js-system)
-        #add_subdirectory(js-timer)
+        add_subdirectory(js-timer)
         add_subdirectory(js-unicode)
         add_subdirectory(js-util)
         add_subdirectory(plugin-ask)
--- a/tests/js-timer/main.cpp	Sun Nov 26 08:41:17 2017 +0100
+++ b/tests/js-timer/main.cpp	Sun Nov 26 09:02:09 2017 +0100
@@ -20,46 +20,69 @@
 #include <boost/test/unit_test.hpp>
 #include <boost/timer/timer.hpp>
 
-#include <irccd/net_util.hpp>
-
 #include <irccd/js/plugin_jsapi.hpp>
 #include <irccd/js/timer_jsapi.hpp>
 
 #include <js_test.hpp>
 
+#include <irccd/js/logger_jsapi.hpp>
+
 namespace irccd {
 
-class fixture : public js_test<plugin_jsapi, timer_jsapi> {
+namespace {
+
+class js_timer_test : public js_test<plugin_jsapi, timer_jsapi> {
 public:
-    using js_test::js_test;
+    js_timer_test()
+        : js_test(CMAKE_CURRENT_SOURCE_DIR "/timer.js")
+    {
+    }
+
+    void set_type(const std::string& name)
+    {
+        duk_get_global_string(plugin_->context(), "Irccd");
+        duk_get_prop_string(plugin_->context(), -1, "Timer");
+        duk_get_prop_string(plugin_->context(), -1, name.c_str());
+        duk_put_global_string(plugin_->context(), "type");
+        duk_pop_n(plugin_->context(), 2);
+
+        plugin_->open();
+        plugin_->on_load(irccd_);
+    }
 };
 
-BOOST_FIXTURE_TEST_SUITE(timer_jsapi_suite, fixture)
+} // !namespace
+
+BOOST_FIXTURE_TEST_SUITE(js_timer_test_suite, js_timer_test)
 
 BOOST_AUTO_TEST_CASE(single)
 {
-    fixture f(CMAKE_CURRENT_SOURCE_DIR "/timer-single.js");
-
     boost::timer::cpu_timer timer;
 
-    while (timer.elapsed().wall / 1000000LL < 3000)
-        net_util::poll(512, f.irccd_);
+    set_type("Single");
 
-    BOOST_TEST(duk_get_global_string(f.plugin_->context(), "count"));
-    BOOST_TEST(duk_get_int(f.plugin_->context(), -1) == 1);
+    while (timer.elapsed().wall / 1000000LL < 3000) {
+        service_.reset();
+        service_.poll();
+    }
+
+    BOOST_TEST(duk_get_global_string(plugin_->context(), "count"));
+    BOOST_TEST(duk_get_int(plugin_->context(), -1) == 1);
 }
 
 BOOST_AUTO_TEST_CASE(repeat)
 {
-    fixture f(CMAKE_CURRENT_SOURCE_DIR "/timer-repeat.js");
-
     boost::timer::cpu_timer timer;
 
-    while (timer.elapsed().wall / 1000000LL < 3000)
-        net_util::poll(512, f.irccd_);
+    set_type("Repeat");
 
-    BOOST_TEST(duk_get_global_string(f.plugin_->context(), "count"));
-    BOOST_TEST(duk_get_int(f.plugin_->context(), -1) >= 5);
+    while (timer.elapsed().wall / 1000000LL < 3000) {
+        service_.reset();
+        service_.poll();
+    }
+
+    BOOST_TEST(duk_get_global_string(plugin_->context(), "count"));
+    BOOST_TEST(duk_get_int(plugin_->context(), -1) >= 5);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
--- a/tests/js-timer/timer-pending.js	Sun Nov 26 08:41:17 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-count = 0;
-
-function onLoad()
-{
-    t = new Irccd.Timer(Irccd.Timer.Repeat, 500, function () {
-        count += 1;
-    });
-
-    t.start();
-}
--- a/tests/js-timer/timer-repeat.js	Sun Nov 26 08:41:17 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-count = 0;
-
-function onLoad()
-{
-    t = new Irccd.Timer(Irccd.Timer.Repeat, 500, function () {
-        count += 1;
-    });
-
-    t.start();
-}
--- a/tests/js-timer/timer-single.js	Sun Nov 26 08:41:17 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-count = 0;
-
-function onLoad()
-{
-    t = new Irccd.Timer(Irccd.Timer.Single, 500, function () {
-        count += 1;
-    });
-
-    t.start();
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/js-timer/timer.js	Sun Nov 26 09:02:09 2017 +0100
@@ -0,0 +1,13 @@
+count = 0;
+
+function onLoad()
+{
+    if (typeof (type) === "undefined")
+        throw Error("global timer type not defined");
+
+    t = new Irccd.Timer(type, 50, function () {
+        count += 1;
+    });
+
+    t.start();
+}