summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2023-05-12 20:48:30 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2023-05-13 16:42:04 +0100
commitfad90428970e332153027773b517a1606c0efa1f (patch)
tree499ad297bc61c02e7151aa76e0cf205142fa150d /tests
parent933b04c219b49d0f9cd779d024bfa39d806839ac (diff)
downloadlibgit2-fad90428970e332153027773b517a1606c0efa1f.tar.gz
streams: sockets are non-blocking and can timeout
Make socket I/O non-blocking and add optional timeouts. Users may now set `GIT_OPT_SET_SERVER_CONNECT_TIMEOUT` to set a shorter connection timeout. (The connect timeout cannot be longer than the operating system default.) Users may also now configure the socket read and write timeouts with `GIT_OPT_SET_SERVER_TIMEOUT`. By default, connects still timeout based on the operating system defaults (typically 75 seconds) and socket read and writes block. Add a test against our custom testing git server that ensures that we can timeout reads against a slow server.
Diffstat (limited to 'tests')
-rw-r--r--tests/libgit2/CMakeLists.txt2
-rw-r--r--tests/libgit2/online/clone.c60
2 files changed, 61 insertions, 1 deletions
diff --git a/tests/libgit2/CMakeLists.txt b/tests/libgit2/CMakeLists.txt
index 866122d1c..af70f55a7 100644
--- a/tests/libgit2/CMakeLists.txt
+++ b/tests/libgit2/CMakeLists.txt
@@ -65,7 +65,7 @@ endif()
include(AddClarTest)
add_clar_test(libgit2_tests offline -v -xonline)
-add_clar_test(libgit2_tests invasive -v -sfilter::stream::bigfile -sodb::largefiles -siterator::workdir::filesystem_gunk -srepo::init -srepo::init::at_filesystem_root)
+add_clar_test(libgit2_tests invasive -v -sfilter::stream::bigfile -sodb::largefiles -siterator::workdir::filesystem_gunk -srepo::init -srepo::init::at_filesystem_root -sonline::clone::connect_timeout_default)
add_clar_test(libgit2_tests online -v -sonline -xonline::customcert)
add_clar_test(libgit2_tests online_customcert -v -sonline::customcert)
add_clar_test(libgit2_tests gitdaemon -v -sonline::push)
diff --git a/tests/libgit2/online/clone.c b/tests/libgit2/online/clone.c
index b635739b6..92fa4d351 100644
--- a/tests/libgit2/online/clone.c
+++ b/tests/libgit2/online/clone.c
@@ -35,6 +35,8 @@ static char *_remote_proxy_selfsigned = NULL;
static char *_remote_expectcontinue = NULL;
static char *_remote_redirect_initial = NULL;
static char *_remote_redirect_subsequent = NULL;
+static char *_remote_speed_timesout = NULL;
+static char *_remote_speed_slow = NULL;
static char *_github_ssh_pubkey = NULL;
static char *_github_ssh_privkey = NULL;
@@ -89,6 +91,8 @@ void test_online_clone__initialize(void)
_remote_expectcontinue = cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE");
_remote_redirect_initial = cl_getenv("GITTEST_REMOTE_REDIRECT_INITIAL");
_remote_redirect_subsequent = cl_getenv("GITTEST_REMOTE_REDIRECT_SUBSEQUENT");
+ _remote_speed_timesout = cl_getenv("GITTEST_REMOTE_SPEED_TIMESOUT");
+ _remote_speed_slow = cl_getenv("GITTEST_REMOTE_SPEED_SLOW");
_github_ssh_pubkey = cl_getenv("GITTEST_GITHUB_SSH_PUBKEY");
_github_ssh_privkey = cl_getenv("GITTEST_GITHUB_SSH_KEY");
@@ -128,6 +132,8 @@ void test_online_clone__cleanup(void)
git__free(_remote_expectcontinue);
git__free(_remote_redirect_initial);
git__free(_remote_redirect_subsequent);
+ git__free(_remote_speed_timesout);
+ git__free(_remote_speed_slow);
git__free(_github_ssh_pubkey);
git__free(_github_ssh_privkey);
@@ -145,6 +151,8 @@ void test_online_clone__cleanup(void)
}
git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, NULL, NULL);
+ git_libgit2_opts(GIT_OPT_SET_SERVER_TIMEOUT, 0);
+ git_libgit2_opts(GIT_OPT_SET_SERVER_CONNECT_TIMEOUT, 0);
}
void test_online_clone__network_full(void)
@@ -1207,3 +1215,55 @@ void test_online_clone__sha256(void)
git_reference_free(head);
#endif
}
+
+void test_online_clone__connect_timeout_configurable(void)
+{
+ uint64_t start, finish;
+
+ start = git_time_monotonic();
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SERVER_CONNECT_TIMEOUT, 1));
+ cl_git_fail(git_clone(&g_repo, "http://www.google.com:8000/", "./timedout", NULL));
+ cl_assert(git_error_last() && strstr(git_error_last()->message, "timed out"));
+
+ finish = git_time_monotonic();
+
+ cl_assert(finish - start < 1000);
+}
+
+void test_online_clone__connect_timeout_default(void)
+{
+ /* This test takes ~ 75 seconds on Unix. */
+ if (!cl_is_env_set("GITTEST_INVASIVE_SPEED"))
+ cl_skip();
+
+ /*
+ * Use a host/port pair that blackholes packets and does not
+ * send an RST.
+ */
+ cl_git_fail_with(GIT_TIMEOUT, git_clone(&g_repo, "http://www.google.com:8000/", "./refused", NULL));
+ cl_assert(git_error_last() && strstr(git_error_last()->message, "timed out"));
+}
+
+void test_online_clone__timeout_configurable_times_out(void)
+{
+ git_repository *failed_repo;
+
+ if (!_remote_speed_timesout)
+ cl_skip();
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SERVER_TIMEOUT, 1000));
+
+ cl_git_fail_with(GIT_TIMEOUT, git_clone(&failed_repo, _remote_speed_timesout, "./timedout", NULL));
+ cl_assert(git_error_last() && strstr(git_error_last()->message, "timed out"));
+}
+
+void test_online_clone__timeout_configurable_succeeds_slowly(void)
+{
+ if (!_remote_speed_slow)
+ cl_skip();
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SERVER_TIMEOUT, 1000));
+
+ cl_git_pass(git_clone(&g_repo, _remote_speed_slow, "./slow-but-successful", NULL));
+}