changeset 2651:1da5a052c777 jobs

testing value serialization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Jun 2018 11:53:12 +0200
parents 5f2dd8b6ee2f
children a3f0f61a14ca
files OrthancServer/LuaScripting.cpp OrthancServer/LuaScripting.h OrthancServer/ServerJobs/Operations/DicomInstanceOperationValue.h OrthancServer/ServerJobs/Operations/StorePeerOperation.cpp OrthancServer/ServerJobs/Operations/StoreScuOperation.cpp OrthancServer/ServerJobs/Operations/SystemCallOperation.cpp Resources/CMake/OrthancFrameworkConfiguration.cmake UnitTestsSources/MultiThreadingTests.cpp
diffstat 8 files changed, 103 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/LuaScripting.cpp	Fri Jun 01 19:03:28 2018 +0200
+++ b/OrthancServer/LuaScripting.cpp	Mon Jun 04 11:53:12 2018 +0200
@@ -516,7 +516,7 @@
 
   LuaScripting::LuaScripting(ServerContext& context) : 
     context_(context),
-    continue_(true)
+    state_(State_Setup)
   {
     lua_.SetGlobalVariable("_ServerContext", &context);
     lua_.RegisterFunction("RestApiGet", RestApiGet);
@@ -529,7 +529,7 @@
 
   LuaScripting::~LuaScripting()
   {
-    if (continue_)
+    if (state_ == State_Running)
     {
       LOG(ERROR) << "INTERNAL ERROR: LuaScripting::Stop() should be invoked manually to avoid mess in the destruction order!";
       Stop();
@@ -548,7 +548,7 @@
         // The event queue is empty, check whether we should stop
         boost::recursive_mutex::scoped_lock lock(that->mutex_);
 
-        if (!that->continue_)
+        if (that->state_ != State_Running)
         {
           return;
         }
@@ -572,7 +572,7 @@
   {
     boost::recursive_mutex::scoped_lock lock(mutex_);
 
-    if (!continue_ ||
+    if (state_ != State_Setup ||
         eventThread_.joinable()  /* already started */)
     {
       throw OrthancException(ErrorCode_BadSequenceOfCalls);
@@ -590,12 +590,12 @@
     {
       boost::recursive_mutex::scoped_lock lock(mutex_);
 
-      if (!continue_)
+      if (state_ != State_Running)
       {
         throw OrthancException(ErrorCode_BadSequenceOfCalls);
       }
 
-      continue_ = false;
+      state_ = State_Done;
     }
 
     jobManager_.AwakeTrailingSleep();
--- a/OrthancServer/LuaScripting.h	Fri Jun 01 19:03:28 2018 +0200
+++ b/OrthancServer/LuaScripting.h	Mon Jun 04 11:53:12 2018 +0200
@@ -47,6 +47,13 @@
   class LuaScripting : public IServerListener
   {
   private:
+    enum State
+    {
+      State_Setup,
+      State_Running,
+      State_Done
+    };
+    
     class ExecuteEvent;
     class IEvent;
     class OnStoredInstanceEvent;
@@ -74,7 +81,7 @@
     LuaContext               lua_;
     ServerContext&           context_;
     LuaJobManager            jobManager_;
-    bool                     continue_;
+    State                    state_;
     boost::thread            eventThread_;
     SharedMessageQueue       pendingEvents_;
 
--- a/OrthancServer/ServerJobs/Operations/DicomInstanceOperationValue.h	Fri Jun 01 19:03:28 2018 +0200
+++ b/OrthancServer/ServerJobs/Operations/DicomInstanceOperationValue.h	Mon Jun 04 11:53:12 2018 +0200
@@ -64,7 +64,7 @@
       return id_;
     }
 
-    void ReadContent(std::string& dicom) const
+    void ReadDicom(std::string& dicom) const
     {
       context_.ReadDicom(dicom, id_);
     }
--- a/OrthancServer/ServerJobs/Operations/StorePeerOperation.cpp	Fri Jun 01 19:03:28 2018 +0200
+++ b/OrthancServer/ServerJobs/Operations/StorePeerOperation.cpp	Mon Jun 04 11:53:12 2018 +0200
@@ -63,7 +63,7 @@
 
     try
     {
-      instance.ReadContent(client.GetBody());
+      instance.ReadDicom(client.GetBody());
 
       std::string answer;
       if (!client.Apply(answer))
--- a/OrthancServer/ServerJobs/Operations/StoreScuOperation.cpp	Fri Jun 01 19:03:28 2018 +0200
+++ b/OrthancServer/ServerJobs/Operations/StoreScuOperation.cpp	Mon Jun 04 11:53:12 2018 +0200
@@ -68,7 +68,7 @@
     try
     {
       std::string dicom;
-      instance.ReadContent(dicom);
+      instance.ReadDicom(dicom);
       resource->GetConnection().Store(dicom);
     }
     catch (OrthancException& e)
--- a/OrthancServer/ServerJobs/Operations/SystemCallOperation.cpp	Fri Jun 01 19:03:28 2018 +0200
+++ b/OrthancServer/ServerJobs/Operations/SystemCallOperation.cpp	Mon Jun 04 11:53:12 2018 +0200
@@ -61,7 +61,7 @@
           dynamic_cast<const DicomInstanceOperationValue&>(input);
 
         std::string dicom;
-        instance.ReadContent(dicom);
+        instance.ReadDicom(dicom);
 
         tmp.reset(new TemporaryFile);
         tmp->Write(dicom);
--- a/Resources/CMake/OrthancFrameworkConfiguration.cmake	Fri Jun 01 19:03:28 2018 +0200
+++ b/Resources/CMake/OrthancFrameworkConfiguration.cmake	Mon Jun 04 11:53:12 2018 +0200
@@ -130,6 +130,7 @@
   ${ORTHANC_ROOT}/Core/DicomFormat/DicomTag.cpp
   ${ORTHANC_ROOT}/Core/DicomFormat/DicomValue.cpp
   ${ORTHANC_ROOT}/Core/Enumerations.cpp
+  ${ORTHANC_ROOT}/Core/FileStorage/MemoryStorageArea.cpp
   ${ORTHANC_ROOT}/Core/Images/Font.cpp
   ${ORTHANC_ROOT}/Core/Images/FontRegistry.cpp
   ${ORTHANC_ROOT}/Core/Images/IImageWriter.cpp
--- a/UnitTestsSources/MultiThreadingTests.cpp	Fri Jun 01 19:03:28 2018 +0200
+++ b/UnitTestsSources/MultiThreadingTests.cpp	Mon Jun 04 11:53:12 2018 +0200
@@ -34,11 +34,20 @@
 #include "PrecompiledHeadersUnitTests.h"
 #include "gtest/gtest.h"
 
+#include "../Core/FileStorage/MemoryStorageArea.h"
 #include "../Core/JobsEngine/JobsEngine.h"
+#include "../Core/JobsEngine/Operations/LogJobOperation.h"
+#include "../Core/JobsEngine/Operations/NullOperationValue.h"
+#include "../Core/JobsEngine/Operations/StringOperationValue.h"
 #include "../Core/MultiThreading/SharedMessageQueue.h"
 #include "../Core/OrthancException.h"
 #include "../Core/SystemToolbox.h"
 #include "../Core/Toolbox.h"
+#include "../OrthancServer/DatabaseWrapper.h"
+#include "../OrthancServer/ServerContext.h"
+#include "../OrthancServer/ServerJobs/LuaJobManager.h"
+#include "../OrthancServer/ServerJobs/Operations/DicomInstanceOperationValue.h"
+#include "../OrthancServer/ServerJobs/OrthancJobUnserializer.h"
 
 using namespace Orthanc;
 
@@ -605,14 +614,6 @@
 }
 
 
-
-
-
-#include "../OrthancServer/ServerJobs/LuaJobManager.h"
-#include "../Core/JobsEngine/Operations/StringOperationValue.h"
-#include "../Core/JobsEngine/Operations/LogJobOperation.h"
-
-
 TEST(JobsEngine, DISABLED_SequenceOfOperationsJob)
 {
   JobsEngine engine;
@@ -675,5 +676,80 @@
   boost::this_thread::sleep(boost::posix_time::milliseconds(2000));
 
   engine.Stop();
+}
 
+
+#include "../OrthancServer/ServerContext.h"
+
+TEST(JobsSerialization, GenericValues)
+{
+  GenericJobUnserializer unserializer;
+    
+  Json::Value s;
+  std::auto_ptr<JobOperationValue> value;
+
+  {
+    NullOperationValue null;
+    null.Serialize(s);
+  }
+
+  value.reset(unserializer.UnserializeValue(s));
+  ASSERT_EQ(JobOperationValue::Type_Null, value->GetType());
+
+  {
+    StringOperationValue str("Hello");
+    str.Serialize(s);
+  }
+
+  value.reset(unserializer.UnserializeValue(s));
+  ASSERT_EQ(JobOperationValue::Type_String, value->GetType());
+  ASSERT_EQ("Hello", dynamic_cast<StringOperationValue&>(*value).GetContent());
 }
+
+
+TEST(JobsSerialization, OrthancValues)
+{
+  MemoryStorageArea storage;
+  DatabaseWrapper db;   // The SQLite DB is in memory
+  db.Open();
+  ServerContext context(db, storage);
+
+  std::string id;
+  
+  {
+    ParsedDicomFile dicom(true);
+    dicom.Replace(DICOM_TAG_PATIENT_NAME, std::string("JODOGNE"),
+                  false, DicomReplaceMode_InsertIfAbsent);
+
+    DicomInstanceToStore toStore;
+    toStore.SetParsedDicomFile(dicom);
+    ASSERT_EQ(StoreStatus_Success, context.Store(id, toStore));
+  }
+
+  {
+    OrthancJobUnserializer unserializer(context);
+    
+    Json::Value s;
+    std::auto_ptr<JobOperationValue> value;
+
+    {
+      DicomInstanceOperationValue instance(context, id);
+      instance.Serialize(s);
+    }
+
+    value.reset(unserializer.UnserializeValue(s));
+    ASSERT_EQ(JobOperationValue::Type_DicomInstance, value->GetType());
+    ASSERT_EQ(id, dynamic_cast<DicomInstanceOperationValue&>(*value).GetId());
+
+    {
+      std::string content;
+      dynamic_cast<DicomInstanceOperationValue&>(*value).ReadDicom(content);
+
+      ParsedDicomFile dicom(content);
+      ASSERT_TRUE(dicom.GetTagValue(content, DICOM_TAG_PATIENT_NAME));
+      ASSERT_EQ("JODOGNE", content);
+    }
+  }
+
+  context.Stop();
+}