aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexei Frolov <frolv@google.com>2023-11-10 01:15:45 +0000
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-11-10 01:15:45 +0000
commit563393f56a8c41b03aed3e99e37993fb1c5c0dc8 (patch)
treefdab619d9107d89c3a62ea8c5aad7877126c4069
parent83707d83614162fa1cb9085abf3078dab59d94e3 (diff)
downloadpigweed-563393f56a8c41b03aed3e99e37993fb1c5c0dc8.tar.gz
pw_transfer: Prevent accidental timeouts in unit tests
Change-Id: Ib41c5973dc1a7d01a2dd278df6843e740d6d1a95 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/180274 Commit-Queue: Alexei Frolov <frolv@google.com> Reviewed-by: Ted Pudlik <tpudlik@google.com> Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
-rw-r--r--pw_transfer/client_test.cc23
-rw-r--r--pw_transfer/transfer_test.cc7
2 files changed, 17 insertions, 13 deletions
diff --git a/pw_transfer/client_test.cc b/pw_transfer/client_test.cc
index 0340ac0c0..d1cf3229f 100644
--- a/pw_transfer/client_test.cc
+++ b/pw_transfer/client_test.cc
@@ -21,7 +21,6 @@
#include "pw_bytes/array.h"
#include "pw_rpc/raw/client_testing.h"
#include "pw_rpc/test_helpers.h"
-#include "pw_thread/sleep.h"
#include "pw_thread/thread.h"
#include "pw_thread_stl/options.h"
#include "pw_transfer_private/chunk_testing.h"
@@ -611,8 +610,8 @@ TEST_F(ReadTransfer, ResendsParametersIfSentRepeatedChunkDuringRecovery) {
EXPECT_EQ(transfer_status, OkStatus());
}
-constexpr chrono::SystemClock::duration kTestTimeout =
- std::chrono::milliseconds(50);
+// Use a long timeout to avoid accidentally triggering timeouts.
+constexpr chrono::SystemClock::duration kTestTimeout = std::chrono::seconds(30);
constexpr uint8_t kTestRetries = 3;
TEST_F(ReadTransfer, Timeout_ResendsCurrentParameters) {
@@ -780,7 +779,7 @@ TEST_F(ReadTransfer, Timeout_EndsTransferAfterMaxRetries) {
EXPECT_EQ(transfer_status, Status::Unknown());
}
- // Sleep one more time after the final retry. The client should cancel the
+ // Time out one more time after the final retry. The client should cancel the
// transfer at this point. As no packets were received from the server, no
// final status chunk should be sent.
transfer_thread_.SimulateClientTimeout(14);
@@ -788,9 +787,10 @@ TEST_F(ReadTransfer, Timeout_EndsTransferAfterMaxRetries) {
EXPECT_EQ(transfer_status, Status::DeadlineExceeded());
- // After finishing the transfer, nothing else should be sent. Verify this by
- // waiting for a bit.
- this_thread::sleep_for(kTestTimeout * 4);
+ // After finishing the transfer, nothing else should be sent.
+ transfer_thread_.SimulateClientTimeout(14);
+ transfer_thread_.SimulateClientTimeout(14);
+ transfer_thread_.SimulateClientTimeout(14);
ASSERT_EQ(payloads.size(), 4u);
}
@@ -1466,7 +1466,7 @@ TEST_F(WriteTransfer, Timeout_EndsTransferAfterMaxRetries) {
EXPECT_EQ(transfer_status, Status::Unknown());
}
- // Sleep one more time after the final retry. The client should cancel the
+ // Time out one more time after the final retry. The client should cancel the
// transfer at this point. As no packets were received from the server, no
// final status chunk should be sent.
transfer_thread_.SimulateClientTimeout(13);
@@ -1474,9 +1474,10 @@ TEST_F(WriteTransfer, Timeout_EndsTransferAfterMaxRetries) {
EXPECT_EQ(transfer_status, Status::DeadlineExceeded());
- // After finishing the transfer, nothing else should be sent. Verify this by
- // waiting for a bit.
- this_thread::sleep_for(kTestTimeout * 4);
+ // After finishing the transfer, nothing else should be sent.
+ transfer_thread_.SimulateClientTimeout(13);
+ transfer_thread_.SimulateClientTimeout(13);
+ transfer_thread_.SimulateClientTimeout(13);
ASSERT_EQ(payloads.size(), 4u);
// Ensure we don't leave a dangling reference to transfer_status.
diff --git a/pw_transfer/transfer_test.cc b/pw_transfer/transfer_test.cc
index 1ba291baa..10ca9c529 100644
--- a/pw_transfer/transfer_test.cc
+++ b/pw_transfer/transfer_test.cc
@@ -1,4 +1,4 @@
-// Copyright 2022 The Pigweed Authors
+// Copyright 2023 The Pigweed 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
@@ -114,7 +114,10 @@ class ReadTransfer : public ::testing::Test {
: handler_(3, kData),
transfer_thread_(span(data_buffer_).first(max_chunk_size_bytes),
encode_buffer_),
- ctx_(transfer_thread_, 64),
+ ctx_(transfer_thread_,
+ 64,
+ // Use a long timeout to avoid accidentally triggering timeouts.
+ std::chrono::minutes(1)),
system_thread_(TransferThreadOptions(), transfer_thread_) {
ctx_.service().RegisterHandler(handler_);