Commit f0debb97 authored by Kalvin Lee's avatar Kalvin Lee Committed by Commit Bot

PpdProvider v3: augment testing fake

This change implements
FakePrinterConfigCache::ConsumeFetchRequestFor(), which allows us to
write tests in which many things happen on-sequence before the server
has a chance to respond to an outstanding Fetch().

Bug: chromium:888189
Test: autoninja ... chrome{,os_unittests}
Change-Id: I747e43e3765ca2862a7274f9356ef95b76b99df6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2303042
Commit-Queue: Kalvin Lee <kdlee@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791389}
parent 381ddb37
......@@ -22,6 +22,15 @@ void FakePrinterConfigCache::SetFetchResponseForTesting(
base::StringPiece key,
base::StringPiece value) {
contents_.insert_or_assign(std::string(key), std::string(value));
// If Fetch(|key|) was previously being consumed by prior call to
// DiscardFetchRequestFor(), we unblock it now.
fetch_requests_to_ignore_.erase(key);
}
void FakePrinterConfigCache::DiscardFetchRequestFor(base::StringPiece key) {
fetch_requests_to_ignore_.insert(std::string(key));
contents_.erase(key);
}
void FakePrinterConfigCache::Fetch(const std::string& key,
......@@ -31,12 +40,20 @@ void FakePrinterConfigCache::Fetch(const std::string& key,
std::move(cb).Run(PrinterConfigCache::FetchResult::Success(
key, contents_.at(key), base::Time()));
return;
} else if (fetch_requests_to_ignore_.contains(key)) {
// Caller has directed us, by way of DiscardFetchRequestFor(), to
// _not_ respond to this Fetch().
return;
}
std::move(cb).Run(PrinterConfigCache::FetchResult::Failure(key));
}
void FakePrinterConfigCache::Drop(const std::string& key) {
contents_.erase(key);
// If Fetch(|key|) was previously being consumed by prior call to
// DiscardFetchRequestFor(), we unblock it now.
fetch_requests_to_ignore_.erase(key);
}
} // namespace chromeos
......@@ -8,6 +8,7 @@
#include <string>
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/strings/string_piece.h"
#include "base/time/clock.h"
#include "chromeos/printing/printer_config_cache.h"
......@@ -40,8 +41,19 @@ class CHROMEOS_EXPORT FakePrinterConfigCache : public PrinterConfigCache {
void SetFetchResponseForTesting(base::StringPiece key,
base::StringPiece value);
// Sets internal state of |this| s.t. future Fetch() calls for
// |key| are consumed (i.e. delayed indefinitely and never called
// back). The effects of this are undone by a subsequent call to
// SetFetchResponseForTesting() or to Drop().
//
// This method is useful for simulating a slow server: one that
// doesn't immediately respond to a Fetch() request (in fact, it
// never responds at all, so use this carefully).
void DiscardFetchRequestFor(base::StringPiece key);
private:
base::flat_map<std::string, std::string> contents_;
base::flat_set<std::string> fetch_requests_to_ignore_;
};
} // namespace chromeos
......
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