Refactoring std::chrono out.

pull/1227/head
Nicolas Noble 10 years ago committed by Nicolas "Pixel" Noble
parent 9973aa3aa6
commit 89219162dd
  1. 23
      include/grpc++/client_context.h
  2. 13
      include/grpc++/completion_queue.h
  3. 5
      include/grpc++/credentials.h
  4. 13
      include/grpc++/server_context.h
  5. 30
      include/grpc++/time.h
  6. 3
      src/cpp/client/channel.cc
  7. 15
      src/cpp/client/client_context.cc
  8. 12
      src/cpp/client/secure_credentials.cc
  9. 9
      src/cpp/common/completion_queue.cc
  10. 4
      src/cpp/server/server.cc
  11. 8
      src/cpp/server/server_context.cc
  12. 7
      src/cpp/util/time.cc
  13. 2
      test/cpp/client/credentials_test.cc
  14. 4
      test/cpp/end2end/async_end2end_test.cc
  15. 16
      test/cpp/end2end/end2end_test.cc
  16. 2
      test/cpp/end2end/generic_end2end_test.cc
  17. 5
      test/cpp/interop/client.cc
  18. 3
      test/cpp/util/time_test.cc

@ -34,15 +34,13 @@
#ifndef GRPCXX_CLIENT_CONTEXT_H
#define GRPCXX_CLIENT_CONTEXT_H
#include <chrono>
#include <map>
#include <string>
#include <grpc/support/log.h>
#include <grpc/support/time.h>
#include <grpc++/config.h>
using std::chrono::system_clock;
#include <grpc++/time.h>
struct grpc_call;
struct grpc_completion_queue;
@ -87,8 +85,19 @@ class ClientContext {
return trailing_metadata_;
}
void set_absolute_deadline(const system_clock::time_point& deadline);
system_clock::time_point absolute_deadline();
template <typename T>
void set_deadline(const T& deadline) {
TimePoint<T> deadline_tp(deadline);
deadline_ = deadline_tp.raw_time();
}
#ifndef GRPC_CXX0X_NO_CHRONO
std::chrono::system_clock::time_point deadline() {
return Timespec2Timepoint(deadline_);
}
#endif // !GRPC_CXX0X_NO_CHRONO
gpr_timespec raw_deadline() { return deadline_; }
void set_authority(const grpc::string& authority) { authority_ = authority; }
@ -125,14 +134,12 @@ class ClientContext {
grpc_completion_queue* cq() { return cq_; }
void set_cq(grpc_completion_queue* cq) { cq_ = cq; }
gpr_timespec RawDeadline() { return absolute_deadline_; }
grpc::string authority() { return authority_; }
bool initial_metadata_received_;
grpc_call* call_;
grpc_completion_queue* cq_;
gpr_timespec absolute_deadline_;
gpr_timespec deadline_;
grpc::string authority_;
std::multimap<grpc::string, grpc::string> send_initial_metadata_;
std::multimap<grpc::string, grpc::string> recv_initial_metadata_;

@ -34,9 +34,9 @@
#ifndef GRPCXX_COMPLETION_QUEUE_H
#define GRPCXX_COMPLETION_QUEUE_H
#include <chrono>
#include <grpc++/impl/client_unary_call.h>
#include <grpc/support/time.h>
#include <grpc++/impl/client_unary_call.h>
#include <grpc++/time.h>
struct grpc_completion_queue;
@ -82,10 +82,13 @@ class CompletionQueue {
// Nonblocking (until deadline) read from queue.
// Cannot rely on result of tag or ok if return is TIMEOUT
NextStatus AsyncNext(void** tag, bool* ok,
std::chrono::system_clock::time_point deadline);
template<typename T>
NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
TimePoint<T> deadline_tp(deadline);
return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
}
// Blocking (until deadline) read from queue.
// Blocking read from queue.
// Returns false if the queue is ready for destruction, true if event
bool Next(void** tag, bool* ok) {

@ -34,7 +34,6 @@
#ifndef GRPCXX_CREDENTIALS_H
#define GRPCXX_CREDENTIALS_H
#include <chrono>
#include <memory>
#include <grpc++/config.h>
@ -103,7 +102,7 @@ std::unique_ptr<Credentials> ComputeEngineCredentials();
// grpc_max_auth_token_lifetime or will be cropped to this value.
std::unique_ptr<Credentials> ServiceAccountCredentials(
const grpc::string& json_key, const grpc::string& scope,
std::chrono::seconds token_lifetime);
long token_lifetime);
// Builds JWT credentials.
// json_key is the JSON key string containing the client's private key.
@ -111,7 +110,7 @@ std::unique_ptr<Credentials> ServiceAccountCredentials(
// this credentials. It should not exceed grpc_max_auth_token_lifetime or
// will be cropped to this value.
std::unique_ptr<Credentials> JWTCredentials(
const grpc::string& json_key, std::chrono::seconds token_lifetime);
const grpc::string& json_key, long token_lifetime);
// Builds refresh token credentials.
// json_refresh_token is the JSON string containing the refresh token along

@ -34,10 +34,11 @@
#ifndef GRPCXX_SERVER_CONTEXT_H
#define GRPCXX_SERVER_CONTEXT_H
#include <chrono>
#include <map>
#include <grpc/support/time.h>
#include <grpc++/config.h>
#include <grpc++/time.h>
struct gpr_timespec;
struct grpc_metadata;
@ -71,9 +72,13 @@ class ServerContext {
ServerContext(); // for async calls
~ServerContext();
std::chrono::system_clock::time_point absolute_deadline() {
return deadline_;
#ifndef GRPC_CXX0X_NO_CHRONO
std::chrono::system_clock::time_point deadline() {
return Timespec2Timepoint(deadline_);
}
#endif // !GRPC_CXX0X_NO_CHRONO
gpr_timespec raw_deadline() { return deadline_; }
void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
@ -110,7 +115,7 @@ class ServerContext {
CompletionOp* completion_op_;
std::chrono::system_clock::time_point deadline_;
gpr_timespec deadline_;
grpc_call* call_;
CompletionQueue* cq_;
bool sent_initial_metadata_;

@ -34,6 +34,23 @@
#ifndef GRPC_INTERNAL_CPP_UTIL_TIME_H
#define GRPC_INTERNAL_CPP_UTIL_TIME_H
#include <grpc++/config.h>
namespace grpc {
template <typename T>
class TimePoint {
public:
TimePoint(const T& time) : time_(time) { }
gpr_timespec raw_time() const { return time_; }
private:
gpr_timespec time_;
};
} // namespace grpc
#ifndef GRPC_CXX0X_NO_CHRONO
#include <chrono>
#include <grpc/support/time.h>
@ -46,6 +63,19 @@ void Timepoint2Timespec(const std::chrono::system_clock::time_point& from,
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t);
template <>
class TimePoint<std::chrono::system_clock::time_point> {
public:
TimePoint(const std::chrono::system_clock::time_point& time) {
Timepoint2Timespec(time, &time_);
}
gpr_timespec raw_time() const { return time_; }
private:
gpr_timespec time_;
};
} // namespace grpc
#endif // !GRPC_CXX0X_NO_CHRONO
#endif // GRPC_INTERNAL_CPP_UTIL_TIME_H

@ -33,7 +33,6 @@
#include "src/cpp/client/channel.h"
#include <chrono>
#include <memory>
#include <grpc/grpc.h>
@ -64,7 +63,7 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
context->authority().empty()
? target_.c_str()
: context->authority().c_str(),
context->RawDeadline());
context->raw_deadline());
context->set_call(c_call);
return Call(c_call, this, cq);
}

