summaryrefslogtreecommitdiffstats
path: root/content/renderer/scheduler/resource_dispatch_throttler_unittest.cc
blob: a1dbcac7076b45fde695105bc068a5bb6f60c0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/scheduler/resource_dispatch_throttler.h"

#include <stddef.h>
#include <stdint.h>

#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "content/common/resource_messages.h"
#include "content/test/fake_renderer_scheduler.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
namespace {

const uint32_t kRequestsPerFlush = 4;
const double kFlushPeriodSeconds = 1.f / 60;
const int kRoutingId = 1;

typedef ScopedVector<IPC::Message> ScopedMessages;

int GetRequestId(const IPC::Message& msg) {
  int request_id = -1;
  switch (msg.type()) {
    case ResourceHostMsg_RequestResource::ID: {
      base::PickleIterator iter(msg);
      int routing_id = -1;
      if (!iter.ReadInt(&routing_id) || !iter.ReadInt(&request_id))
        NOTREACHED() << "Invalid id for resource request message.";
    } break;

    case ResourceHostMsg_DidChangePriority::ID:
    case ResourceHostMsg_ReleaseDownloadedFile::ID:
    case ResourceHostMsg_CancelRequest::ID:
      if (!base::PickleIterator(msg).ReadInt(&request_id))
        NOTREACHED() << "Invalid id for resource message.";
      break;

    default:
      NOTREACHED() << "Invalid message for resource throttling.";
      break;
  }
  return request_id;
}

class RendererSchedulerForTest : public FakeRendererScheduler {
 public:
  RendererSchedulerForTest() : high_priority_work_anticipated_(false) {}
  ~RendererSchedulerForTest() override {}

  // RendererScheduler implementation:
  bool IsHighPriorityWorkAnticipated() override {
    return high_priority_work_anticipated_;
  }

  void set_high_priority_work_anticipated(bool anticipated) {
    high_priority_work_anticipated_ = anticipated;
  }

 private:
  bool high_priority_work_anticipated_;
};

}  // namespace

class ResourceDispatchThrottlerForTest : public ResourceDispatchThrottler {
 public:
  ResourceDispatchThrottlerForTest(IPC::Sender* sender,
                                   scheduler::RendererScheduler* scheduler)
      : ResourceDispatchThrottler(
            sender,
            scheduler,
            base::TimeDelta::FromSecondsD(kFlushPeriodSeconds),
            kRequestsPerFlush),
        flush_scheduled_(false) {}
  ~ResourceDispatchThrottlerForTest() override {}

  void Advance(base::TimeDelta delta) { now_ += delta; }

  bool RunScheduledFlush() {
    if (!flush_scheduled_)
      return false;

    flush_scheduled_ = false;
    Flush();
    return true;
  }

  bool flush_scheduled() const { return flush_scheduled_; }

 private:
  // ResourceDispatchThrottler overrides:
  base::TimeTicks Now() const override { return now_; }
  void ScheduleFlush() override { flush_scheduled_ = true; }

  base::TimeTicks now_;
  bool flush_scheduled_;
};

class ResourceDispatchThrottlerTest : public testing::Test, public IPC::Sender {
 public:
  ResourceDispatchThrottlerTest() : last_request_id_(0) {
    throttler_.reset(new ResourceDispatchThrottlerForTest(this, &scheduler_));
  }
  ~ResourceDispatchThrottlerTest() override {}

  // IPC::Sender implementation:
  bool Send(IPC::Message* msg) override {
    sent_messages_.push_back(msg);
    return true;
  }

 protected:
  void SetHighPriorityWorkAnticipated(bool anticipated) {
    scheduler_.set_high_priority_work_anticipated(anticipated);
  }

  void Advance(base::TimeDelta delta) { throttler_->Advance(delta); }

  bool RunScheduledFlush() { return throttler_->RunScheduledFlush(); }

  bool FlushScheduled() { return throttler_->flush_scheduled(); }

  bool RequestResource() {
    ResourceHostMsg_Request request;
    request.download_to_file = true;
    return throttler_->Send(new ResourceHostMsg_RequestResource(
        kRoutingId, ++last_request_id_, request));
  }

  bool RequestResourceSync() {
    SyncLoadResult result;
    return throttler_->Send(new ResourceHostMsg_SyncLoad(
        kRoutingId, ++last_request_id_, ResourceHostMsg_Request(), &result));
  }

