summaryrefslogtreecommitdiffstats
path: root/test/src/test-stress.cpp
blob: d0c15fda9ce1462b32ea8f6cfd2016ebbfab5910 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "uscxml/config.h"
#include "uscxml/Interpreter.h"
//#include "uscxml/Factory.h"
#include "uscxml/server/HTTPServer.h"

#include "uscxml/interpreter/Logging.h"
#include "uscxml/interpreter/InterpreterMonitor.h"
#include "uscxml/interpreter/InterpreterImpl.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 {
	void beforeTakingTransition(const std::string& sessionId, const XERCESC_NS::DOMElement* transition) {
		lastTransitionAt = time(NULL);
	}

};

void printUsageAndExit() {
	printf("test-stress 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;

	std::map<std::string, struct stat>::iterator entryIter = entries.begin();
	while(entryIter != entries.end()) {
		if (!boost::ends_with(entryIter->first, ".scxml")) {
			entryIter++;
			continue;
		}

		startedAt = time(NULL);
		lastTransitionAt = time(NULL);

		Interpreter interpreter = Interpreter::fromURL(std::string(argv[optind]) + PATH_SEPERATOR + entryIter->first);
//        Interpreter interpreter = Interpreter::fromURL("/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test159.scxml");

		LOGD(USCXML_INFO) << "Processing " << interpreter.getImpl()->getBaseURL() << std::endl;
		if (interpreter) {

			interpreter.addMonitor(&vm);

			InterpreterState state = InterpreterState::USCXML_UNDEF;
			int now = time(NULL);

			try {
				while(state != USCXML_FINISHED && now - startedAt < 20 && now - lastTransitionAt < 2) {
//                while(state != USCXML_FINISHED) {
					state = interpreter.step(200);
					now = time(NULL);
				}
			} catch (...) {}
		}
		entryIter++;

		// forever
		if (entryIter == entries.end()) {
			entryIter = entries.begin();
		}
	}

	delete watcher;

	return EXIT_SUCCESS;
}