@ -34,9 +34,7 @@
#include <grpc++/client_context.h>
#include <grpc/grpc.h>
#include "src/cpp/util/time.h"
using std::chrono::system_clock;
#include <grpc++/time.h>
namespace grpc {
@ -44,7 +42,7 @@ ClientContext::ClientContext()
: initial_metadata_received_(false),
call_(nullptr),
cq_(nullptr),
absolute_deadline_(gpr_inf_future) {}
deadline_(gpr_inf_future) {}
ClientContext::~ClientContext() {
if (call_) {
@ -64,15 +62,6 @@ ClientContext::~ClientContext() {
}
}
void ClientContext::set_absolute_deadline(
const system_clock::time_point& deadline) {
Timepoint2Timespec(deadline, &absolute_deadline_);
}
system_clock::time_point ClientContext::absolute_deadline() {
return Timespec2Timepoint(absolute_deadline_);
}
void ClientContext::AddMetadata(const grpc::string& meta_key,
const grpc::string& meta_value) {
send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));

@ -96,27 +96,27 @@ std::unique_ptr<Credentials> ComputeEngineCredentials() {
// Builds service account credentials.
std::unique_ptr<Credentials> ServiceAccountCredentials(
const grpc::string& json_key, const grpc::string& scope,
std::chrono::seconds token_lifetime) {
if (token_lifetime.count() <= 0) {
long token_lifetime) {
if (token_lifetime <= 0) {
gpr_log(GPR_ERROR,
"Trying to create ServiceAccountCredentials "
"with non-positive lifetime");
return WrapCredentials(nullptr);
}
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count());
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime);
return WrapCredentials(grpc_service_account_credentials_create(
json_key.c_str(), scope.c_str(), lifetime));
}
// Builds JWT credentials.
std::unique_ptr<Credentials> JWTCredentials(
const grpc::string& json_key, std::chrono::seconds token_lifetime) {
if (token_lifetime.count() <= 0) {
const grpc::string& json_key, long token_lifetime) {
if (token_lifetime <= 0) {
gpr_log(GPR_ERROR,
"Trying to create JWTCredentials with non-positive lifetime");
return WrapCredentials(nullptr);
}
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count());
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime);
return WrapCredentials(
grpc_jwt_credentials_create(json_key.c_str(), lifetime));
}