  void RequestResourcesUntilThrottled() {
    SetHighPriorityWorkAnticipated(true);
    GetAndResetSentMessageCount();
    RequestResource();
    while (GetAndResetSentMessageCount())
      RequestResource();
  }

  bool UpdateRequestPriority(int request_id, net::RequestPriority priority) {
    return throttler_->Send(
        new ResourceHostMsg_DidChangePriority(request_id, priority, 0));
  }

  bool ReleaseDownloadedFile(int request_id) {
    return throttler_->Send(
        new ResourceHostMsg_ReleaseDownloadedFile(request_id));
  }

  bool CancelRequest(int request_id) {
    return throttler_->Send(new ResourceHostMsg_CancelRequest(request_id));
  }

  size_t GetAndResetSentMessageCount() {
    size_t sent_message_count = sent_messages_.size();
    sent_messages_.clear();
    return sent_message_count;
  }

  const IPC::Message* LastSentMessage() const {
    return sent_messages_.empty() ? nullptr : sent_messages_.back();
  }

  int LastSentRequestId() const {
    const IPC::Message* msg = LastSentMessage();
    if (!msg)
      return -1;

    int routing_id = -1;
    int request_id = -1;
    base::PickleIterator iter(*msg);
    CHECK(IPC::ReadParam(msg, &iter, &routing_id));
    CHECK(IPC::ReadParam(msg, &iter, &request_id));
    return request_id;
  }

  int last_request_id() const { return last_request_id_; }

  ScopedMessages sent_messages_;

 private:
  scoped_ptr<ResourceDispatchThrottlerForTest> throttler_;
  RendererSchedulerForTest scheduler_;
  int last_request_id_;
  bool flush_scheduled_;

  DISALLOW_COPY_AND_ASSIGN(ResourceDispatchThrottlerTest);
};

TEST_F(ResourceDispatchThrottlerTest, NotThrottledByDefault) {
  SetHighPriorityWorkAnticipated(false);
  for (size_t i = 0; i < kRequestsPerFlush * 2; ++i) {
    RequestResource();
    EXPECT_EQ(i + 1, sent_messages_.size());
  }
}

TEST_F(ResourceDispatchThrottlerTest, NotThrottledIfSendLimitNotReached) {
  SetHighPriorityWorkAnticipated(true);
  for (size_t i = 0; i < kRequestsPerFlush; ++i) {
    RequestResource();
    EXPECT_EQ(i + 1, sent_messages_.size());
  }
}

TEST_F(ResourceDispatchThrottlerTest, ThrottledWhenHighPriorityWork) {
  SetHighPriorityWorkAnticipated(true);
  for (size_t i = 0; i < kRequestsPerFlush; ++i)
    RequestResource();
  ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());

  RequestResource();
  EXPECT_EQ(0U, sent_messages_.size());

  EXPECT_TRUE(RunScheduledFlush());
  EXPECT_EQ(1U, sent_messages_.size());
}

TEST_F(ResourceDispatchThrottlerTest,
       ThrottledWhenDeferredMessageQueueNonEmpty) {
  SetHighPriorityWorkAnticipated(true);
  for (size_t i = 0; i < kRequestsPerFlush; ++i)
    RequestResource();
  ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());

  RequestResource();
  EXPECT_EQ(0U, sent_messages_.size());
  SetHighPriorityWorkAnticipated(false);
  RequestResource();
  EXPECT_EQ(0U, sent_messages_.size());

  EXPECT_TRUE(RunScheduledFlush());
  EXPECT_EQ(2U, sent_messages_.size());
}

TEST_F(ResourceDispatchThrottlerTest, NotThrottledIfSufficientTimePassed) {
  SetHighPriorityWorkAnticipated(true);

  for (size_t i = 0; i < kRequestsPerFlush * 2; ++i) {
    Advance(base::TimeDelta::FromSecondsD(kFlushPeriodSeconds * 2));
    RequestResource();
    EXPECT_EQ(1U, GetAndResetSentMessageCount());
    EXPECT_FALSE(FlushScheduled());
  }
}

TEST_F(ResourceDispatchThrottlerTest, NotThrottledIfSyncMessage) {
  SetHighPriorityWorkAnticipated(true);

  RequestResourceSync();
  EXPECT_EQ(1U, GetAndResetSentMessageCount());

  // Saturate the queue.
  for (size_t i = 0; i < kRequestsPerFlush * 2; ++i)
    RequestResource();
  ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());

  // Synchronous messages should flush any previously throttled messages.
  RequestResourceSync();
  EXPECT_EQ(1U + kRequestsPerFlush, GetAndResetSentMessageCount());
  RequestResourceSync();
  EXPECT_EQ(1U, GetAndResetSentMessageCount());

  // Previously throttled messages should already have been flushed.
  RunScheduledFlush();
  EXPECT_EQ(0U, GetAndResetSentMessageCount());
}

