ResInsight/ApplicationCode/UnitTests/RiaGrpcInterface-Test.cpp
Gaute Lindkvist a468532d7f
#4457 Python: clean up grpc api, Python client API and make installable python package (#4456)
* gRPC: Make names more consistent

* gRPC: clean up case info and improve Python API for cases

* gRPC: much more object oriented Python interface

* Python: Make a proper pip-installable package

* Update rips Python package to auto generate setup.py with version number

* Python: add setup.py to gitignore

* Python: Update Python RIPS interface

* gRPC: Remove example client from cmake file and unit test

* gRPC: Fix up unit test after merge and hide warnings

* gRPC: fix up python client code
2019-06-03 14:33:16 +02:00

69 lines
2.4 KiB
C++

#include "gtest/gtest.h"
#include <grpc/grpc.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <numeric>
#include <memory>
#include "Properties.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::ClientReader;
using grpc::ClientReaderWriter;
using grpc::ClientWriter;
using grpc::Status;
class PropertiesClient
{
public:
PropertiesClient(std::shared_ptr<Channel> channel)
: m_stub(rips::Properties::NewStub(channel))
{
}
Status GetActiveCellProperty(rips::PropertyType propType, const std::string& propertyName, int timeStep, std::vector<double>* results) const
{
rips::PropertyRequest request;
rips::CaseRequest* requestCase = new rips::CaseRequest;
requestCase->set_id(0);
request.set_allocated_case_request(requestCase);
request.set_grid_index(0);
request.set_porosity_model(rips::PorosityModelType::MATRIX_MODEL);
request.set_property_type(propType);
request.set_property_name(propertyName);
request.set_time_step(timeStep);
rips::PropertyChunk resultArray;
ClientContext context;
std::unique_ptr<ClientReader<rips::PropertyChunk>> reader = m_stub->GetActiveCellProperty(&context, request);
while (reader->Read(&resultArray))
{
results->insert(results->end(), resultArray.values().begin(), resultArray.values().end());
}
return reader->Finish();
}
private:
std::unique_ptr<rips::Properties::Stub> m_stub;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(DISABLED_RiaGrpcInterface, SoilAverage)
{
PropertiesClient client(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
for (size_t i = 0; i < 10; ++i)
{
std::vector<double> results;
Status status = client.GetActiveCellProperty(rips::PropertyType::DYNAMIC_NATIVE, "SOIL", i, &results);
std::cout << "Number of results: " << results.size() << std::endl;
double sum = std::accumulate(results.begin(), results.end(), 0.0);
std::cout << "Avg: " << sum / static_cast<double>(results.size()) << std::endl;
EXPECT_EQ(grpc::OK, status.error_code());
}
}