#4460 #4424 Adding Exit() to the gRPC interface plus implemented test framework

This commit is contained in:
Gaute Lindkvist 2019-06-04 12:59:06 +02:00
parent 930abbf02f
commit 3530c8b3de
17 changed files with 146 additions and 79 deletions

View File

@ -345,7 +345,10 @@ void RiaConsoleApplication::showErrorMessage(const QString& errMsg)
void RiaConsoleApplication::launchGrpcServer()
{
#ifdef ENABLE_GRPC
m_grpcServer->run();
m_grpcServer->runInThread();
m_idleTimer = new QTimer(this);
connect(m_idleTimer, SIGNAL(timeout()), this, SLOT(runIdleProcessing()));
m_idleTimer->start(0);
#endif
}
@ -381,3 +384,21 @@ void RiaConsoleApplication::onProjectClosed()
processEvents();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaConsoleApplication::runIdleProcessing()
{
#ifdef ENABLE_GRPC
if (RiaGrpcServer::receivedExitRequest())
{
m_grpcServer->quit();
QCoreApplication::quit();
}
else
{
m_grpcServer->processAllQueuedRequests();
}
#endif
}

View File

@ -21,6 +21,9 @@
#include <QCoreApplication>
#include <QPointer>
#include <QTimer>
namespace cvf
{
class ProgramOptions;
@ -48,5 +51,13 @@ protected:
void onProjectOpeningError(const QString& errMsg) override;
void onProjectOpened();
void onProjectClosed();
private slots:
void runIdleProcessing();
private:
#ifdef ENABLE_GRPC
QPointer<QTimer> m_idleTimer;
#endif
};

View File

@ -1687,7 +1687,12 @@ void RiaGuiApplication::slotWorkerProcessFinished(int exitCode, QProcess::ExitSt
void RiaGuiApplication::runIdleProcessing()
{
#ifdef ENABLE_GRPC
if (!caf::ProgressInfoStatic::isRunning())
if (RiaGrpcServer::receivedExitRequest())
{
m_grpcServer->quit();
QCoreApplication::quit();
}
else if (!caf::ProgressInfoStatic::isRunning())
{
m_grpcServer->processAllQueuedRequests();
}

View File

@ -9,7 +9,7 @@ set ( SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcGridService.h
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcProjectService.h
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcCommandService.h
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcAppInfoService.h
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcAppService.h
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcPropertiesService.h
)
@ -20,7 +20,7 @@ set ( SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcGridService.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcProjectService.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcCommandService.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcAppInfoService.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcAppService.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaGrpcPropertiesService.cpp
)
@ -79,7 +79,7 @@ set(PROTO_FILES
"Case"
"Project"
"Commands"
"AppInfo"
"App"
"Properties"
"Grid"
)
@ -149,7 +149,7 @@ if (PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE})
${GRPC_PYTHON_GENERATED_SOURCES}
"generated/RiaVersionInfo.py"
"rips/__init__.py"
"rips/AppInfo.py"
"rips/App.py"
"rips/Case.py"
"rips/Commands.py"
"rips/Grid.py"
@ -168,7 +168,9 @@ if (PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE})
"examples/InputPropTestAsync.py"
"examples/SoilAverage.py"
"examples/SoilAverageNoComm.py"
"tests/test_sample.py"
"tests/test_cases.py"
"tests/conftest.py"
"tests/dataroot.py"
"requirements.txt"
"setup.py.cmake"
"README.md"

View File

