Salome HOME
Updated copyright comment
[modules/yacs.git] / src / workloadmanager / Test / TestMain.cxx
index b299f06840bf2687f251e6ae8c8f872c326582e2..eaf661e70ec646bd93930b100ff5f92fbc342146 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020  EDF R&D
+// Copyright (C) 2020-2024  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -14,6 +14,7 @@
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include <cppunit/TestFixture.h>
@@ -173,10 +174,12 @@ class MyTest: public CppUnit::TestFixture
   CPPUNIT_TEST_SUITE(MyTest);
   CPPUNIT_TEST(atest);
   CPPUNIT_TEST(btest);
+  CPPUNIT_TEST(ctest);
   CPPUNIT_TEST_SUITE_END();
 public:
   void atest();
   void btest(); // ignore resources
+  void ctest(); // no available resource
 };
 
 /**
@@ -216,7 +219,7 @@ void MyTest::atest()
 
   DEBUG_LOG("Number of tasks: ", tasksNumber);
   for(int type_id = 0; type_id < typesNumber; type_id++)
-    DEBUG_LOG("Tasks from ", type_id * tasksNumber / typesNumber, 
+    DEBUG_LOG("Tasks from ", type_id * tasksNumber / typesNumber,
               " to ", (type_id + 1) * tasksNumber / typesNumber,
               " are of type ", check.types[type_id].name);
 
@@ -305,6 +308,70 @@ void MyTest::btest()
   CPPUNIT_ASSERT( duration <= maxExpectedDuration);
 }
 
+/**
+ * Test the case of a task which need more cores than any resource has.
+ */
+class ErrorTask : public WorkloadManager::Task
+{
+public:
+  ErrorTask(int nb_cores): WorkloadManager::Task(), _type(), _ok(), _message()
+  {
+    _type.ignoreResources = false;
+    _type.neededCores = nb_cores;
+  }
+
+  const WorkloadManager::ContainerType& type()const override {return _type;}
+
+  void run(const WorkloadManager::RunInfo& c)override
+  {
+    _ok = c.isOk;
+    _message = c.error_message;
+  }
+
+  bool checkState(bool ok, const std::string& message)
+  {
+    return (ok == _ok) && (message == _message);
+  }
+
+private:
+  WorkloadManager::ContainerType _type;
+  bool _ok;
+  std::string _message;
+};
+
+void MyTest::ctest()
+{
+  WorkloadManager::Resource r;
+  r.id = 1;
+  r.name = "r1";
+  r.nbCores = 1;
+  ErrorTask t1(1), t2(10);
+  WorkloadManager::DefaultAlgorithm algo;
+  WorkloadManager::WorkloadManager wlm(algo);
+  wlm.addResource(r);
+  wlm.addTask(&t1);
+  wlm.addTask(&t2);
+  wlm.start();
+  wlm.stop();
+  CPPUNIT_ASSERT(t1.checkState(true, ""));
+  CPPUNIT_ASSERT(t2.checkState(false, "No resource can run this task."));
+  // no error mode: wait for a resource to be added
+  WorkloadManager::DefaultAlgorithm algo_noerror;
+  WorkloadManager::WorkloadManager wlm2(algo_noerror);
+  wlm2.addResource(r);
+  wlm2.addTask(&t1);
+  wlm2.addTask(&t2);
+  wlm2.start();
+  std::this_thread::sleep_for(std::chrono::seconds(1));
+  r.id = 2;
+  r.name = "r2";
+  r.nbCores = 20;
+  wlm2.addResource(r);
+  wlm2.stop();
+  CPPUNIT_ASSERT(t1.checkState(true, ""));
+  CPPUNIT_ASSERT(t2.checkState(true, ""));
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(MyTest);
 
 #include "BasicMainTest.hxx"