TEST_F(ResourceDispatchThrottlerTest, MultipleFlushes) {
  SetHighPriorityWorkAnticipated(true);
  for (size_t i = 0; i < kRequestsPerFlush * 4; ++i)
    RequestResource();
  ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());

  for (size_t i = 0; i < 3; ++i) {
    SCOPED_TRACE(i);
    EXPECT_TRUE(RunScheduledFlush());
    EXPECT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());
  }

  EXPECT_FALSE(FlushScheduled());
  EXPECT_EQ(0U, sent_messages_.size());
}

TEST_F(ResourceDispatchThrottlerTest, MultipleFlushesWhileReceiving) {
  SetHighPriorityWorkAnticipated(true);
  for (size_t i = 0; i < kRequestsPerFlush * 4; ++i)
    RequestResource();
  ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());

  for (size_t i = 0; i < 3; ++i) {
    SCOPED_TRACE(i);
    EXPECT_TRUE(RunScheduledFlush());
    EXPECT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());
    for (size_t j = 0; j < kRequestsPerFlush; ++j)
      RequestResource();
    EXPECT_EQ(0U, sent_messages_.size());
  }

  for (size_t i = 0; i < 3; ++i) {
    EXPECT_TRUE(RunScheduledFlush());
    EXPECT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());
  }

  EXPECT_FALSE(FlushScheduled());
  EXPECT_EQ(0U, sent_messages_.size());
}

TEST_F(ResourceDispatchThrottlerTest, NonRequestsNeverTriggerThrottling) {
  RequestResource();
  ASSERT_EQ(1U, GetAndResetSentMessageCount());

  for (size_t i = 0; i < kRequestsPerFlush * 3; ++i)
    UpdateRequestPriority(last_request_id(), net::HIGHEST);
  EXPECT_EQ(kRequestsPerFlush * 3, sent_messages_.size());

  RequestResource();
  EXPECT_EQ(1U + kRequestsPerFlush * 3, GetAndResetSentMessageCount());
}

TEST_F(ResourceDispatchThrottlerTest, NonRequestsDeferredWhenThrottling) {
  RequestResource();
  ASSERT_EQ(1U, GetAndResetSentMessageCount());

  RequestResourcesUntilThrottled();
  UpdateRequestPriority(last_request_id(), net::HIGHEST);
  ReleaseDownloadedFile(last_request_id());
  CancelRequest(last_request_id());

  EXPECT_TRUE(RunScheduledFlush());
  EXPECT_EQ(4U, GetAndResetSentMessageCount());
  EXPECT_FALSE(FlushScheduled());
}

TEST_F(ResourceDispatchThrottlerTest, MessageOrderingPreservedWhenThrottling) {
  SetHighPriorityWorkAnticipated(true);
  for (size_t i = 0; i < kRequestsPerFlush; ++i)
    RequestResource();
  ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount());

  for (size_t i = 0; i < kRequestsPerFlush; ++i) {
    RequestResource();
    UpdateRequestPriority(last_request_id(), net::HIGHEST);
    CancelRequest(last_request_id() - 1);
  }
  ASSERT_EQ(0U, sent_messages_.size());

  EXPECT_TRUE(RunScheduledFlush());
  ASSERT_EQ(kRequestsPerFlush * 3, sent_messages_.size());
  for (size_t i = 0; i < sent_messages_.size(); i += 3) {
    SCOPED_TRACE(i);
    const auto& request_msg = *sent_messages_[i];
    const auto& priority_msg = *sent_messages_[i + 1];
    const auto& cancel_msg = *sent_messages_[i + 2];

    EXPECT_EQ(request_msg.type(), ResourceHostMsg_RequestResource::ID);
    EXPECT_EQ(priority_msg.type(), ResourceHostMsg_DidChangePriority::ID);
    EXPECT_EQ(cancel_msg.type(), ResourceHostMsg_CancelRequest::ID);

    EXPECT_EQ(GetRequestId(request_msg), GetRequestId(priority_msg));
    EXPECT_EQ(GetRequestId(request_msg) - 1, GetRequestId(cancel_msg));
  }
  EXPECT_FALSE(FlushScheduled());
}

}  // namespace content