mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
6.7 KiB
195 lines
6.7 KiB
3 years ago
|
// Copyright 2016 gRPC authors.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
2 years ago
|
#ifndef GRPC_TEST_CPP_END2END_CONNECTION_ATTEMPT_INJECTOR_H
|
||
|
#define GRPC_TEST_CPP_END2END_CONNECTION_ATTEMPT_INJECTOR_H
|
||
3 years ago
|
|
||
|
#include <memory>
|
||
|
|
||
2 years ago
|
#include "src/core/lib/channel/channel_args.h"
|
||
3 years ago
|
#include "src/core/lib/gprpp/time.h"
|
||
3 years ago
|
#include "src/core/lib/iomgr/tcp_client.h"
|
||
|
#include "src/core/lib/iomgr/timer.h"
|
||
3 years ago
|
|
||
|
namespace grpc {
|
||
|
namespace testing {
|
||
|
|
||
|
// Allows injecting connection-establishment delays into C-core.
|
||
|
// Typical usage:
|
||
|
//
|
||
3 years ago
|
// // At grpc_init() time.
|
||
|
// ConnectionAttemptInjector::Init();
|
||
3 years ago
|
//
|
||
2 years ago
|
// // Instantiate when injection is desired.
|
||
|
// ConnectionAttemptInjector injector;
|
||
|
//
|
||
|
// // To inject a hold for the next connection attempt for a given port.
|
||
|
// auto hold = injector.AddHold(port);
|
||
|
// hold.Wait();
|
||
|
// // ...do stuff...
|
||
|
// hold.Resume(); // Or hold.Fail() if you want to force a failure.
|
||
|
//
|
||
|
// // Inject a fixed delay for all connection attempts.
|
||
|
// injector.SetDelay(grpc_core::Duration::Seconds(10));
|
||
3 years ago
|
//
|
||
3 years ago
|
// The injection is global, so there must be only one ConnectionAttemptInjector
|
||
|
// object at any one time.
|
||
2 years ago
|
//
|
||
|
// Note: This must be "final" to avoid tsan problems in both the ctor
|
||
|
// and dtor related to initializing the vtable.
|
||
|
class ConnectionAttemptInjector final {
|
||
|
private:
|
||
|
// Forward declarations.
|
||
|
class QueuedAttempt;
|
||
|
|
||
|
grpc_core::Mutex mu_;
|
||
|
|
||
3 years ago
|
public:
|
||
2 years ago
|
class Hold {
|
||
|
public:
|
||
|
// Do not instantiate directly -- must be created via AddHold().
|
||
|
Hold(ConnectionAttemptInjector* injector, int port,
|
||
|
bool intercept_completion);
|
||
|
|
||
|
// Waits for the connection attempt to start.
|
||
|
// After this returns, exactly one of Resume() or Fail() must be called.
|
||
|
void Wait();
|
||
|
|
||
|
// Resumes a connection attempt. Must be called after Wait().
|
||
|
void Resume();
|
||
|
|
||
|
// Fails a connection attempt. Must be called after Wait().
|
||
|
void Fail(grpc_error_handle error);
|
||
|
|
||
|
// If the hold was created with intercept_completion=true, then this
|
||
|
// can be called after Resume() to wait for the connection attempt
|
||
|
// to complete.
|
||
|
void WaitForCompletion();
|
||
|
|
||
|
// Returns true if the connection attempt has been started.
|
||
|
bool IsStarted();
|
||
|
|
||
|
private:
|
||
|
friend class ConnectionAttemptInjector;
|
||
|
|
||
|
static void OnComplete(void* arg, grpc_error_handle error);
|
||
|
|
||
|
ConnectionAttemptInjector* injector_;
|
||
|
const int port_;
|
||
|
const bool intercept_completion_;
|
||
|
std::unique_ptr<QueuedAttempt> queued_attempt_
|
||
|
ABSL_GUARDED_BY(&ConnectionAttemptInjector::mu_);
|
||
|
grpc_core::CondVar start_cv_;
|
||
|
grpc_closure on_complete_;
|
||
|
grpc_closure* original_on_complete_;
|
||
|
grpc_core::CondVar complete_cv_;
|
||
|
};
|
||
|
|
||
3 years ago
|
// Global initializer. Replaces the iomgr TCP client vtable.
|
||
3 years ago
|
// Must be called exactly once after grpc_init() but before any TCP
|
||
|
// connections are established.
|
||
3 years ago
|
static void Init();
|
||
|
|
||
2 years ago
|
ConnectionAttemptInjector();
|
||
|
~ConnectionAttemptInjector();
|
||
3 years ago
|
|
||
2 years ago
|
// Adds a hold for a given port. The caller may then use Wait() on
|
||
|
// the resulting Hold object to wait for the connection attempt to start.
|
||
|
// If intercept_completion is true, the caller can use WaitForCompletion()
|
||
|
// on the resulting Hold object.
|
||
|
std::unique_ptr<Hold> AddHold(int port, bool intercept_completion = false);
|
||
3 years ago
|
|
||
2 years ago
|
// Set a fixed delay for all RPCs. Will be used only if there is no
|
||
|
// hold for the connection attempt.
|
||
|
void SetDelay(grpc_core::Duration delay);
|
||
|
|
||
|
private:
|
||
|
static grpc_tcp_client_vtable kDelayedConnectVTable;
|
||
3 years ago
|
|
||
|
// Represents a queued attempt.
|
||
|
// The caller must invoke either Resume() or Fail() before destroying.
|
||
|
class QueuedAttempt {
|
||
3 years ago
|
public:
|
||
3 years ago
|
QueuedAttempt(grpc_closure* closure, grpc_endpoint** ep,
|
||
|
grpc_pollset_set* interested_parties,
|
||
2 years ago
|
const grpc_channel_args* channel_args,
|
||
3 years ago
|
const grpc_resolved_address* addr,
|
||
2 years ago
|
grpc_core::Timestamp deadline);
|
||
|
~QueuedAttempt();
|
||
3 years ago
|
|
||
|
// Caller must invoke this from a thread with an ExecCtx.
|
||
2 years ago
|
void Resume();
|
||
3 years ago
|
|
||
|
// Caller must invoke this from a thread with an ExecCtx.
|
||
2 years ago
|
void Fail(grpc_error_handle error);
|
||
3 years ago
|
|
||
|
private:
|
||
|
grpc_closure* closure_;
|
||
|
grpc_endpoint** endpoint_;
|
||
|
grpc_pollset_set* interested_parties_;
|
||
2 years ago
|
const grpc_channel_args* channel_args_;
|
||
3 years ago
|
grpc_resolved_address address_;
|
||
|
grpc_core::Timestamp deadline_;
|
||
3 years ago
|
};
|
||
|
|
||
3 years ago
|
// Injects a delay before continuing a connection attempt.
|
||
|
class InjectedDelay {
|
||
|
public:
|
||
|
virtual ~InjectedDelay() = default;
|
||
|
|
||
|
InjectedDelay(grpc_core::Duration duration, grpc_closure* closure,
|
||
|
grpc_endpoint** ep, grpc_pollset_set* interested_parties,
|
||
2 years ago
|
const grpc_channel_args* channel_args,
|
||
3 years ago
|
const grpc_resolved_address* addr,
|
||
|
grpc_core::Timestamp deadline);
|
||
|
|
||
|
private:
|
||
|
static void TimerCallback(void* arg, grpc_error_handle /*error*/);
|
||
|
|
||
|
QueuedAttempt attempt_;
|
||
|
grpc_timer timer_;
|
||
|
grpc_closure timer_callback_;
|
||
|
};
|
||
|
|
||
2 years ago
|
// Invoked for every TCP connection attempt.
|
||
2 years ago
|
void HandleConnection(grpc_closure* closure, grpc_endpoint** ep,
|
||
|
grpc_pollset_set* interested_parties,
|
||
|
const grpc_channel_args* channel_args,
|
||
|
const grpc_resolved_address* addr,
|
||
2 years ago
|
grpc_core::Timestamp deadline);
|
||
2 years ago
|
|
||
2 years ago
|
static void AttemptConnection(grpc_closure* closure, grpc_endpoint** ep,
|
||
|
grpc_pollset_set* interested_parties,
|
||
|
const grpc_channel_args* channel_args,
|
||
|
const grpc_resolved_address* addr,
|
||
|
grpc_core::Timestamp deadline);
|
||
2 years ago
|
|
||
2 years ago
|
// Replacement iomgr tcp_connect vtable functions that use the current
|
||
|
// ConnectionAttemptInjector object.
|
||
|
static int64_t TcpConnect(grpc_closure* closure, grpc_endpoint** ep,
|
||
|
grpc_pollset_set* interested_parties,
|
||
|
const grpc_channel_args* channel_args,
|
||
|
const grpc_resolved_address* addr,
|
||
|
grpc_core::Timestamp deadline);
|
||
|
static bool TcpConnectCancel(int64_t connection_handle);
|
||
|
|
||
|
std::vector<Hold*> holds_ ABSL_GUARDED_BY(&mu_);
|
||
|
absl::optional<grpc_core::Duration> delay_ ABSL_GUARDED_BY(&mu_);
|
||
2 years ago
|
};
|
||
|
|
||
3 years ago
|
} // namespace testing
|
||
|
} // namespace grpc
|
||
|
|
||
2 years ago
|
#endif // GRPC_TEST_CPP_END2END_CONNECTION_ATTEMPT_INJECTOR_H
|