1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
6 #define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/timer/timer.h"
13 #include "build/build_config.h"
15 // Re-creates a given test file inside the cache test folder.
16 bool CreateCacheTestFile(const base::FilePath
& name
);
18 // Deletes all file son the cache.
19 bool DeleteCache(const base::FilePath
& path
);
21 // Fills buffer with random values (may contain nulls unless no_nulls is true).
22 void CacheTestFillBuffer(char* buffer
, size_t len
, bool no_nulls
);
24 // Generates a random key of up to 200 bytes.
25 std::string
GenerateKey(bool same_length
);
27 // Returns true if the cache is not corrupt.
28 bool CheckCacheIntegrity(const base::FilePath
& path
, bool new_eviction
,
31 // -----------------------------------------------------------------------
33 // Simple helper to deal with the message loop on a test.
34 class MessageLoopHelper
{
39 // Run the message loop and wait for num_callbacks before returning. Returns
40 // false if we are waiting to long. Each callback that will be waited on is
41 // required to call CallbackWasCalled() to indicate when it was called.
42 bool WaitUntilCacheIoFinished(int num_callbacks
);
44 // True if a given callback was called more times than it expected.
45 bool callback_reused_error() const { return callback_reused_error_
; }
46 void set_callback_reused_error(bool error
) {
47 callback_reused_error_
= error
;
50 int callbacks_called() const { return callbacks_called_
; }
51 // Report that a callback was called. Each callback that will be waited on
52 // via WaitUntilCacheIoFinished() is expected to call this method to
53 // indicate when it has been executed.
54 void CallbackWasCalled() { ++callbacks_called_
; }
57 // Sets the number of callbacks that can be received so far.
58 void ExpectCallbacks(int num_callbacks
) {
59 num_callbacks_
= num_callbacks
;
60 num_iterations_
= last_
= 0;
64 // Called periodically to test if WaitUntilCacheIoFinished should return.
67 base::RepeatingTimer
<MessageLoopHelper
> timer_
;
73 // True if a callback was called/reused more than expected.
74 bool callback_reused_error_
;
75 int callbacks_called_
;
77 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper
);
80 // -----------------------------------------------------------------------
82 // Simple callback to process IO completions from the cache. It allows tests
83 // with multiple simultaneous IO operations.
86 // Creates a new CallbackTest object. When the callback is called, it will
87 // update |helper|. If |reuse| is false and a callback is called more than
88 // once, or if |reuse| is true and a callback is called more than twice, an
89 // error will be reported to |helper|.
90 CallbackTest(MessageLoopHelper
* helper
, bool reuse
);
95 int last_result() const { return last_result_
; }
98 MessageLoopHelper
* helper_
;
101 DISALLOW_COPY_AND_ASSIGN(CallbackTest
);
104 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_