Forráskód Böngészése

Add unit test for timers

bel 8 éve
szülő
commit
7ec97f166a

+ 2 - 0
VisualStudio/unit_test/unit_test.vcxproj

@@ -17,6 +17,7 @@
     <ClInclude Include="..\..\test\private_exe.h" />
     <ClInclude Include="..\..\test\public_func.h" />
     <ClInclude Include="..\..\test\public_server.h" />
+    <ClInclude Include="..\..\test\timertest.h" />
     <ClInclude Include="..\..\test\shared.h" />
   </ItemGroup>
   <ItemGroup>
@@ -25,6 +26,7 @@
     <ClCompile Include="..\..\test\private_exe.c" />
     <ClCompile Include="..\..\test\public_func.c" />
     <ClCompile Include="..\..\test\public_server.c" />
+    <ClInclude Include="..\..\test\timertest.c" />
     <ClCompile Include="..\..\test\shared.c" />
   </ItemGroup>
   <PropertyGroup Label="Globals">

+ 6 - 0
VisualStudio/unit_test/unit_test.vcxproj.filters

@@ -30,6 +30,9 @@
     <ClInclude Include="..\..\test\private.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\test\timertest.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\test\shared.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
@@ -50,6 +53,9 @@
     <ClCompile Include="..\..\test\private_exe.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\test\timertest.c">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>    
     <ClCompile Include="..\..\test\shared.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>

+ 2 - 0
format.bat

@@ -25,6 +25,8 @@ clang-format -i test/private_exe.h
 clang-format -i test/private_exe.c
 clang-format -i test/shared.h
 clang-format -i test/shared.c
+clang-format -i test/timertest.h
+clang-format -i test/timertest.c
 clang-format -i test/civetweb_check.h
 clang-format -i test/main.c
 

+ 6 - 1
src/timer.inl

@@ -19,6 +19,7 @@ struct ttimers {
 	unsigned timer_count;             /* Current size of timer list */
 };
 
