Commit aa568e0e authored by Leonid Baraz's avatar Leonid Baraz Committed by Commit Bot

Refactor tests to use TestEvent

Bug: None
Change-Id: Ic04c54b9b5db82f9c17d5abd2b4e79b2b9e15dcc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461748
Commit-Queue: Leonid Baraz <lbaraz@chromium.org>
Auto-Submit: Leonid Baraz <lbaraz@chromium.org>
Reviewed-by: default avatarZach Trudo <zatrudo@google.com>
Cr-Commit-Position: refs/heads/master@{#815422}
parent b4b03cb8
...@@ -29,52 +29,35 @@ namespace { ...@@ -29,52 +29,35 @@ namespace {
// //
// TestEvent<ResType> e; // TestEvent<ResType> e;
// ... Do some async work passing e.cb() as a completion callback of // ... Do some async work passing e.cb() as a completion callback of
// base::OnceCallback<void(ResType* res)> type which also may perform // base::OnceCallback<void(ResType* res)> type which also may perform some
// some other action specified by |done| callback provided by the caller. // other action specified by |done| callback provided by the caller.
// ... = e.result(); // Will wait for e.cb() to be called and return the // ... = e.result(); // Will wait for e.cb() to be called and return the
// // collected result. // collected result.
//
// Or, when the callback is not expected to be invoked:
//
// TestEvent<ResType> e(/*expected_to_complete=*/false);
// ... Start work passing e.cb() as a completion callback,
// which will not happen.
// //
template <typename ResType> template <typename ResType>
class TestEvent { class TestEvent {
public: public:
explicit TestEvent(bool expected_to_complete = true) TestEvent() : run_loop_(std::make_unique<base::RunLoop>()) {}
: expected_to_complete_(expected_to_complete), ~TestEvent() { EXPECT_FALSE(run_loop_->running()) << "Not responded"; }
completed_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
~TestEvent() {
if (expected_to_complete_) {
EXPECT_TRUE(completed_.IsSignaled()) << "Not responded";
} else {
EXPECT_FALSE(completed_.IsSignaled()) << "Responded";
}
}
TestEvent(const TestEvent& other) = delete; TestEvent(const TestEvent& other) = delete;
TestEvent& operator=(const TestEvent& other) = delete; TestEvent& operator=(const TestEvent& other) = delete;
ResType result() { ResType result() {
completed_.Wait(); run_loop_->Run();
return std::forward<ResType>(result_); return std::forward<ResType>(result_);
} }
// Completion callback to hand over to the processing method. // Completion callback to hand over to the processing method.
base::OnceCallback<void(ResType res)> cb() { base::OnceCallback<void(ResType res)> cb() {
DCHECK(!completed_.IsSignaled());
return base::BindOnce( return base::BindOnce(
[](base::WaitableEvent* completed, ResType* result, ResType res) { [](base::RunLoop* run_loop, ResType* result, ResType res) {
*result = std::forward<ResType>(res); *result = std::forward<ResType>(res);
completed->Signal(); run_loop->Quit();
}, },
base::Unretained(&completed_), base::Unretained(&result_)); base::Unretained(run_loop_.get()), base::Unretained(&result_));
} }
private: private:
bool expected_to_complete_; std::unique_ptr<base::RunLoop> run_loop_;
base::WaitableEvent completed_;
ResType result_; ResType result_;
}; };
......
...@@ -28,52 +28,35 @@ namespace { ...@@ -28,52 +28,35 @@ namespace {
// //
// TestEvent<ResType> e; // TestEvent<ResType> e;
// ... Do some async work passing e.cb() as a completion callback of // ... Do some async work passing e.cb() as a completion callback of
// base::OnceCallback<void(ResType* res)> type which also may perform // base::OnceCallback<void(ResType* res)> type which also may perform some
// some other action specified by |done| callback provided by the caller. // other action specified by |done| callback provided by the caller.
// ... = e.result(); // Will wait for e.cb() to be called and return the // ... = e.result(); // Will wait for e.cb() to be called and return the
// // collected result. // collected result.
//
// Or, when the callback is not expected to be invoked:
//
// TestEvent<ResType> e(/*expected_to_complete=*/false);
// ... Start work passing e.cb() as a completion callback,
// which will not happen.
// //
template <typename ResType> template <typename ResType>
class TestEvent { class TestEvent {
public: public:
explicit TestEvent(bool expected_to_complete = true) TestEvent() : run_loop_(std::make_unique<base::RunLoop>()) {}
: expected_to_complete_(expected_to_complete), ~TestEvent() { EXPECT_FALSE(run_loop_->running()) << "Not responded"; }
completed_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
~TestEvent() {
if (expected_to_complete_) {
EXPECT_TRUE(completed_.IsSignaled()) << "Not responded";
} else {
EXPECT_FALSE(completed_.IsSignaled()) << "Responded";
}
}
TestEvent(const TestEvent& other) = delete; TestEvent(const TestEvent& other) = delete;
TestEvent& operator=(const TestEvent& other) = delete; TestEvent& operator=(const TestEvent& other) = delete;
ResType result() { ResType result() {
completed_.Wait(); run_loop_->Run();
return std::forward<ResType>(result_); return std::forward<ResType>(result_);
} }
// Completion callback to hand over to the processing method. // Completion callback to hand over to the processing method.
base::OnceCallback<void(ResType res)> cb() { base::OnceCallback<void(ResType res)> cb() {
DCHECK(!completed_.IsSignaled());
return base::BindOnce( return base::BindOnce(
[](base::WaitableEvent* completed, ResType* result, ResType res) { [](base::RunLoop* run_loop, ResType* result, ResType res) {
*result = std::forward<ResType>(res); *result = std::forward<ResType>(res);
completed->Signal(); run_loop->Quit();
}, },
base::Unretained(&completed_), base::Unretained(&result_)); base::Unretained(run_loop_.get()), base::Unretained(&result_));
} }
private: private:
bool expected_to_complete_; std::unique_ptr<base::RunLoop> run_loop_;
base::WaitableEvent completed_;
ResType result_; ResType result_;
}; };
......
...@@ -1012,7 +1012,7 @@ class StorageQueue::ConfirmContext : public TaskRunnerContext<Status> { ...@@ -1012,7 +1012,7 @@ class StorageQueue::ConfirmContext : public TaskRunnerContext<Status> {
void OnStart() override { void OnStart() override {
DCHECK_CALLED_ON_VALID_SEQUENCE(confirm_sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(confirm_sequence_checker_);
Response(storage_queue_->RemoveUnusedFiles(seq_number_)); Response(storage_queue_->RemoveConfirmedData(seq_number_));
} }
// Confirmed sequencing number. // Confirmed sequencing number.
...@@ -1028,7 +1028,7 @@ void StorageQueue::Confirm(uint64_t seq_number, ...@@ -1028,7 +1028,7 @@ void StorageQueue::Confirm(uint64_t seq_number,
Start<ConfirmContext>(seq_number, std::move(completion_cb), this); Start<ConfirmContext>(seq_number, std::move(completion_cb), this);
} }
Status StorageQueue::RemoveUnusedFiles(uint64_t seq_number) { Status StorageQueue::RemoveConfirmedData(uint64_t seq_number) {
DCHECK_CALLED_ON_VALID_SEQUENCE(storage_queue_sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(storage_queue_sequence_checker_);
if (first_seq_number_ <= seq_number) { if (first_seq_number_ <= seq_number) {
first_seq_number_ = seq_number + 1; first_seq_number_ = seq_number + 1;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file.h" #include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
...@@ -146,7 +147,7 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> { ...@@ -146,7 +147,7 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> {
// Confirms acceptance of the records up to |seq_number| (inclusively). // Confirms acceptance of the records up to |seq_number| (inclusively).
// All records with sequencing numbers <= this one can be removed from // All records with sequencing numbers <= this one can be removed from
// the StorageQueue, and can no longer be uploaded. // the StorageQueue, and can no longer be uploaded.
// Helper methods: RemoveUnusedFiles. // Helper methods: RemoveConfirmedData.
void Confirm(uint64_t seq_number, void Confirm(uint64_t seq_number,
base::OnceCallback<void(Status)> completion_cb); base::OnceCallback<void(Status)> completion_cb);
...@@ -256,6 +257,13 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> { ...@@ -256,6 +257,13 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> {
// as the last one for the next record. // as the last one for the next record.
void UpdateRecordDigest(WrappedRecord* wrapped_record); void UpdateRecordDigest(WrappedRecord* wrapped_record);
// Helper method for Init(): process single data file.
// Return seq_number from <prefix>.<seq_number> file name, or Status
// in case there is any error.
StatusOr<uint64_t> AddDataFile(
const base::FilePath& full_name,
const base::FileEnumerator::FileInfo& file_info);
// Helper method for Init(): enumerates all data files in the directory. // Helper method for Init(): enumerates all data files in the directory.
// Valid file names are <prefix>.<seq_number>, any other names are ignored. // Valid file names are <prefix>.<seq_number>, any other names are ignored.
Status EnumerateDataFiles(); Status EnumerateDataFiles();
...@@ -312,9 +320,10 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> { ...@@ -312,9 +320,10 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> {
uint64_t seq_number, uint64_t seq_number,
std::vector<scoped_refptr<SingleFile>>* files) const; std::vector<scoped_refptr<SingleFile>>* files) const;
// Helper method for Confirm: Removes files that only have records with seq // Helper method for Confirm: Moves |first_seq_number_| to (|seq_number|+1)
// numbers below or equal to |seq_number|. // and removes files that only have records with seq numbers below or equal to
Status RemoveUnusedFiles(uint64_t seq_number); // |seq_number| (below |first_seq_number_|).
Status RemoveConfirmedData(uint64_t seq_number);
// Immutable options, stored at the time of creation. // Immutable options, stored at the time of creation.
const Options options_; const Options options_;
...@@ -332,8 +341,8 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> { ...@@ -332,8 +341,8 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> {
// Next sequencing number to store (not assigned yet). // Next sequencing number to store (not assigned yet).
uint64_t next_seq_number_ = 0; uint64_t next_seq_number_ = 0;
// First unconfirmed sequencing number (no records with lower // First sequencing number store still has (no records with lower
// sequencing number are guaranteed to exist in store). // sequencing number exist in store).
uint64_t first_seq_number_ = 0; uint64_t first_seq_number_ = 0;
// Ordered map of the files by ascending sequence number. // Ordered map of the files by ascending sequence number.
......
...@@ -47,30 +47,27 @@ namespace { ...@@ -47,30 +47,27 @@ namespace {
template <typename ResType> template <typename ResType>
class TestEvent { class TestEvent {
public: public:
TestEvent() TestEvent() : run_loop_(std::make_unique<base::RunLoop>()) {}
: completed_(base::WaitableEvent::ResetPolicy::MANUAL, ~TestEvent() { EXPECT_FALSE(run_loop_->running()) << "Not responded"; }
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
~TestEvent() { EXPECT_TRUE(completed_.IsSignaled()) << "Not responded"; }
TestEvent(const TestEvent& other) = delete; TestEvent(const TestEvent& other) = delete;
TestEvent& operator=(const TestEvent& other) = delete; TestEvent& operator=(const TestEvent& other) = delete;
ResType result() { ResType result() {
completed_.Wait(); run_loop_->Run();
return std::forward<ResType>(result_); return std::forward<ResType>(result_);
} }
// Completion callback to hand over to the processing method. // Completion callback to hand over to the processing method.
base::OnceCallback<void(ResType res)> cb() { base::OnceCallback<void(ResType res)> cb() {
DCHECK(!completed_.IsSignaled());
return base::BindOnce( return base::BindOnce(
[](base::WaitableEvent* completed, ResType* result, ResType res) { [](base::RunLoop* run_loop, ResType* result, ResType res) {
*result = std::forward<ResType>(res); *result = std::forward<ResType>(res);
completed->Signal(); run_loop->Quit();
}, },
base::Unretained(&completed_), base::Unretained(&result_)); base::Unretained(run_loop_.get()), base::Unretained(&result_));
} }
private: private:
base::WaitableEvent completed_; std::unique_ptr<base::RunLoop> run_loop_;
ResType result_; ResType result_;
}; };
......
...@@ -49,30 +49,27 @@ namespace { ...@@ -49,30 +49,27 @@ namespace {
template <typename ResType> template <typename ResType>
class TestEvent { class TestEvent {
public: public:
TestEvent() TestEvent() : run_loop_(std::make_unique<base::RunLoop>()) {}
: completed_(base::WaitableEvent::ResetPolicy::MANUAL, ~TestEvent() { EXPECT_FALSE(run_loop_->running()) << "Not responded"; }
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
~TestEvent() { EXPECT_TRUE(completed_.IsSignaled()) << "Not responded"; }
TestEvent(const TestEvent& other) = delete; TestEvent(const TestEvent& other) = delete;
TestEvent& operator=(const TestEvent& other) = delete; TestEvent& operator=(const TestEvent& other) = delete;
ResType result() { ResType result() {
completed_.Wait(); run_loop_->Run();
return std::forward<ResType>(result_); return std::forward<ResType>(result_);
} }
// Completion callback to hand over to the processing method. // Completion callback to hand over to the processing method.
base::OnceCallback<void(ResType res)> cb() { base::OnceCallback<void(ResType res)> cb() {
DCHECK(!completed_.IsSignaled());
return base::BindOnce( return base::BindOnce(
[](base::WaitableEvent* completed, ResType* result, ResType res) { [](base::RunLoop* run_loop, ResType* result, ResType res) {
*result = std::forward<ResType>(res); *result = std::forward<ResType>(res);
completed->Signal(); run_loop->Quit();
}, },
base::Unretained(&completed_), base::Unretained(&result_)); base::Unretained(run_loop_.get()), base::Unretained(&result_));
} }
private: private:
base::WaitableEvent completed_; std::unique_ptr<base::RunLoop> run_loop_;
ResType result_; ResType result_;
}; };
......
...@@ -90,10 +90,16 @@ message SequencingInformation { ...@@ -90,10 +90,16 @@ message SequencingInformation {
// |WrappedRecord| as it is stored on disc, and sent to the server. // |WrappedRecord| as it is stored on disc, and sent to the server.
message EncryptedRecord { message EncryptedRecord {
// Encrypted Wrapped Record (required) // Encrypted Wrapped Record
// |WrappedRecord| encrypted with the |encryption_key| in |encryption_info|. // |WrappedRecord| encrypted with the |encryption_key| in |encryption_info|.
// When absent, indicates gap - respective record is irreparably corrupt or
// missing from Storage, and server side should log it as such and no longer
// expect client to deliver it.
optional bytes encrypted_wrapped_record = 1; optional bytes encrypted_wrapped_record = 1;
// Must be present to facilitate decryption of encrypted record.
// If missing, the record is either not encrypted or missing.
// TODO(b/153651358): Disable an option to send record not encrypted.
optional EncryptionInfo encryption_info = 2; optional EncryptionInfo encryption_info = 2;
optional SequencingInformation sequencing_information = 3; optional SequencingInformation sequencing_information = 3;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment