Permalink
Browse files

cmake: add automated test.

Following work on luasocket issue#1.
  • Loading branch information...
davidm committed May 2, 2012
1 parent 540127e commit 8692d0a5aa34902e2a45b89a33432adf6d00af60
Showing with 101 additions and 5 deletions.
  1. +28 −5 CMakeLists.txt
  2. +73 −0 test/testing_thread.c
View
@@ -1,5 +1,5 @@
# Copyright (C) 2007-2012 LuaDist.
-# Created by Peter Drahoš
+# Created by Peter Drahoš, David Manura
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Please note that the package source code is licensed under its own license.
@@ -41,6 +41,7 @@ install_lua_module ( ltn12 src/ltn12.lua )
install_lua_module ( mime src/mime.lua )
install_lua_module ( socket src/socket.lua )
install_lua_module ( socket.ftp src/ftp.lua )
+install_lua_module ( socket.headers src/headers.lua )
install_lua_module ( socket.http src/http.lua )
install_lua_module ( socket.smtp src/smtp.lua )
install_lua_module ( socket.tp src/tp.lua )
@@ -52,7 +53,29 @@ install_example ( samples/ )
install_doc ( doc/ )
install_data ( README NEW LICENSE )
-#2DO: how to spawn testsvr.lua to run in parallel with testclnt.lua?
-#~ add_lua_test ( test/testclnt.lua )
-file ( WRITE t/t.lua " require 'socket' " ) # very basic test
-add_lua_test ( t/t.lua )
+# very basic test
+file ( WRITE ${CMAKE_CURRENT_BINARY_DIR}/t/t.lua " require 'socket' " )
+add_lua_test ( ${CMAKE_CURRENT_BINARY_DIR}/t/t.lua )
+
+# test with client-server.
+# This is more complicated because the test suite client and server must be
+# run in different threads (or different processes).
+find_package ( Threads )
+if ( Threads_FOUND )
+ add_lua_module ( testing_thread test/testing_thread.c LINK ${CMAKE_THREAD_LIBS_INIT} )
+ file ( WRITE ${CMAKE_CURRENT_BINARY_DIR}/t/clientserver.lua "
+ local socket = require 'socket'
+ function sleep(sec) socket.select(nil, nil, sec) end
+ print('RUN_THREAD=', RUN_THREAD)
+ if RUN_THREAD then
+ dofile '${CMAKE_CURRENT_SOURCE_DIR}/test/testsrvr.lua'
+ else
+ require 'testing_thread' '${CMAKE_CURRENT_BINARY_DIR}/clientserver.lua'
+ sleep(2) -- allow some time for server to start
+ dofile '${CMAKE_CURRENT_SOURCE_DIR}/test/testclnt.lua'
+ end
+ " )
+ add_lua_test ( ${CMAKE_CURRENT_BINARY_DIR}/t/clientserver.lua )
+else ()
+ message ( WARNING "skipping tests (no threading)" )
+endif ()
View
@@ -0,0 +1,73 @@
+/**
+ Lua C module used only for test suite.
+ This allows the client and server to run under different threads.
+
+ Copyright (C) 2007-2012 LuaDist.
+ Created by David Manura.
+ Redistribution and use of this file is allowed according to the terms of the MIT license.
+ For details see the COPYRIGHT file distributed with LuaDist.
+ Please note that the package source code is licensed under its own license.
+*/
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <pthread.h>
+#endif
+
+static char path[1000];
+
+#ifdef _WIN32
+DWORD WINAPI thread_func(void * p)
+#else
+void * thread_func(void * p)
+#endif
+{
+ lua_State * L = luaL_newstate();
+ luaL_openlibs(L);
+ lua_pushboolean(L, 1);
+ lua_setglobal(L, "RUN_THREAD");
+ lua_newtable(L);
+ lua_setglobal(L, "arg");
+ if (luaL_loadfile(L, path) != 0)
+ lua_error(L);
+ lua_call(L, 0,0);
+#ifdef _WIN32
+ return 0;
+#else
+ return NULL;
+#endif
+}
+
+int lrun(lua_State * L)
+{
+ #ifdef _WIN32
+ HANDLE hthread;
+ #else
+ int iret;
+ #endif
+
+ strcpy(path, lua_tostring(L, 1));
+
+ #ifdef _WIN32
+ hthread = CreateThread(NULL, 0, thread_func, NULL, 0, NULL);
+ if (hthread == NULL) exit(1);
+ #else
+ pthread_t thread1;
+ iret = pthread_create( &thread1, NULL, thread_func, (void*)NULL);
+ if (iret != 0) exit(1);
+ #endif
+
+ return 0;
+}
+
+int luaopen_testing_thread(lua_State * L)
+{
+ lua_pushcfunction(L, lrun);
+ return 1;
+}

0 comments on commit 8692d0a

Please sign in to comment.