@ -4,8 +4,9 @@ package rips;
import "Empty.proto";
service AppInfo {
service App {
rpc GetVersion(Empty) returns (Version) {}
rpc Exit(Empty) returns (Empty) {}
}
message Version {

View File

@ -6,14 +6,14 @@ sys.path.insert(1, os.path.join(sys.path[0], '../generated'))
from Empty_pb2 import Empty
import AppInfo_pb2
import AppInfo_pb2_grpc
import App_pb2
import App_pb2_grpc
class AppInfo:
class App:
def __init__(self, channel):
self.appInfo = AppInfo_pb2_grpc.AppInfoStub(channel)
self.app = App_pb2_grpc.AppStub(channel)
def versionMessage(self):
return self.appInfo.GetVersion(Empty())
return self.app.GetVersion(Empty())
def majorVersion(self):
return self.versionMessage().major_version
def minorVersion(self):
@ -22,4 +22,5 @@ class AppInfo:
return self.versionMessage().patch_version
def versionString(self):
return str(self.majorVersion()) + "." + str(self.minorVersion()) + "." + str(self.patchVersion())
def exit(self):
return self.app.Exit(Empty())

View File

@ -20,7 +20,7 @@ class Case:
self.type = info.type
self.properties = Properties(self)
self.request = Case_pb2.CaseRequest(id=self.id)
def gridCount(self):
try:
return self.stub.GetGridCount(self.request).count

View File

@ -8,7 +8,7 @@ sys.path.insert(1, os.path.join(sys.path[0], '../generated'))
import RiaVersionInfo
from .AppInfo import AppInfo
from .App import App
from .Commands import Commands
from .Project import Project
@ -60,10 +60,10 @@ class Instance:
self.channel = grpc.insecure_channel(location)
# Main version check package
self.appInfo = AppInfo(self.channel)
self.app = App(self.channel)
try:
majorVersionOk = self.appInfo.majorVersion() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION)
minorVersionOk = self.appInfo.minorVersion() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION)
majorVersionOk = self.app.majorVersion() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION)
minorVersionOk = self.app.minorVersion() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION)
if not (majorVersionOk and minorVersionOk):
raise Exception('Version of ResInsight does not match version of Python API')
except grpc.RpcError as e:

View File

@ -44,3 +44,7 @@ class Project:
return case
except grpc.RpcError as e:
return None
def loadCase(self, path):
return Commands(self.channel).loadCase(path)

View File

@ -0,0 +1,16 @@
import pytest
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
import rips
instance = rips.Instance.launch()
@pytest.fixture
def rips_instance():
return instance
def pytest_unconfigure(config):
print("Telling ResInsight to Exit")
instance.app.exit()

View File

@ -0,0 +1 @@
PATH = "../../../TestModels"

View File

@ -0,0 +1,19 @@
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
import rips
import dataroot
def test_Launch(rips_instance):
assert(rips_instance is not None)
def test_EmptyProject(rips_instance):
cases = rips_instance.project.cases()
assert(len(cases) is 0)
def test_OneCase(rips_instance):
case = rips_instance.project.loadCase(dataroot.PATH + "/TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID")
cases = rips_instance.project.cases()
assert(len(cases) is 1)

View File

@ -1,51 +0,0 @@
import os, sys
# Add the 'api' path to system path to be able to import modules from the 'api' folder
# python current working directory must be 'tests'
sys.path.insert(1, os.path.join(sys.path[0], '..\\api'))
import ResInsight
resInsight = ResInsight.Instance()
# content of test_sample.py
def getActiveCellCount(caseId):
activeCellInfoChunks = resInsight.gridInfo.streamActiveCellInfo(caseId)
receivedActiveCells = []
for activeCellChunk in activeCellInfoChunks:
for activeCell in activeCellChunk.data:
receivedActiveCells.append(activeCell)
return len(receivedActiveCells)
def myOpenProject(filepath):
resInsight = ResInsight.Instance()
#resInsight.commands.setMainWindowSize(width=800, height=500)
resInsight.commands.openProject(filepath)
def test_openProjectAndCountCells():
testRepositoryRoot = "d:\\gitroot-ceesol\\ResInsight-regression-test"
#casePath = testRepositoryRoot + "\\ModelData\\TEST10K_FLT_LGR_NNC\\TEST10K_FLT_LGR_NNC.EGRID"
#openEclipseCase(casePath)
# projectPath = testRepositoryRoot + "\\ProjectFiles\\ProjectFilesSmallTests\\TestCase_10K_Complete\\RegressionTest.rsp"
# projectPath = testRepositoryRoot + "\\ProjectFiles\\ProjectFilesSmallTests\\TestCase_Norne\\RegressionTest.rsp"
projectPath = testRepositoryRoot + "\\ProjectFiles\\ProjectFilesSmallTests\\TestCase_10K_Watertight\\RegressionTest.rsp"
myOpenProject(projectPath)
assert getActiveCellCount(0) == 11125
def test_openCaseAndCountCells():
testRepositoryRoot = "d:\\gitroot-ceesol\\ResInsight-regression-test"
casePath = testRepositoryRoot + "\\ModelData\\TEST10K_FLT_LGR_NNC\\TEST10K_FLT_LGR_NNC.EGRID"
resInsight.commands.loadCase(casePath)
assert getActiveCellCount(0) == 11125
resInsight.commands.closeProject()

