]> SALOME platform Git repositories - tools/libbatch.git/blobdiff - src/PBS/Test/Test_PBS.cxx
Salome HOME
Merged from BR_AR
[tools/libbatch.git] / src / PBS / Test / Test_PBS.cxx
index e58278d1134865e52fc34f7d4ac5f3ae61709389..5ceb09ff68825ce68c756438ce0320dab290bbdf 100644 (file)
@@ -47,6 +47,8 @@
 using namespace std;
 using namespace Batch;
 
+const int MAX_SLEEP_TIME = 600;
+
 int main(int argc, char** argv)
 {
   cout << "*******************************************************************************************" << endl;
@@ -92,6 +94,7 @@ int main(int argc, char** argv)
     job.setParametre(p);
     // ... and its environment
     Environnement e;
+    e["MYENVVAR"] = "MYVALUE";
     job.setEnvironnement(e);
     cout << job << endl;
 
@@ -107,13 +110,28 @@ int main(int argc, char** argv)
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    string state = "Undefined";
-    for (int i=0 ; i<timeout/2 && state != "U" && state != "C"; i++) {
-      sleep(2);
-      JobInfo jinfo = jobid.queryJob();
+    int time = 0;
+    int sleeptime = 1;
+    bool testTimeout = (timeout > -1);
+    bool timeoutReached = (testTimeout && time >= timeout);
+    JobInfo jinfo = jobid.queryJob();
+    string state = jinfo.getParametre()["STATE"].str();
+    cout << "State is \"" << state << "\"";
+    while (!timeoutReached && state != "U" && state != "C") {
+      cout << ", sleeping " << sleeptime << "s..." << endl;
+      sleep(sleeptime);
+      time += sleeptime;
+      timeoutReached = (testTimeout && time >= timeout);
+      sleeptime *= 2;
+      if (testTimeout && sleeptime > timeout - time)
+        sleeptime = timeout - time;
+      if (sleeptime > MAX_SLEEP_TIME)
+        sleeptime = MAX_SLEEP_TIME;
+      jinfo = jobid.queryJob();
       state = jinfo.getParametre()["STATE"].str();
-      cout << "State is \"" << state << "\"" << endl;
+      cout << "State is \"" << state << "\"";
     }
+    cout << endl;
 
     if (state == "U" || state == "C") {
       cout << "Job " << jobid.__repr__() << " is done" << endl;
@@ -131,16 +149,21 @@ int main(int argc, char** argv)
   }
 
   // test the result file
-  string exp = "c = 12";
-  string res;
-  ifstream f("result.txt");
-  getline(f, res);
-  f.close();
-
-  cout << "result found : " << res << ", expected : " << exp << endl;
-
-  if (res == exp)
-    return 0;
-  else
+  try {
+    SimpleParser resultParser;
+    resultParser.parse("result.txt");
+    cout << "Result:" << endl << resultParser;
+    const string & envvar = resultParser.getValue("MYENVVAR");
+    int result = resultParser.getValueAsInt("c");
+    if (envvar == "MYVALUE" && result == 12) {
+      cout << "OK, Expected result found." << endl;
+      return 0;
+    } else {
+      cerr << "Error, result is not the expected one (MYENVVAR = MYVALUE, c = 12)." << endl;
+      return 1;
+    }
+  } catch (ParserException e) {
+    cerr << "Parser error on result file: " << e.what() << endl;
     return 1;
+  }
 }