OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ | |
6 #define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ | |
7 | |
8 #include <string> | |
9 | |
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" | |
14 | |
15 // Re-creates a given test file inside the cache test folder. | |
16 bool CreateCacheTestFile(const base::FilePath& name); | |
17 | |
18 // Deletes all file son the cache. | |
19 bool DeleteCache(const base::FilePath& path); | |
20 | |
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); | |
23 | |
24 // Generates a random key of up to 200 bytes. | |
25 std::string GenerateKey(bool same_length); | |
26 | |
27 // Returns true if the cache is not corrupt. | |
28 bool CheckCacheIntegrity(const base::FilePath& path, bool new_eviction, | |
29 uint32 mask); | |
30 | |
31 // ----------------------------------------------------------------------- | |
32 | |
33 // Simple helper to deal with the message loop on a test. | |
34 class MessageLoopHelper { | |
35 public: | |
36 MessageLoopHelper(); | |
37 ~MessageLoopHelper(); | |
38 | |
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); | |
43 | |
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; | |
48 } | |
49 | |
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_; } | |
55 | |
56 private: | |
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; | |
61 completed_ = false; | |
62 } | |
63 | |
64 // Called periodically to test if WaitUntilCacheIoFinished should return. | |
65 void TimerExpired(); | |
66 | |
67 base::RepeatingTimer<MessageLoopHelper> timer_; | |
68 int num_callbacks_; | |
69 int num_iterations_; | |
70 int last_; | |
71 bool completed_; | |
72 | |
73 // True if a callback was called/reused more than expected. | |
74 bool callback_reused_error_; | |
75 int callbacks_called_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper); | |
78 }; | |
79 | |
80 // ----------------------------------------------------------------------- | |
81 | |
82 // Simple callback to process IO completions from the cache. It allows tests | |
83 // with multiple simultaneous IO operations. | |
84 class CallbackTest { | |
85 public: | |
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); | |
91 ~CallbackTest(); | |
92 | |
93 void Run(int result); | |
94 | |
95 int last_result() const { return last_result_; } | |
96 | |
97 private: | |
98 MessageLoopHelper* helper_; | |
99 int reuse_; | |
100 int last_result_; | |
101 DISALLOW_COPY_AND_ASSIGN(CallbackTest); | |
102 }; | |
103 | |
104 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ | |
OLD | NEW |