Add timestamp when socket times out

Merge from Widevine repo of http://go/wvgerrit/123407
and http://go/wvgerrit/130983

This should help us debug flaky tests.

Bug: 186031735
Change-Id: I5229176c80dad5875b6ef01ea167e4e26d48506d
This commit is contained in:
Fred Gylys-Colwell
2021-10-13 22:06:56 +00:00
parent c7e237eb00
commit 1a93b9f1cc
4 changed files with 114 additions and 50 deletions

View File

@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <ctime>
#include <string>
#include <gtest/gtest_prod.h>
@@ -23,7 +24,7 @@ class HttpSocket {
explicit HttpSocket(const std::string& url);
~HttpSocket();
bool Connect(int timeout_in_ms);
bool ConnectAndLogErrors(int timeout_in_ms);
void CloseSocket();
const std::string& scheme() const { return scheme_; }
@@ -32,13 +33,25 @@ class HttpSocket {
int port() const { return atoi(port_.c_str()); }
const std::string& resource_path() const { return resource_path_; }
int Read(char* data, int len, int timeout_in_ms);
int Write(const char* data, int len, int timeout_in_ms);
int ReadAndLogErrors(char* data, int len, int timeout_in_ms);
int WriteAndLogErrors(const char* data, int len, int timeout_in_ms);
private:
static bool ParseUrl(const std::string& url, std::string* scheme,
bool* secure_connect, std::string* domain_name,
std::string* port, std::string* path);
// The following three functions do the work without logging errors.
bool Connect(int timeout_in_ms);
int Read(char* data, int len, int timeout_in_ms);
int Write(const char* data, int len, int timeout_in_ms);
// Log times with a note as an error.
void LogTime(const char* note, const std::time_t& start,
const std::time_t& finish);
// Wait for a socket to be ready for reading or writing.
// Establishing a connection counts as "ready for write".
// Returns false on select error or timeout.
// Returns true when the socket is ready.
bool Wait(bool for_read, int timeout_in_ms);
FRIEND_TEST(HttpSocketTest, ParseUrlTest);
std::string scheme_;
@@ -52,6 +65,10 @@ class HttpSocket {
SSL* ssl_;
SSL_CTX* ssl_ctx_;
// When the socket was created. Logged on error to help debug flaky
// tests. e.g. b/186031735
std::time_t create_time_;
CORE_DISALLOW_COPY_AND_ASSIGN(HttpSocket);
};