View File

@ -15,15 +15,16 @@
// for more details.
//
//////////////////////////////////////////////////////////////////////////////////
#include "RiaGrpcAppInfoService.h"
#include "RiaGrpcAppService.h"
#include "RiaVersionInfo.h"
#include "RiaGrpcCallbacks.h"
#include "RiaGrpcServer.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
grpc::Status RiaGrpcAppInfoService::GetVersion(grpc::ServerContext* context, const rips::Empty* request, rips::Version* reply)
grpc::Status RiaGrpcAppService::GetVersion(grpc::ServerContext* context, const rips::Empty* request, rips::Version* reply)
{
reply->set_major_version(RESINSIGHT_MAJOR_VERSION);
reply->set_minor_version(RESINSIGHT_MINOR_VERSION);
@ -34,11 +35,21 @@ grpc::Status RiaGrpcAppInfoService::GetVersion(grpc::ServerContext* context, con
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RiaGrpcCallbackInterface*> RiaGrpcAppInfoService::createCallbacks()
grpc::Status RiaGrpcAppService::Exit(grpc::ServerContext* context, const rips::Empty* request, rips::Empty* reply)
{
typedef RiaGrpcAppInfoService Self;
return { new RiaGrpcUnaryCallback<Self, rips::Empty, rips::Version>(this, &Self::GetVersion, &Self::RequestGetVersion) };
RiaGrpcServer::setReceivedExitRequest();
return grpc::Status::OK;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RiaGrpcCallbackInterface*> RiaGrpcAppService::createCallbacks()
{
typedef RiaGrpcAppService Self;
return { new RiaGrpcUnaryCallback<Self, rips::Empty, rips::Version>(this, &Self::GetVersion, &Self::RequestGetVersion),
new RiaGrpcUnaryCallback<Self, rips::Empty, rips::Empty>(this, &Self::Exit, &Self::RequestExit) };
}
static bool RiaGrpcAppInfoService_init =
RiaGrpcServiceFactory::instance()->registerCreator<RiaGrpcAppInfoService>(typeid(RiaGrpcAppInfoService).hash_code());
RiaGrpcServiceFactory::instance()->registerCreator<RiaGrpcAppService>(typeid(RiaGrpcAppService).hash_code());

View File

@ -21,7 +21,7 @@
#include "RiaGrpcServiceInterface.h"
#include "AppInfo.grpc.pb.h"
#include "App.grpc.pb.h"
#include <grpcpp/grpcpp.h>
#include <vector>
@ -39,10 +39,11 @@ class PdmValueField;
class RiaGrpcCallbackInterface;
class RiaGrpcAppInfoService : public rips::AppInfo::AsyncService, public RiaGrpcServiceInterface
class RiaGrpcAppService : public rips::App::AsyncService, public RiaGrpcServiceInterface
{
public:
grpc::Status GetVersion(grpc::ServerContext* context, const rips::Empty* request, rips::Version* reply) override;
grpc::Status Exit(grpc::ServerContext* context, const rips::Empty* request, rips::Empty* reply) override;
std::vector<RiaGrpcCallbackInterface*> createCallbacks() override;
};

View File

@ -323,6 +323,11 @@ void RiaGrpcServer::runInThread()
m_serverImpl->runInThread();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaGrpcServer::s_receivedExitRequest = false;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -373,3 +378,21 @@ int RiaGrpcServer::findAvailablePortNumber(int defaultPortNumber)
}
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaGrpcServer::setReceivedExitRequest()
{
RiaLogging::info("Received Exit Request");
s_receivedExitRequest = true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaGrpcServer::receivedExitRequest()
{
return s_receivedExitRequest;
}

View File

@ -44,10 +44,12 @@ public:
void processAllQueuedRequests();
void quit();
static int findAvailablePortNumber(int defaultPortNumber);
static void setReceivedExitRequest();
static bool receivedExitRequest();
private:
void initialize();
private:
RiaGrpcServerImpl* m_serverImpl;
static bool s_receivedExitRequest;
};