diff options
author | Stefan Radomski <github@mintwerk.de> | 2017-01-13 16:47:44 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2017-01-13 16:47:44 (GMT) |
commit | 4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235 (patch) | |
tree | 8c023473bb342780ddf51a893d18369f1319bb5c /test | |
parent | 0aa0fe08dc308c94379c47d0bf9745e341cb4c81 (diff) | |
download | uscxml-4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235.zip uscxml-4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235.tar.gz uscxml-4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235.tar.bz2 |
First support for serialization and some bug fixes for DOM per data.src
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | test/src/test-extensions.cpp | 99 | ||||
-rw-r--r-- | test/src/test-serialization.cpp | 147 | ||||
-rw-r--r-- | test/src/test-url.cpp | 17 |
4 files changed, 178 insertions, 88 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 874ed16..658d93c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -42,11 +42,12 @@ function(USCXML_TEST_COMPILE) endfunction() # simple one file tests -USCXML_TEST_COMPILE(NAME test-extensions LABEL general/test-extensions FILES src/test-extensions.cpp) +USCXML_TEST_COMPILE(NAME test-extensions LABEL general/test-extensions FILES src/test-extensions.cpp ../contrib/src/uscxml/PausableDelayedEventQueue.cpp) USCXML_TEST_COMPILE(NAME test-url LABEL general/test-url FILES src/test-url.cpp) USCXML_TEST_COMPILE(NAME test-lifecycle LABEL general/test-lifecycle FILES src/test-lifecycle.cpp) USCXML_TEST_COMPILE(NAME test-validating LABEL general/test-validating FILES src/test-validating.cpp) USCXML_TEST_COMPILE(NAME test-snippets LABEL general/test-snippets FILES src/test-snippets.cpp) +USCXML_TEST_COMPILE(NAME test-serialization LABEL general/test-serialization FILES src/test-serialization.cpp ../contrib/src/uscxml/PausableDelayedEventQueue.cpp) # USCXML_TEST_COMPILE(NAME test-c89-parser LABEL general/test-c89-parser FILES src/test-c89-parser.cpp) # test-stress is not an automated test diff --git a/test/src/test-extensions.cpp b/test/src/test-extensions.cpp index e3dfbb6..7686f9f 100644 --- a/test/src/test-extensions.cpp +++ b/test/src/test-extensions.cpp @@ -3,6 +3,7 @@ #include "uscxml/interpreter/InterpreterImpl.h" #include "uscxml/interpreter/BasicEventQueue.h" #include "uscxml/interpreter/BasicDelayedEventQueue.h" +#include "uscxml/PausableDelayedEventQueue.h" #include <event2/util.h> // for evutil_socket_t @@ -10,6 +11,21 @@ #include <mutex> using namespace uscxml; +class MyPausableDelayedEventQueue; + +std::shared_ptr<MyPausableDelayedEventQueue> nestedDelayQueue; + +class MyPausableDelayedEventQueue : public PausableDelayedEventQueue { + MyPausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks) : PausableDelayedEventQueue(callbacks) { + } + + std::shared_ptr<DelayedEventQueueImpl> create(DelayedEventQueueCallbacks* callbacks) { + // remember as nestedDelayQueue in global scope + nestedDelayQueue = std::shared_ptr<MyPausableDelayedEventQueue>(new MyPausableDelayedEventQueue(callbacks)); + return nestedDelayQueue; + } +}; + // from issue 96: // https://github.com/tklab-tud/uscxml/issues/96 @@ -45,89 +61,6 @@ static const char *customDelayedEQ = " <final id=\"FinalShape1\"/>" "</scxml>"; -class PausableDelayedEventQueue; -std::shared_ptr<PausableDelayedEventQueue> nestedDelayQueue; - -/** - * A DelayedEventQueue that implements pause/resume - */ -class PausableDelayedEventQueue : public BasicDelayedEventQueue { -public: - PausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks) : BasicDelayedEventQueue(callbacks) { - _pausedAt.tv_sec = 0; - _pausedAt.tv_usec = 0; - } - - std::shared_ptr<DelayedEventQueueImpl> create(DelayedEventQueueCallbacks* callbacks) { - // remember as nestedDelayQueue in global scope - nestedDelayQueue = std::shared_ptr<PausableDelayedEventQueue>(new PausableDelayedEventQueue(callbacks)); - return nestedDelayQueue; - } - - void pause() { - if(_pausedAt.tv_sec != 0 || _pausedAt.tv_usec != 0) { - return; // we are already paused! - } - - evutil_gettimeofday(&_pausedAt, NULL); // remember when we paused - - { - // Verbatim copy of stop() without cancelAllDelayed() - if (_isStarted) { - _isStarted = false; - event_base_loopbreak(_eventLoop); - } - if (_thread) { - _thread->join(); - delete _thread; - _thread = NULL; - } - } - - std::lock_guard<std::recursive_mutex> lock(_mutex); - - // remove all events from libevent without deleting them - for(auto callbackData : _callbackData) { - Event data = callbackData.second.userData; - event_del(callbackData.second.event); - } - } - - void resume() { - if (_pausedAt.tv_sec != 0 || _pausedAt.tv_usec != 0) { - struct timeval now; - struct timeval pausedFor; - - evutil_gettimeofday(&now, NULL); - evutil_timersub(&now, &_pausedAt, &pausedFor); - _pausedAt.tv_sec = 0; - _pausedAt.tv_usec = 0; - - for(auto& callbackData : _callbackData) { - // add the time we were paused to all due times - evutil_timeradd(&callbackData.second.due, &pausedFor, &callbackData.second.due); - - struct timeval remain; - evutil_timersub(&callbackData.second.due, &now, &remain); - -#if 0 - std::cout << "Now : " << now.tv_sec << "." << now.tv_usec << std::endl; - std::cout << "Paused : " << pausedFor.tv_sec << "." << pausedFor.tv_usec << std::endl; - std::cout << "Remaining: " << remain.tv_sec << "." << remain.tv_usec << std::endl; -#endif - assert(remain.tv_usec >= 0 && remain.tv_sec >= 0); - - // reenqueue with libevent - event_add(callbackData.second.event, &remain); - } - } - start(); - } - -protected: - timeval _pausedAt; -}; - bool testPausableEventQueue() { Interpreter interpreter = Interpreter::fromXML(customDelayedEQ, ""); diff --git a/test/src/test-serialization.cpp b/test/src/test-serialization.cpp new file mode 100644 index 0000000..a19786e --- /dev/null +++ b/test/src/test-serialization.cpp @@ -0,0 +1,147 @@ +#define protected public + +#include "uscxml/config.h" +#include "uscxml/Interpreter.h" +#include "uscxml/interpreter/InterpreterImpl.h" +//#include "uscxml/Factory.h" +#include "uscxml/server/HTTPServer.h" + +#include "uscxml/interpreter/Logging.h" + +#include "uscxml/plugins/invoker/dirmon/DirMonInvoker.h" +#include <boost/algorithm/string.hpp> + +#ifdef _WIN32 +#include "XGetopt.h" +#include "XGetopt.cpp" +#endif + +int startedAt; +int lastTransitionAt; + +//class StatusMonitor : public uscxml::InterpreterMonitor { +class StatusMonitor : public uscxml::StateTransitionMonitor { + void beforeTakingTransition(uscxml::Interpreter& interpreter, const XERCESC_NS::DOMElement* transition) { + lastTransitionAt = time(NULL); + } + +}; + +void printUsageAndExit() { + printf("test-serialization version " USCXML_VERSION " (" CMAKE_BUILD_TYPE " build - " CMAKE_COMPILER_STRING ")\n"); + printf("Usage\n"); + printf("\ttest-stress"); +#ifdef BUILD_AS_PLUGINS + printf(" [-p pluginPath]"); +#endif + printf(" <PATH>\n"); + printf("\n"); + exit(1); +} + +int main(int argc, char** argv) { + using namespace uscxml; + + if (argc < 2) { + printUsageAndExit(); + } + + HTTPServer::getInstance(8188, 8189); +#ifndef _WIN32 + opterr = 0; +#endif + int option; + while ((option = getopt(argc, argv, "vl:p:")) != -1) { + switch(option) { + case 'p': + uscxml::Factory::setDefaultPluginPath(optarg); + break; + case '?': + break; + default: + printUsageAndExit(); + break; + } + } + + DirectoryWatch* watcher = new DirectoryWatch(argv[optind], true); + watcher->updateEntries(true); + + std::map<std::string, struct stat> entries = watcher->getAllEntries(); + + StatusMonitor vm; + vm.copyToInvokers(true); + + std::map<std::string, struct stat>::iterator entryIter = entries.begin(); + while(entryIter != entries.end()) { + if (!boost::ends_with(entryIter->first, ".scxml") || + entryIter->first.find("sub") != std::string::npos || + entryIter->first.find("test415.scxml") != std::string::npos || + entryIter->first.find("test329.scxml") != std::string::npos || + entryIter->first.find("test326.scxml") != std::string::npos || + entryIter->first.find("test307.scxml") != std::string::npos || + entryIter->first.find("test301.scxml") != std::string::npos || + entryIter->first.find("test230.scxml") != std::string::npos || + entryIter->first.find("test250.scxml") != std::string::npos || + entryIter->first.find("test178.scxml") != std::string::npos) { + entryIter++; + continue; + } + + startedAt = time(NULL); + lastTransitionAt = time(NULL); + + std::string sourceXML = std::string(argv[optind]) + PATH_SEPERATOR + entryIter->first; + sourceXML = "/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test557.scxml"; + + Interpreter interpreter = Interpreter::fromURL(sourceXML); + LOGD(USCXML_INFO) << "Processing " << interpreter.getImpl()->getBaseURL(); + if (interpreter) { + + interpreter.addMonitor(&vm); + std::string serializedState; + +RESTART_WITH_STATE: + InterpreterState state = InterpreterState::USCXML_UNDEF; + int now = time(NULL); + + try { +#if 1 + if (serializedState.size() > 0) { + Interpreter interpreter = Interpreter::fromURL(sourceXML); + interpreter.deserialize(serializedState); + } +#endif +// while(state != USCXML_FINISHED && now - startedAt < 20 && now - lastTransitionAt < 2) { + + while(state != USCXML_FINISHED) { + state = interpreter.step(); + +#if 1 + if (state == USCXML_MACROSTEPPED) { + assert(!interpreter.getImpl()->_internalQueue.dequeue(0)); + serializedState = interpreter.serialize(); +// std::cout << serializedState << std::endl; + goto RESTART_WITH_STATE; + } +#endif + + now = time(NULL); + } + assert(interpreter.isInState("pass")); + } catch (ErrorEvent e) { + LOGD(USCXML_ERROR) << e; + } catch (...) {} + } + entryIter++; + + // forever +// if (entryIter == entries.end()) { +// entryIter = entries.begin(); +// } + } + + delete watcher; + + return EXIT_SUCCESS; +} diff --git a/test/src/test-url.cpp b/test/src/test-url.cpp index 51fb545..94d5c91 100644 --- a/test/src/test-url.cpp +++ b/test/src/test-url.cpp @@ -153,6 +153,15 @@ void testFileURLs() { void testData() {
{
+ Data data;
+ std::string key = "//state[@id=\"s0\"]/onentry[1]/send[1]";
+ data[key] = Data("fooo");
+ std::string json = data.asJSON();
+ data = Data::fromJSON(json);
+ assert(data.hasKey(key));
+ }
+
+ {
Data data = Data::fromJSON("{\"shiftKey\":false,\"toElement\":{\"id\":\"\",\"localName\":\"body\"},\"clientY\":38,\"y\":38,\"x\":66,\"ctrlKey\":false,\"relatedTarget\":{\"id\":\"\",\"localName\":\"body\"},\"clientX\":66,\"screenY\":288,\"metaKey\":false,\"offsetX\":58,\"altKey\":false,\"offsetY\":30,\"fromElement\":{\"id\":\"foo\",\"localName\":\"div\"},\"screenX\":-1691,\"dataTransfer\":null,\"button\":0,\"pageY\":38,\"layerY\":38,\"pageX\":66,\"charCode\":0,\"which\":0,\"keyCode\":0,\"detail\":0,\"layerX\":66,\"returnValue\":true,\"timeStamp\":1371223991895,\"eventPhase\":2,\"target\":{\"id\":\"foo\",\"localName\":\"div\"},\"defaultPrevented\":false,\"srcElement\":{\"id\":\"foo\",\"localName\":\"div\"},\"type\":\"mouseout\",\"cancelable\":true,\"currentTarget\":{\"id\":\"foo\",\"localName\":\"div\"},\"bubbles\":true,\"cancelBubble\":false}");
std::cout << data << std::endl;
}
@@ -266,11 +275,11 @@ int main(int argc, char** argv) { HTTPServer::getInstance(8199, 8200);
try {
- testPaths();
- testFileURLs();
- testHTTPURLs();
+// testPaths();
+// testFileURLs();
+// testHTTPURLs();
testData();
- testServlets();
+// testServlets();
} catch (Event e) {
LOGD(USCXML_ERROR) << e;
exit(EXIT_FAILURE);
|