+
 static int
 timer_add(struct mg_context *ctx,
           double next_time,
@@ -47,7 +48,7 @@ timer_add(struct mg_context *ctx,
 	 *        n times until (next_time + (n * period)) > now
 	 *        then the period is working
 	 * Solution:
-	 *        if next_time < now then we set  next_time = now.
+	 *        if next_time < now then we set next_time = now.
 	 *        The first callback will be so fast as possible  (now)
 	 *        but the next callback on period
 	*/
@@ -80,6 +81,7 @@ timer_add(struct mg_context *ctx,
 	return error;
 }
 
+
 static void
 timer_thread_run(void *thread_func_param)
 {
@@ -130,6 +132,7 @@ timer_thread_run(void *thread_func_param)
 #endif
 }
 
+
 #ifdef _WIN32
 static unsigned __stdcall timer_thread(void *thread_func_param)
 {
@@ -145,6 +148,7 @@ timer_thread(void *thread_func_param)
 }
 #endif /* _WIN32 */
 
+
 static int
 timers_init(struct mg_context *ctx)
 {
@@ -157,6 +161,7 @@ timers_init(struct mg_context *ctx)
 	return 0;
 }
 
+
 static void
 timers_exit(struct mg_context *ctx)
 {

+ 11 - 0
test/CMakeLists.txt

@@ -91,6 +91,13 @@ target_include_directories(
 target_link_libraries(private-c-unit-tests ${CHECK_LIBRARIES})
 add_dependencies(private-c-unit-tests check-unit-test-framework)
 
+add_library(timer-c-unit-tests STATIC timertest.c)
+target_include_directories(
+  timer-c-unit-tests PUBLIC
+  ${PROJECT_SOURCE_DIR}/include)
+target_link_libraries(timer-c-unit-tests ${CHECK_LIBRARIES})
+add_dependencies(timer-c-unit-tests check-unit-test-framework)
+
 add_library(exe-c-unit-tests STATIC private_exe.c)
 if (BUILD_SHARED_LIBS)
   target_compile_definitions(exe-c-unit-tests PRIVATE)
@@ -107,6 +114,7 @@ target_link_libraries(main-c-unit-test
   public-func-c-unit-tests
   public-server-c-unit-tests
   private-c-unit-tests
+  timer-c-unit-tests
   exe-c-unit-tests
   ${CHECK_LIBRARIES})
 add_dependencies(main-c-unit-test check-unit-test-framework)
@@ -162,6 +170,9 @@ civetweb_add_test(PublicServer "Handle Form")
 civetweb_add_test(PublicServer "HTTP Authentication")
 civetweb_add_test(PublicServer "HTTP Keep Alive")
 
+# Timer tests
+civetweb_add_test(Timer "Timer1")
+
 # Tests with main.c
 #civetweb_add_test(EXE "Helper funcs")
 

+ 3 - 1
test/main.c

@@ -1,4 +1,4 @@
-/* Copyright (c) 2015 the Civetweb developers
+/* Copyright (c) 2015-2016 the Civetweb developers
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -24,6 +24,7 @@
 #include "public_func.h"
 #include "public_server.h"
 #include "private.h"
+#include "timertest.h"
 #include "private_exe.h"
 
 #include <stdlib.h>
@@ -78,6 +79,7 @@ main(const int argc, char *argv[])
 	srunner_add_suite(srunner, make_public_server_suite());
 	srunner_add_suite(srunner, make_private_suite());
 	srunner_add_suite(srunner, make_private_exe_suite());
+	srunner_add_suite(srunner, make_timertest_suite());
 
 	/* Write test logs to a file */
 	srunner_set_log(srunner, "test.log");

+ 1 - 1
test/private.c

@@ -810,7 +810,7 @@ MAIN_PRIVATE(void)
 	test_sha1(0);
 
 #if defined(_WIN32)
-    WSACleanup();
+	WSACleanup();
 #endif
 }
 

+ 114 - 0
test/timertest.c

@@ -0,0 +1,114 @@
+/* Copyright (c) 2016 the Civetweb developers
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * We include the source file so that we have access to the internal private
+ * static functions
+ */
+#ifdef _MSC_VER
+#ifndef _CRT_SECURE_NO_WARNINGS
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#define CIVETWEB_API static
+#endif
+
+
+#define USE_TIMERS
+#include "../src/civetweb.c"
+
+#include "timertest.h"
+
+
+int
+action(void *arg)
+{
+	int *p = (int *)arg;
+	(*p)--;
+	return 1;
+}
+
+
+START_TEST(test_timer1)
+{
+	struct mg_context ctx;
+	int c[10];
+	memset(&ctx, 0, sizeof(ctx));
+	memset(c, 0, sizeof(c));
+
+	mark_point();
+	timers_init(&ctx);
+	mg_sleep(100);
+	mark_point();
+
+	c[0] = 10;
+	timer_add(&ctx, 0, 0.1, 1, action, c + 0);
+	c[2] = 2;
+	timer_add(&ctx, 0, 0.5, 1, action, c + 2);
+	c[1] = 5;
+	timer_add(&ctx, 0, 0.2, 1, action, c + 1);
+
+	mg_sleep(1000);
+
+	ctx.stop_flag = 99;
+	mg_sleep(100);
+	timers_exit(&ctx);
+
+	ck_assert_int_eq(c[0], 0);
+	ck_assert_int_eq(c[1], 0);
+	ck_assert_int_eq(c[2], 0);
+}
+END_TEST
+
+
+Suite *
+make_timertest_suite(void)
+{
+	Suite *const suite = suite_create("Timer");
+
+	TCase *const tcase_timer1 = tcase_create("Timer1");
+
+	tcase_add_test(tcase_timer1, test_timer1);
+	tcase_set_timeout(tcase_timer1, civetweb_min_test_timeout);
+	suite_add_tcase(suite, tcase_timer1);
+
+	return suite;
+}
+
+
+#ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
+/* Used to debug test cases without using the check framework */
+
+void
+TIMER_PRIVATE(void)
+{
+#if defined(_WIN32)
+	WSADATA data;
+	WSAStartup(MAKEWORD(2, 2), &data);
+#endif
+
+	test_timer1(0);
+
+#if defined(_WIN32)
+	WSACleanup();
+#endif
+}
+
+#endif

+ 28 - 0
test/timertest.h

@@ -0,0 +1,28 @@
+/* Copyright (c) 2015 the Civetweb developers
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef TEST_TIMER_H_
+#define TEST_TIMER_H_
+
+#include "civetweb_check.h"
+
+Suite *make_timertest_suite(void);
+
+#endif /* TEST_TIMER_H_ */