History

Add NETWORKLESS_TEST_SCRIPT type to NetworkManager.

This is a slightly hacky way to make ScriptManager work without any networking, and minimize the number of dependencies needed to run a test.

Dale Glass committed 3 years ago

Commit 02a0e331 parent 12f239b7 files · +170 −58Browse files

Showing the changes to tests/script-engine/src/ScriptEngineTests.cpp (1 of 7 files). Show all

Unified · Split

tests/script-engine/src/ScriptEngineTests.cpp+72−40
@@ -1,5 +1,8 @@
11 #include <QSignalSpy>
22 #include <QDebug>
3+#include <QFile>
4+#include <QTextStream>
5+
36
47 #include "ScriptEngineTests.h"
58 #include "DependencyManager.h"
@@ -7,6 +10,8 @@ · 1 unmodified line
710 #include "ScriptEngines.h"
811 #include "ScriptEngine.h"
912 #include "ScriptCache.h"
13+#include "ScriptManager.h"
14+
1015 #include "ResourceManager.h"
1116 #include "ResourceRequestObserver.h"
1217 #include "StatTracker.h"
@@ -18,34 +23,21 @@ · 5 unmodified lines
1823 QTEST_MAIN(ScriptEngineTests)
1924
2025
21−// script factory generates scriptmanager -- singleton
22−//
23−// default scripts -- all in one thread, but chat spawns a separate thread
24−// // https://apidocs.overte.org/Script.html#.executeOnScriptThread
25−//
26−// scriptmanager
27−// every thread has a manager, and its own engine
28−// provides non-qt interface?
29−//
30−// special threads for entity scripts -- 12 (fixed? dynamic?)
31−
32−
33−
3426
3527 void ScriptEngineTests::initTestCase() {
3628 // AudioClient starts networking, but for the purposes of the tests here we don't care,
3729 // so just got to use some port.
38− int listenPort = 10000;
30+ //int listenPort = 10000;
3931
40− DependencyManager::registerInheritance<LimitedNodeList, NodeList>();
41− DependencyManager::set<NodeList>(NodeType::Agent, listenPort);
42− DependencyManager::set<ScriptEngines>(ScriptManager::CLIENT_SCRIPT, QUrl(""));
32+ //DependencyManager::registerInheritance<LimitedNodeList, NodeList>();
33+ //DependencyManager::set<NodeList>(NodeType::Agent, listenPort);
34+ DependencyManager::set<ScriptEngines>(ScriptManager::NETWORKLESS_TEST_SCRIPT, QUrl(""));
4335 DependencyManager::set<ScriptCache>();
44− DependencyManager::set<ResourceManager>();
45− DependencyManager::set<ResourceRequestObserver>();
36+ // DependencyManager::set<ResourceManager>();
37+ // DependencyManager::set<ResourceRequestObserver>();
4638 DependencyManager::set<StatTracker>();
4739 DependencyManager::set<ScriptInitializers>();
48− DependencyManager::set<EntityScriptingInterface>(true);
40+ // DependencyManager::set<EntityScriptingInterface>(true);
4941
5042 QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
5143 QVERIFY(!ac.isNull());
@@ -77,31 +69,71 @@ void ScriptEngineTests::scriptTest() { · 25 unmodified lines
7769 QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>();
7870 QVERIFY(!ac.isNull());
7971
80− // TODO: can we execute test scripts in serial way rather than parallel
81− /*QDir testScriptsDir("tests");
72+
73+ QDir testScriptsDir("tests");
8274 QStringList testScripts = testScriptsDir.entryList(QStringList() << "*.js", QDir::Files);
8375 testScripts.sort();
8476
85− for(QString script : testScripts) {
86− script = "tests/" + script;
87− qInfo() << "Running test script: " << script;
88− ac->loadOneScript(script);
89− }*/
90− //ac->loadOneScript("tests/003_vector_math.js");
91− ac->loadOneScript("tests/005_include.js");
92−
93− qDebug() << ac->getRunning();
94−
95− // TODO: if I don't have infinite loop here, it exits before scripts finish. It also reports: QSignalSpy: No such signal: 'scriptCountChanged'
96− for (int n = 0; n > -1; n++) {
97− QSignalSpy spy(ac.get(), SIGNAL(scriptCountChanged));
98− spy.wait(100000);
99− qDebug() << "Signal happened";
100− }
101− //spy.wait(5000);
102− //ac->shutdownScripting();
77+ for(QString scriptFilename : testScripts) {
78+ scriptFilename = "tests/" + scriptFilename;
79+ qInfo() << "Running test script: " << scriptFilename;
80+
81+ QString scriptSource;
82+
83+ {
84+ QFile scriptFile(scriptFilename);
85+ scriptFile.open(QIODevice::ReadOnly);
86+ QTextStream scriptStream(&scriptFile);
87+ scriptSource.append(scriptStream.readAll());
88+
89+ // Scripts keep on running until Script.stop() is called. For our tests here,
90+ // that's not desirable, so we append an automatic stop at the end of every
91+ // script.
92+ scriptSource.append("\nScript.stop(true);\n");
93+ }
94+
95+
96+ //qDebug() << "Source: " << scriptSource;
10397
98+ ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename);
10499
105100
101+ connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){
102+ qWarning() << "Loaded script" << filename;
103+ });
104+
105+
106+ connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){
107+ qWarning() << "Failed to load script" << filename;
108+ });
109+
110+ connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){
111+ qDebug() << "Printed message from engine" << engineName << ": " << message;
112+ });
113+
114+ connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){
115+ qInfo() << "Info message from engine" << engineName << ": " << message;
116+ });
117+
118+ connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){
119+ qWarning() << "Warning from engine" << engineName << ": " << message;
120+ });
121+
122+ connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){
123+ qCritical() << "Error from engine" << engineName << ": " << message;
124+ });
125+
126+ connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){
127+ qInfo() << "Finished running script" << fileNameString;
128+ });
129+
130+ connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){
131+ qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished();
132+ });
133+
134+
135+ sm->run();
136+ }
137+
106138 //TODO: Add a test for Script.require(JSON)
107139 }