OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_TEST_TOOLS_SIMULATOR_SIMULATOR_H_ | 5 #ifndef NET_QUIC_TEST_TOOLS_SIMULATOR_SIMULATOR_H_ |
6 #define NET_QUIC_TEST_TOOLS_SIMULATOR_SIMULATOR_H_ | 6 #define NET_QUIC_TEST_TOOLS_SIMULATOR_SIMULATOR_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <unordered_map> | 9 #include <unordered_map> |
10 #include <unordered_set> | 10 #include <unordered_set> |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // For each actor, maintain the time it is scheduled at. The value for | 128 // For each actor, maintain the time it is scheduled at. The value for |
129 // unscheduled actors is QuicTime::Infinite(). | 129 // unscheduled actors is QuicTime::Infinite(). |
130 std::unordered_map<Actor*, QuicTime> scheduled_times_; | 130 std::unordered_map<Actor*, QuicTime> scheduled_times_; |
131 std::unordered_set<std::string> actor_names_; | 131 std::unordered_set<std::string> actor_names_; |
132 | 132 |
133 DISALLOW_COPY_AND_ASSIGN(Simulator); | 133 DISALLOW_COPY_AND_ASSIGN(Simulator); |
134 }; | 134 }; |
135 | 135 |
136 template <class TerminationPredicate> | 136 template <class TerminationPredicate> |
137 bool Simulator::RunUntil(TerminationPredicate termination_predicate) { | 137 bool Simulator::RunUntil(TerminationPredicate termination_predicate) { |
138 while (!schedule_.empty()) { | 138 bool predicate_value = false; |
139 if (termination_predicate()) { | 139 while (true) { |
140 return true; | 140 predicate_value = termination_predicate(); |
| 141 if (predicate_value || schedule_.empty()) { |
| 142 break; |
141 } | 143 } |
142 HandleNextScheduledActor(); | 144 HandleNextScheduledActor(); |
143 } | 145 } |
144 return false; | 146 return predicate_value; |
145 } | 147 } |
146 | 148 |
147 template <class TerminationPredicate> | 149 template <class TerminationPredicate> |
148 bool Simulator::RunUntilOrTimeout(TerminationPredicate termination_predicate, | 150 bool Simulator::RunUntilOrTimeout(TerminationPredicate termination_predicate, |
149 QuicTime::Delta timeout) { | 151 QuicTime::Delta timeout) { |
150 QuicTime end_time = clock_.Now() + timeout; | 152 QuicTime end_time = clock_.Now() + timeout; |
151 bool return_value = RunUntil([end_time, &termination_predicate, this]() { | 153 bool return_value = RunUntil([end_time, &termination_predicate, this]() { |
152 return termination_predicate() || clock_.Now() >= end_time; | 154 return termination_predicate() || clock_.Now() >= end_time; |
153 }); | 155 }); |
154 | 156 |
155 if (clock_.Now() >= end_time) { | 157 if (clock_.Now() >= end_time) { |
156 return false; | 158 return false; |
157 } | 159 } |
158 return return_value; | 160 return return_value; |
159 } | 161 } |
160 | 162 |
161 } // namespace simulator | 163 } // namespace simulator |
162 } // namespace net | 164 } // namespace net |
163 | 165 |
164 #endif // NET_QUIC_TEST_TOOLS_SIMULATOR_SIMULATOR_H_ | 166 #endif // NET_QUIC_TEST_TOOLS_SIMULATOR_SIMULATOR_H_ |
OLD | NEW |