Mercurial > irccd
comparison tests/test-jsapi-timer.c @ 1076:99dccafade22
tests: resurrect test-jsapi-timer
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Jul 2021 21:07:41 +0200 |
parents | 371e1cc2c697 |
children | 6f85c4743494 |
comparison
equal
deleted
inserted
replaced
1075:69b90a8d4d35 | 1076:99dccafade22 |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 /* TODO: We need proper bot function to dispatch */ | 19 #include <poll.h> |
20 | |
21 #if 0 | |
22 | 20 |
23 #define GREATEST_USE_ABBREVS 0 | 21 #define GREATEST_USE_ABBREVS 0 |
24 #include <greatest.h> | 22 #include <greatest.h> |
25 | 23 |
26 // TODO: irccd/ | 24 #include <irccd/irccd.h> |
27 #include <config.h> | |
28 | |
29 #include <irccd/js-plugin.h> | 25 #include <irccd/js-plugin.h> |
30 #include <irccd/plugin.h> | 26 #include <irccd/plugin.h> |
31 | 27 |
32 static struct irc_plugin *plugin; | 28 static struct irc_plugin *plugin; |
33 static struct irc_js_plugin_data *data; | 29 static duk_context *ctx; |
34 | 30 |
35 static void | 31 static void |
36 setup(void *udata) | 32 setup(void *udata) |
37 { | 33 { |
38 (void)udata; | 34 (void)udata; |
39 | 35 |
40 plugin = irc_js_plugin_open(SOURCE "/data/timer.js"); | 36 plugin = js_plugin_open("timer", TOP "/tests/data/timer.js"); |
41 data = plugin->data; | 37 ctx = js_plugin_get_context(plugin); |
42 } | 38 } |
43 | 39 |
44 static void | 40 static void |
45 teardown(void *udata) | 41 teardown(void *udata) |
46 { | 42 { |
47 (void)udata; | 43 (void)udata; |
48 | 44 |
49 irc_plugin_finish(plugin); | 45 irc_plugin_finish(plugin); |
50 | 46 |
51 plugin = NULL; | 47 plugin = NULL; |
52 data = NULL; | 48 ctx = NULL; |
53 } | 49 } |
54 | 50 |
55 static void | 51 static void |
56 set_type(const char *name) | 52 set_type(const char *name) |
57 { | 53 { |
58 duk_get_global_string(data->ctx, "Irccd"); | 54 duk_get_global_string(ctx, "Irccd"); |
59 duk_get_prop_string(data->ctx, -1, "Timer"); | 55 duk_get_prop_string(ctx, -1, "Timer"); |
60 duk_get_prop_string(data->ctx, -1, name.c_str()); | 56 duk_get_prop_string(ctx, -1, name); |
61 duk_put_global_string(data->ctx, "type"); | 57 duk_put_global_string(ctx, "type"); |
62 duk_pop_n(data->ctx, 2); | 58 duk_pop_n(ctx, 2); |
63 | 59 |
64 plugin_->open(); | 60 irc_plugin_load(plugin); |
65 plugin_->handle_load(bot_); | |
66 } | 61 } |
67 | 62 |
68 BOOST_AUTO_TEST_CASE(single) | 63 GREATEST_TEST |
64 basics_single(void) | |
69 { | 65 { |
70 boost::timer::cpu_timer timer; | 66 time_t start = time(NULL); |
67 struct pollfd fd; | |
71 | 68 |
72 set_type("Single"); | 69 set_type("Single"); |
73 | 70 |
74 while (timer.elapsed().wall / 1000000LL < 3000) { | 71 while (difftime(time(NULL), start) < 3) { |
75 ctx_.reset(); | 72 irc_bot_prepare(&fd); |
76 ctx_.poll(); | 73 poll(&fd, 1, 1); |
74 irc_bot_flush(&fd); | |
77 } | 75 } |
78 | 76 |
79 BOOST_TEST(duk_get_global_string(data->ctx, "count")); | 77 GREATEST_ASSERT(duk_get_global_string(ctx, "count")); |
80 BOOST_TEST(duk_get_int(data->ctx, -1) == 1); | 78 GREATEST_ASSERT_EQ(duk_get_int(ctx, -1), 1); |
79 GREATEST_PASS(); | |
81 } | 80 } |
82 | 81 |
83 BOOST_AUTO_TEST_CASE(repeat) | 82 GREATEST_TEST |
83 basics_repeat(void) | |
84 { | 84 { |
85 boost::timer::cpu_timer timer; | 85 time_t start = time(NULL); |
86 struct pollfd fd; | |
86 | 87 |
87 set_type("Repeat"); | 88 set_type("Repeat"); |
88 | 89 |
89 while (timer.elapsed().wall / 1000000LL < 3000) { | 90 while (difftime(time(NULL), start) < 3) { |
90 ctx_.reset(); | 91 irc_bot_prepare(&fd); |
91 ctx_.poll(); | 92 poll(&fd, 1, 1); |
93 irc_bot_flush(&fd); | |
92 } | 94 } |
93 | 95 |
94 BOOST_TEST(duk_get_global_string(data->ctx, "count")); | 96 GREATEST_ASSERT(duk_get_global_string(ctx, "count")); |
95 BOOST_TEST(duk_get_int(data->ctx, -1) >= 5); | 97 GREATEST_ASSERT(duk_get_int(ctx, -1) >= 5); |
98 GREATEST_PASS(); | |
96 } | 99 } |
97 | 100 |
98 #endif | 101 GREATEST_SUITE(suite_basics) |
102 { | |
103 GREATEST_SET_SETUP_CB(setup, NULL); | |
104 GREATEST_SET_TEARDOWN_CB(teardown, NULL); | |
105 GREATEST_RUN_TEST(basics_single); | |
106 GREATEST_RUN_TEST(basics_repeat); | |
107 } | |
108 | |
109 GREATEST_MAIN_DEFS(); | |
99 | 110 |
100 int | 111 int |
101 main(void) | 112 main(int argc, char **argv) |
102 { | 113 { |
103 | 114 irc_bot_init(); |
115 | |
116 GREATEST_MAIN_BEGIN(); | |
117 GREATEST_RUN_SUITE(suite_basics); | |
118 GREATEST_MAIN_END(); | |
119 | |
120 return 0; | |
104 } | 121 } |