@ -36,7 +36,7 @@
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include "src/cpp/util/time.h"
#include <grpc++/time.h>
namespace grpc {
@ -77,13 +77,6 @@ CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal(
}
}
CompletionQueue::NextStatus CompletionQueue::AsyncNext(
void** tag, bool* ok, std::chrono::system_clock::time_point deadline) {
gpr_timespec gpr_deadline;
Timepoint2Timespec(deadline, &gpr_deadline);
return AsyncNextInternal(tag, ok, gpr_deadline);
}
bool CompletionQueue::Pluck(CompletionQueueTag* tag) {
std::unique_ptr<grpc_event, EventDeleter> ev;

@ -45,9 +45,9 @@
#include <grpc++/server_context.h>
#include <grpc++/server_credentials.h>
#include <grpc++/thread_pool_interface.h>
#include <grpc++/time.h>
#include "src/cpp/proto/proto_utils.h"
#include "src/cpp/util/time.h"
namespace grpc {
@ -348,7 +348,7 @@ class Server::AsyncRequest GRPC_FINAL : public CompletionQueueTag {
ServerContext* ctx = ctx_ ? ctx_ : generic_ctx_;
GPR_ASSERT(ctx);
if (*status) {
ctx->deadline_ = Timespec2Timepoint(call_details_.deadline);
ctx->deadline_ = call_details_.deadline;
for (size_t i = 0; i < array_.count; i++) {
ctx->client_metadata_.insert(std::make_pair(
grpc::string(array_.metadata[i].key),

@ -33,11 +33,11 @@
#include <grpc++/server_context.h>
#include <grpc++/impl/call.h>
#include <grpc++/impl/sync.h>
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include "src/cpp/util/time.h"
#include <grpc++/impl/call.h>
#include <grpc++/impl/sync.h>
#include <grpc++/time.h>
namespace grpc {
@ -99,7 +99,7 @@ ServerContext::ServerContext()
ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
size_t metadata_count)
: completion_op_(nullptr),
deadline_(Timespec2Timepoint(deadline)),
deadline_(deadline),
call_(nullptr),
cq_(nullptr),
sent_initial_metadata_(false) {

@ -31,9 +31,12 @@
*
*/
#include "src/cpp/util/time.h"
#include <grpc++/config.h>
#ifndef GRPC_CXX0X_NO_CHRONO
#include <grpc/support/time.h>
#include <grpc++/time.h>
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
@ -68,3 +71,5 @@ system_clock::time_point Timespec2Timepoint(gpr_timespec t) {
}
} // namespace grpc
#endif // !GRPC_CXX0X_NO_CHRONO

@ -47,7 +47,7 @@ class CredentialsTest : public ::testing::Test {
TEST_F(CredentialsTest, InvalidServiceAccountCreds) {
std::unique_ptr<Credentials> bad1 =
ServiceAccountCredentials("", "", std::chrono::seconds(1));
ServiceAccountCredentials("", "", 1);
EXPECT_EQ(nullptr, bad1.get());
}

@ -35,9 +35,9 @@
#include <memory>
#include "test/core/util/test_config.h"
#include "test/core/util/port.h"
#include "test/cpp/util/echo_duplicate.pb.h"
#include "test/cpp/util/echo.pb.h"
#include "src/cpp/util/time.h"
#include <grpc++/async_unary_call.h>
#include <grpc++/channel_arguments.h>
#include <grpc++/channel_interface.h>
@ -50,7 +50,7 @@
#include <grpc++/server_credentials.h>
#include <grpc++/status.h>
#include <grpc++/stream.h>
#include "test/core/util/port.h"
#include <grpc++/time.h>
#include <gtest/gtest.h>
#include <grpc/grpc.h>

@ -34,10 +34,10 @@
#include <chrono>
#include <thread>
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/cpp/util/echo_duplicate.pb.h"
#include "test/cpp/util/echo.pb.h"
#include "src/cpp/util/time.h"
#include "src/cpp/server/thread_pool.h"
#include <grpc++/channel_arguments.h>
#include <grpc++/channel_interface.h>
@ -50,7 +50,7 @@
#include <grpc++/server_credentials.h>
#include <grpc++/status.h>
#include <grpc++/stream.h>
#include "test/core/util/port.h"
#include <grpc++/time.h>
#include <gtest/gtest.h>
#include <grpc/grpc.h>
@ -72,8 +72,8 @@ void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
EchoResponse* response) {
if (request->has_param() && request->param().echo_deadline()) {
gpr_timespec deadline = gpr_inf_future;
if (context->absolute_deadline() != system_clock::time_point::max()) {
Timepoint2Timespec(context->absolute_deadline(), &deadline);
if (context->deadline() != system_clock::time_point::max()) {
Timepoint2Timespec(context->deadline(), &deadline);
}
response->mutable_param()->set_request_deadline(deadline.tv_sec);
}
@ -245,7 +245,7 @@ TEST_F(End2endTest, RpcDeadlineExpires) {
ClientContext context;
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::microseconds(10);
context.set_absolute_deadline(deadline);
context.set_deadline(deadline);
Status s = stub_->Echo(&context, request, &response);
EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code());
}
@ -260,7 +260,7 @@ TEST_F(End2endTest, RpcLongDeadline) {
ClientContext context;
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::hours(1);
context.set_absolute_deadline(deadline);
context.set_deadline(deadline);
Status s = stub_->Echo(&context, request, &response);
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
@ -277,7 +277,7 @@ TEST_F(End2endTest, EchoDeadline) {
ClientContext context;
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(100);
context.set_absolute_deadline(deadline);
context.set_deadline(deadline);
Status s = stub_->Echo(&context, request, &response);
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
@ -428,7 +428,7 @@ TEST_F(End2endTest, DiffPackageServices) {
// rpc and stream should fail on bad credentials.
TEST_F(End2endTest, BadCredentials) {
std::unique_ptr<Credentials> bad_creds =
ServiceAccountCredentials("", "", std::chrono::seconds(1));
ServiceAccountCredentials("", "", 1);
EXPECT_EQ(nullptr, bad_creds.get());
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), bad_creds, ChannelArguments());

@ -35,7 +35,6 @@
#include <memory>
#include "src/cpp/proto/proto_utils.h"
#include "src/cpp/util/time.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/cpp/util/echo.pb.h"
@ -55,6 +54,7 @@
#include <grpc++/slice.h>
#include <grpc++/status.h>
#include <grpc++/stream.h>
#include <grpc++/time.h>
#include <gtest/gtest.h>
#include <grpc/grpc.h>

@ -138,8 +138,7 @@ std::shared_ptr<ChannelInterface> CreateChannelForTestCase(
std::unique_ptr<Credentials> creds;
GPR_ASSERT(FLAGS_enable_ssl);
grpc::string json_key = GetServiceAccountJsonKey();
creds = ServiceAccountCredentials(json_key, FLAGS_oauth_scope,
std::chrono::hours(1));
creds = ServiceAccountCredentials(json_key, FLAGS_oauth_scope, 3600);
return CreateTestChannel(host_port, FLAGS_server_host_override,
FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
} else if (test_case == "compute_engine_creds") {
@ -152,7 +151,7 @@ std::shared_ptr<ChannelInterface> CreateChannelForTestCase(
std::unique_ptr<Credentials> creds;
GPR_ASSERT(FLAGS_enable_ssl);
grpc::string json_key = GetServiceAccountJsonKey();
creds = JWTCredentials(json_key, std::chrono::hours(1));
creds = JWTCredentials(json_key, 3600);
return CreateTestChannel(host_port, FLAGS_server_host_override,
FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
} else {

@ -31,11 +31,10 @@
*
*/
#include "src/cpp/util/time.h"
#include <chrono>
#include <grpc/support/time.h>
#include <grpc++/time.h>
#include <gtest/gtest.h>
using std::chrono::duration_cast;

Loading…
Cancel
Save