[Tarantool-patches] [PATCH 09/10] test: factor out swim from fakeev.h/.c files

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue Dec 1 02:56:18 MSK 2020


SWIM unit tests contain a special library for emulating the event
loop: swim_test_ev. It provides API similar to libev, but
implemented entirely in user-space, including clock functions.

The latter is the most important point, as the original libev
does not allow to define your own timing functions - internally it
relies on select/kqueue/epoll/poll/select/... with true clock.

Because of that it is impossible to perform long tests with the
original libev, which could last for minutes or even tens of
seconds if their count is big. swim_test_ev uses virtual time,
where hours can be played in milliseconds.

--

This commit extracts all swim code to swim_test_ev.c. Now this
file is nothing but an implementation of swim_ev.h on top of
fakeev API.

Fakeev, in turn, does not depend on SWIM anymore, and can be moved
to fakesys library.

Part of #5303
---
 test/unit/CMakeLists.txt |  6 ++--
 test/unit/fakeev.c       | 35 -----------------------
 test/unit/fakeev.h       | 13 +++++++++
 test/unit/swim_test_ev.c | 62 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 78 insertions(+), 38 deletions(-)
 create mode 100644 test/unit/swim_test_ev.c

diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt
index 436178280..7f64f9685 100644
--- a/test/unit/CMakeLists.txt
+++ b/test/unit/CMakeLists.txt
@@ -240,16 +240,16 @@ target_link_libraries(sio.test unit core)
 add_executable(crypto.test crypto.c core_test_utils.c)
 target_link_libraries(crypto.test crypto unit)
 
-add_executable(swim.test swim.c swim_test_transport.c fakeev.c
+add_executable(swim.test swim.c swim_test_transport.c fakeev.c swim_test_ev.c
                swim_test_utils.c ${PROJECT_SOURCE_DIR}/src/version.c core_test_utils.c)
 target_link_libraries(swim.test unit fakesys swim)
 
-add_executable(swim_proto.test swim_proto.c swim_test_transport.c fakeev.c
+add_executable(swim_proto.test swim_proto.c swim_test_transport.c fakeev.c swim_test_ev.c
                swim_test_utils.c ${PROJECT_SOURCE_DIR}/src/version.c core_test_utils.c)
 target_link_libraries(swim_proto.test unit fakesys swim)
 
 add_executable(swim_errinj.test swim_errinj.c swim_test_transport.c
-               fakeev.c swim_test_utils.c
+               fakeev.c swim_test_ev.c swim_test_utils.c
                ${PROJECT_SOURCE_DIR}/src/version.c core_test_utils.c)
 target_link_libraries(swim_errinj.test unit fakesys swim)
 
diff --git a/test/unit/fakeev.c b/test/unit/fakeev.c
index 20de35d7f..20fab916b 100644
--- a/test/unit/fakeev.c
+++ b/test/unit/fakeev.c
@@ -30,7 +30,6 @@
  */
 #include "fakeev.h"
 #include "trivia/util.h"
-#include "swim/swim_ev.h"
 #include "tarantool_ev.h"
 #define HEAP_FORWARD_DECLARATION
 #include "salad/heap.h"
@@ -269,10 +268,6 @@ fakeev_time(void)
 	return watch;
 }
 
-/**
- * Start of a timer generates a delayed event. If a timer is
- * already started - nothing happens.
- */
 void
 fakeev_timer_start(struct ev_loop *loop, struct ev_timer *base)
 {
@@ -353,33 +348,3 @@ fakeev_free(void)
 	mh_i64ptr_delete(events_hash);
 	ev_loop_destroy(test_loop);
 }
-
-double
-swim_time(void)
-{
-	return fakeev_time();
-}
-
-void
-swim_ev_timer_start(struct ev_loop *loop, struct ev_timer *watcher)
-{
-	return fakeev_timer_start(loop, watcher);
-}
-
-void
-swim_ev_timer_again(struct ev_loop *loop, struct ev_timer *watcher)
-{
-	return fakeev_timer_again(loop, watcher);
-}
-
-void
-swim_ev_timer_stop(struct ev_loop *loop, struct ev_timer *watcher)
-{
-	return fakeev_timer_stop(loop, watcher);
-}
-
-struct ev_loop *
-swim_loop(void)
-{
-	return fakeev_loop();
-}
diff --git a/test/unit/fakeev.h b/test/unit/fakeev.h
index b50fe5b3d..89954b2e1 100644
--- a/test/unit/fakeev.h
+++ b/test/unit/fakeev.h
@@ -30,6 +30,7 @@
  * SUCH DAMAGE.
  */
 struct ev_loop;
+struct ev_timer;
 
 /**
  * Fakeev implements a 'fake' event loop with bogus clock to speed up events
@@ -94,6 +95,18 @@ fakeev_time(void);
 struct ev_loop *
 fakeev_loop(void);
 
+/** Emulator of ev_timer_start(). */
+void
+fakeev_timer_start(struct ev_loop *loop, struct ev_timer *base);
+
+/** Emulator of ev_timer_again(). */
+void
+fakeev_timer_again(struct ev_loop *loop, struct ev_timer *base);
+
+/** Emulator of ev_timer_stop(). */
+void
+fakeev_timer_stop(struct ev_loop *loop, struct ev_timer *base);
+
 /**
  * Stop the event loop after @a delay fake seconds. It does not
  * affect other events, so the loop can stop earlier multiple
diff --git a/test/unit/swim_test_ev.c b/test/unit/swim_test_ev.c
new file mode 100644
index 000000000..876aa4eea
--- /dev/null
+++ b/test/unit/swim_test_ev.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2010-2020, Tarantool AUTHORS, please see AUTHORS file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ *    copyright notice, this list of conditions and the
+ *    following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials
+ *    provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include "swim/swim_ev.h"
+#include "fakeev.h"
+
+double
+swim_time(void)
+{
+	return fakeev_time();
+}
+
+void
+swim_ev_timer_start(struct ev_loop *loop, struct ev_timer *watcher)
+{
+	return fakeev_timer_start(loop, watcher);
+}
+
+void
+swim_ev_timer_again(struct ev_loop *loop, struct ev_timer *watcher)
+{
+	return fakeev_timer_again(loop, watcher);
+}
+
+void
+swim_ev_timer_stop(struct ev_loop *loop, struct ev_timer *watcher)
+{
+	return fakeev_timer_stop(loop, watcher);
+}
+
+struct ev_loop *
+swim_loop(void)
+{
+	return fakeev_loop();
+}
-- 
2.24.3 (Apple Git-128)



More information about the Tarantool-patches mailing list