summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestTimers.cpp
blob: 7bd4cb1a148b55e2f6f1b67c6f59d11d3c0f7ac7 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsIThread.h"
#include "nsITimer.h"

#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsIObserverService.h"
#include "nsThreadUtils.h"
#include "prinrval.h"
#include "prmon.h"
#include "prthread.h"
#include "mozilla/Attributes.h"
#include "mozilla/gtest/MozAssertions.h"
#include "mozilla/Services.h"

#include "mozilla/Monitor.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/StaticPrefs_timer.h"

#include <list>
#include <vector>

#include "gtest/gtest.h"

using namespace mozilla;

typedef nsresult (*TestFuncPtr)();

class AutoTestThread {
 public:
  AutoTestThread() {
    nsCOMPtr<nsIThread> newThread;
    nsresult rv =
        NS_NewNamedThread("AutoTestThread", getter_AddRefs(newThread));
    if (NS_FAILED(rv)) return;

    newThread.swap(mThread);
  }

  ~AutoTestThread() { mThread->Shutdown(); }

  operator nsIThread*() const { return mThread; }

  nsIThread* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
    return mThread;
  }

 private:
  nsCOMPtr<nsIThread> mThread;
};

class AutoCreateAndDestroyReentrantMonitor {
 public:
  AutoCreateAndDestroyReentrantMonitor() {
    mReentrantMonitor = new ReentrantMonitor("TestTimers::AutoMon");
    MOZ_RELEASE_ASSERT(mReentrantMonitor, "Out of memory!");
  }

  ~AutoCreateAndDestroyReentrantMonitor() { delete mReentrantMonitor; }

  operator ReentrantMonitor*() const { return mReentrantMonitor; }

 private:
  ReentrantMonitor* mReentrantMonitor;
};

class TimerCallback final : public nsITimerCallback {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS

  TimerCallback(nsIThread** aThreadPtr, ReentrantMonitor* aReentrantMonitor)
      : mThreadPtr(aThreadPtr), mReentrantMonitor(aReentrantMonitor) {}

  NS_IMETHOD Notify(nsITimer* aTimer) override {
    MOZ_RELEASE_ASSERT(mThreadPtr, "Callback was not supposed to be called!");
    nsCOMPtr<nsIThread> current(do_GetCurrentThread());

    ReentrantMonitorAutoEnter mon(*mReentrantMonitor);

    MOZ_RELEASE_ASSERT(!*mThreadPtr, "Timer called back more than once!");
    *mThreadPtr = current;

    mon.Notify();

    return NS_OK;
  }

 private:
  ~TimerCallback() = default;

  nsIThread** mThreadPtr;
  ReentrantMonitor* mReentrantMonitor;
};

NS_IMPL_ISUPPORTS(TimerCallback, nsITimerCallback)

class TimerHelper {
 public:
  explicit TimerHelper(nsIEventTarget* aTarget)
      : mStart(TimeStamp::Now()),
        mTimer(NS_NewTimer(aTarget)),
        mMonitor(__func__),
        mTarget(aTarget) {}

  ~TimerHelper() { Cancel(); }

  static void ClosureCallback(nsITimer*, void* aClosure) {
    reinterpret_cast<TimerHelper*>(aClosure)->Notify();
  }

  // We do not use nsITimerCallback, because that results in a circular
  // reference. One of the properties we want from TimerHelper is for the
  // timer to be canceled when it goes out of scope.
  void Notify() {
    MonitorAutoLock lock(mMonitor);
    EXPECT_TRUE(mTarget->IsOnCurrentThread());
    TimeDuration elapsed = TimeStamp::Now() - mStart;
    mStart = TimeStamp::Now();
    mLastDelay = Some(elapsed.ToMilliseconds());
    if (mBlockTime) {
      PR_Sleep(mBlockTime);
    }
    mMonitor.Notify();
  }

  nsresult SetTimer(uint32_t aDelay, uint8_t aType) {
    Cancel();
    MonitorAutoLock lock(mMonitor);
    mStart = TimeStamp::Now();
    return mTimer->InitWithNamedFuncCallback(
        ClosureCallback, this, aDelay, aType, "TimerHelper::ClosureCallback");
  }

  Maybe<uint32_t> Wait(uint32_t aLimitMs) {
    return WaitAndBlockCallback(aLimitMs, 0);
  }

  // Waits for callback, and if it occurs within the limit, causes the callback
  // to block for the specified time. Useful for testing cases where the
  // callback takes a long time to return.
  Maybe<uint32_t> WaitAndBlockCallback(uint32_t aLimitMs, uint32_t aBlockTime) {
    MonitorAutoLock lock(mMonitor);
    mBlockTime = aBlockTime;
    TimeStamp start = TimeStamp::Now();
    while (!mLastDelay.isSome()) {
      mMonitor.Wait(TimeDuration::FromMilliseconds(aLimitMs));
      TimeDuration elapsed = TimeStamp::Now() - start;
      uint32_t elapsedMs = static_cast<uint32_t>(elapsed.ToMilliseconds());
      if (elapsedMs >= aLimitMs) {
        break;
      }
      aLimitMs -= elapsedMs;
      start = TimeStamp::Now();
    }
    mBlockTime = 0;
    return std::move(mLastDelay);
  }

  void Cancel() {
    NS_DispatchAndSpinEventLoopUntilComplete(
        "~TimerHelper timer cancel"_ns, mTarget,
        NS_NewRunnableFunction("~TimerHelper timer cancel", [this] {
          MonitorAutoLock lock(mMonitor);
          mTimer->Cancel();
        }));
  }

 private:
  TimeStamp mStart;
  RefPtr<nsITimer> mTimer;
  mutable Monitor mMonitor MOZ_UNANNOTATED;
  uint32_t mBlockTime = 0;
  Maybe<uint32_t> mLastDelay;
  RefPtr<nsIEventTarget> mTarget;
};

class SimpleTimerTest : public ::testing::Test {
 public:
  std::unique_ptr<TimerHelper> MakeTimer(uint32_t aDelay, uint8_t aType) {
    std::unique_ptr<TimerHelper> timer(new TimerHelper(mThread));
    timer->SetTimer(aDelay, aType);
    return timer;
  }

  void PauseTimerThread() {
    nsCOMPtr<nsIObserverService> observerService =
        mozilla::services::GetObserverService();
    observerService->NotifyObservers(nullptr, "sleep_notification", nullptr);
  }

  void ResumeTimerThread() {
    nsCOMPtr<nsIObserverService> observerService =
        mozilla::services::GetObserverService();
    observerService->NotifyObservers(nullptr, "wake_notification", nullptr);
  }

 protected:
  AutoTestThread mThread;
};

#ifdef XP_MACOSX
// For some reason, our OS X testers fire timed condition waits _extremely_
// late (as much as 200ms).
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1726915
const unsigned kSlowdownFactor = 50;
#elif XP_WIN
// Windows also needs some extra leniency, but not nearly as much as our OS X
// testers
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1729035
const unsigned kSlowdownFactor = 5;
#else
const unsigned kSlowdownFactor = 1;
#endif

TEST_F(SimpleTimerTest, OneShot) {
  auto timer = MakeTimer(100 * kSlowdownFactor, nsITimer::TYPE_ONE_SHOT);
  auto res = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(res.isSome());
  ASSERT_LT(*res, 110U * kSlowdownFactor);
  ASSERT_GT(*res, 95U * kSlowdownFactor);
}

TEST_F(SimpleTimerTest, TimerWithStoppedTarget) {
  mThread->Shutdown();
  auto timer = MakeTimer(100 * kSlowdownFactor, nsITimer::TYPE_ONE_SHOT);
  auto res = timer->Wait(110 * kSlowdownFactor);
  ASSERT_FALSE(res.isSome());
}

TEST_F(SimpleTimerTest, SlackRepeating) {
  auto timer = MakeTimer(100 * kSlowdownFactor, nsITimer::TYPE_REPEATING_SLACK);
  auto delay =
      timer->WaitAndBlockCallback(110 * kSlowdownFactor, 50 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);
  // REPEATING_SLACK timers re-schedule with the full duration when the timer
  // callback completes

  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 160U * kSlowdownFactor);
  ASSERT_GT(*delay, 145U * kSlowdownFactor);
}

TEST_F(SimpleTimerTest, RepeatingPrecise) {
  auto timer = MakeTimer(100 * kSlowdownFactor,
                         nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP);
  auto delay =
      timer->WaitAndBlockCallback(110 * kSlowdownFactor, 50 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);

  // Delays smaller than the timer's period do not effect the period.
  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);

  // Delays larger than the timer's period should result in the skipping of
  // firings, but the cadence should remain the same.
  delay =
      timer->WaitAndBlockCallback(110 * kSlowdownFactor, 150 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);

  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 210U * kSlowdownFactor);
  ASSERT_GT(*delay, 195U * kSlowdownFactor);
}

// gtest on 32bit Win7 debug build is unstable and somehow this test
// makes it even worse.
#if !defined(XP_WIN) || !defined(DEBUG) || defined(HAVE_64BIT_BUILD)

class FindExpirationTimeState final {
 public:
  // We'll offset the timers 10 seconds into the future to assure that they
  // won't fire
  const uint32_t kTimerOffset = 10 * 1000;
  // And we'll set the timers spaced by 5 seconds.
  const uint32_t kTimerInterval = 5 * 1000;
  // We'll use 20 timers
  const uint32_t kNumTimers = 20;

  TimeStamp mBefore;
  TimeStamp mMiddle;

  std::list<nsCOMPtr<nsITimer>> mTimers;

  ~FindExpirationTimeState() {
    while (!mTimers.empty()) {
      nsCOMPtr<nsITimer> t = mTimers.front().get();
      mTimers.pop_front();
      t->Cancel();
    }
  }

  // Create timers, with aNumLowPriority low priority timers first in the queue
  void InitTimers(uint32_t aNumLowPriority, uint32_t aType) {
    // aType is just for readability.
    MOZ_RELEASE_ASSERT(aType == nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY);
    InitTimers(aNumLowPriority, nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY, nullptr);
  }

  // Create timers, with aNumDifferentTarget timers with target aTarget first in
  // the queue
  void InitTimers(uint32_t aNumDifferentTarget, nsIEventTarget* aTarget) {
    InitTimers(aNumDifferentTarget, nsITimer::TYPE_ONE_SHOT, aTarget);
  }

  void InitTimers(uint32_t aNumDifferingTimers, uint32_t aType,
                  nsIEventTarget* aTarget) {
    do {
      TimeStamp clearUntil =
          TimeStamp::Now() + TimeDuration::FromMilliseconds(
                                 kTimerOffset + kNumTimers * kTimerInterval);

      // NS_GetTimerDeadlineHintOnCurrentThread returns clearUntil if there are
      // no pending timers before clearUntil.
      // Passing 0 ensures that we examine the next timer to fire, regardless
      // of its thread target. This is important, because lots of the checks
      // we perform in the test get confused by timers targeted at other
      // threads.
      TimeStamp t = NS_GetTimerDeadlineHintOnCurrentThread(clearUntil, 0);
      if (t >= clearUntil) {
        break;
      }

      // Clear whatever random timers there might be pending.
      uint32_t waitTime = 10;
      if (t > TimeStamp::Now()) {
        waitTime = uint32_t((t - TimeStamp::Now()).ToMilliseconds());
      }
      PR_Sleep(PR_MillisecondsToInterval(waitTime));
    } while (true);

    mBefore = TimeStamp::Now();
    // To avoid getting exactly the same time for a timer and mMiddle, subtract
    // 50 ms.
    mMiddle = mBefore +
              TimeDuration::FromMilliseconds(kTimerOffset +
                                             kTimerInterval * kNumTimers / 2) -
              TimeDuration::FromMilliseconds(50);
    for (uint32_t i = 0; i < kNumTimers; ++i) {
      nsCOMPtr<nsITimer> timer = NS_NewTimer();
      ASSERT_TRUE(timer);

      if (i < aNumDifferingTimers) {
        if (aTarget) {
          timer->SetTarget(aTarget);
        }

        timer->InitWithNamedFuncCallback(
            &UnusedCallbackFunc, nullptr, kTimerOffset + kTimerInterval * i,
            aType, "FindExpirationTimeState::InitTimers");
      } else {
        timer->InitWithNamedFuncCallback(
            &UnusedCallbackFunc, nullptr, kTimerOffset + kTimerInterval * i,
            nsITimer::TYPE_ONE_SHOT, "FindExpirationTimeState::InitTimers");
      }
      mTimers.push_front(timer.get());
    }
  }

  static void UnusedCallbackFunc(nsITimer* aTimer, void* aClosure) {
    FAIL() << "Timer shouldn't fire.";
  }
};

TEST_F(SimpleTimerTest, FindExpirationTime) {
  {
    FindExpirationTimeState state;
    // 0 low priority timers
    state.InitTimers(0, nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY);
    TimeStamp before = state.mBefore;
    TimeStamp middle = state.mMiddle;

    TimeStamp t;
    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 10);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";
  }

  {
    FindExpirationTimeState state;
    // 5 low priority timers
    state.InitTimers(5, nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY);
    TimeStamp before = state.mBefore;
    TimeStamp middle = state.mMiddle;

    TimeStamp t;
    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 10);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";
  }

  {
    FindExpirationTimeState state;
    // 15 low priority timers
    state.InitTimers(15, nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY);
    TimeStamp before = state.mBefore;
    TimeStamp middle = state.mMiddle;

    TimeStamp t;
    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 10);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, middle) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, middle) << "Found time should be equal to default";
  }

  {
    AutoTestThread testThread;
    FindExpirationTimeState state;
    // 5 other targets
    state.InitTimers(5, static_cast<nsIEventTarget*>(testThread));
    TimeStamp before = state.mBefore;
    TimeStamp middle = state.mMiddle;

    TimeStamp t;
    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 10);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";
  }

  {
    AutoTestThread testThread;
    FindExpirationTimeState state;
    // 15 other targets
    state.InitTimers(15, static_cast<nsIEventTarget*>(testThread));
    TimeStamp before = state.mBefore;
    TimeStamp middle = state.mMiddle;

    TimeStamp t;
    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(before, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, before) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 0);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_LT(t, middle) << "Found time should be less than default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 10);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, middle) << "Found time should be equal to default";

    t = NS_GetTimerDeadlineHintOnCurrentThread(middle, 20);
    EXPECT_TRUE(t) << "We should find a time";
    EXPECT_EQ(t, middle) << "Found time should be equal to default";
  }
}

#endif

// Do these _after_ FindExpirationTime; apparently pausing the timer thread
// schedules minute-long timers, which FindExpirationTime waits out before
// starting.
TEST_F(SimpleTimerTest, SleepWakeOneShot) {
  if (StaticPrefs::timer_ignore_sleep_wake_notifications()) {
    return;
  }
  auto timer = MakeTimer(100 * kSlowdownFactor, nsITimer::TYPE_ONE_SHOT);
  PauseTimerThread();
  auto delay = timer->Wait(200 * kSlowdownFactor);
  ResumeTimerThread();
  ASSERT_FALSE(delay.isSome());
}

TEST_F(SimpleTimerTest, SleepWakeRepeatingSlack) {
  if (StaticPrefs::timer_ignore_sleep_wake_notifications()) {
    return;
  }
  auto timer = MakeTimer(100 * kSlowdownFactor, nsITimer::TYPE_REPEATING_SLACK);
  PauseTimerThread();
  auto delay = timer->Wait(200 * kSlowdownFactor);
  ResumeTimerThread();
  ASSERT_FALSE(delay.isSome());

  // Timer thread slept for ~200ms, longer than the duration of the timer, so
  // it should fire pretty much immediately.
  delay = timer->Wait(10 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 210 * kSlowdownFactor);
  ASSERT_GT(*delay, 199 * kSlowdownFactor);

  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);

  PauseTimerThread();
  delay = timer->Wait(50 * kSlowdownFactor);
  ResumeTimerThread();
  ASSERT_FALSE(delay.isSome());

  // Timer thread only slept for ~50 ms, shorter than the duration of the
  // timer, so there should be no effect on the timing.
  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);
}

TEST_F(SimpleTimerTest, SleepWakeRepeatingPrecise) {
  if (StaticPrefs::timer_ignore_sleep_wake_notifications()) {
    return;
  }
  auto timer = MakeTimer(100 * kSlowdownFactor,
                         nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP);
  PauseTimerThread();
  auto delay = timer->Wait(350 * kSlowdownFactor);
  ResumeTimerThread();
  ASSERT_FALSE(delay.isSome());

  // Timer thread slept longer than the duration of the timer, so it should
  // fire pretty much immediately.
  delay = timer->Wait(10 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 360U * kSlowdownFactor);
  ASSERT_GT(*delay, 349U * kSlowdownFactor);

  // After that, we should get back on our original cadence
  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 60U * kSlowdownFactor);
  ASSERT_GT(*delay, 45U * kSlowdownFactor);

  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);

  PauseTimerThread();
  delay = timer->Wait(50 * kSlowdownFactor);
  ResumeTimerThread();
  ASSERT_FALSE(delay.isSome());

  // Timer thread only slept for ~50 ms, shorter than the duration of the
  // timer, so there should be no effect on the timing.
  delay = timer->Wait(110 * kSlowdownFactor);
  ASSERT_TRUE(delay.isSome());
  ASSERT_LT(*delay, 110U * kSlowdownFactor);
  ASSERT_GT(*delay, 95U * kSlowdownFactor);
}

#define FUZZ_MAX_TIMEOUT 9
class FuzzTestThreadState final : public nsITimerCallback {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS

  explicit FuzzTestThreadState(nsIThread* thread)
      : mThread(thread), mStopped(false) {}

  class StartRunnable final : public mozilla::Runnable {
   public:
    explicit StartRunnable(FuzzTestThreadState* threadState)
        : mozilla::Runnable("FuzzTestThreadState::StartRunnable"),
          mThreadState(threadState) {}

    NS_IMETHOD Run() override {
      mThreadState->ScheduleOrCancelTimers();
      return NS_OK;
    }

   private:
    RefPtr<FuzzTestThreadState> mThreadState;
  };

  void Start() {
    nsCOMPtr<nsIRunnable> runnable = new StartRunnable(this);
    nsresult rv = mThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
    MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv), "Failed to dispatch StartRunnable.");
  }

  void Stop() { mStopped = true; }

  NS_IMETHOD Notify(nsITimer* aTimer) override {
    bool onCorrectThread;
    nsresult rv = mThread->IsOnCurrentThread(&onCorrectThread);
    MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv), "Failed to perform thread check.");
    MOZ_RELEASE_ASSERT(onCorrectThread, "Notify invoked on wrong thread.");

    uint32_t delay;
    rv = aTimer->GetDelay(&delay);
    MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv), "GetDelay failed.");

    MOZ_RELEASE_ASSERT(delay <= FUZZ_MAX_TIMEOUT,
                       "Delay was an invalid value for this test.");

    uint32_t type;
    rv = aTimer->GetType(&type);
    MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv), "Failed to get timer type.");
    MOZ_RELEASE_ASSERT(type <= nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP);

    if (type == nsITimer::TYPE_ONE_SHOT) {
      MOZ_RELEASE_ASSERT(!mOneShotTimersByDelay[delay].empty(),
                         "Unexpected one-shot timer.");

      MOZ_RELEASE_ASSERT(mOneShotTimersByDelay[delay].front().get() == aTimer,
                         "One-shot timers have been reordered.");

      mOneShotTimersByDelay[delay].pop_front();
      --mTimersOutstanding;
    } else if (mStopped) {
      CancelRepeatingTimer(aTimer);
    }

    ScheduleOrCancelTimers();
    RescheduleSomeTimers();
    return NS_OK;
  }

  bool HasTimersOutstanding() const { return !!mTimersOutstanding; }

 private:
  ~FuzzTestThreadState() {
    for (size_t i = 0; i <= FUZZ_MAX_TIMEOUT; ++i) {
      MOZ_RELEASE_ASSERT(mOneShotTimersByDelay[i].empty(),
                         "Timers remain at end of test.");
    }
  }

  uint32_t GetRandomType() const {
    return rand() % (nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP + 1);
  }

  size_t CountOneShotTimers() const {
    size_t count = 0;
    for (size_t i = 0; i <= FUZZ_MAX_TIMEOUT; ++i) {
      count += mOneShotTimersByDelay[i].size();
    }
    return count;
  }

  void ScheduleOrCancelTimers() {
    if (mStopped) {
      return;
    }

    const size_t numTimersDesired = (rand() % 100) + 100;
    MOZ_RELEASE_ASSERT(numTimersDesired >= 100);
    MOZ_RELEASE_ASSERT(numTimersDesired < 200);
    int adjustment = numTimersDesired - mTimersOutstanding;

    while (adjustment > 0) {
      CreateRandomTimer();
      --adjustment;
    }

    while (adjustment < 0) {
      CancelRandomTimer();
      ++adjustment;
    }

    MOZ_RELEASE_ASSERT(numTimersDesired == mTimersOutstanding);
  }

  void RescheduleSomeTimers() {
    if (mStopped) {
      return;
    }

    static const size_t kNumRescheduled = 40;

    // Reschedule some timers with a Cancel first.
    for (size_t i = 0; i < kNumRescheduled; ++i) {
      InitRandomTimer(CancelRandomTimer().get());
    }
    // Reschedule some timers without a Cancel first.
    for (size_t i = 0; i < kNumRescheduled; ++i) {
      InitRandomTimer(RemoveRandomTimer().get());
    }
  }

  void CreateRandomTimer() {
    nsCOMPtr<nsITimer> timer =
        NS_NewTimer(static_cast<nsIEventTarget*>(mThread.get()));
    MOZ_RELEASE_ASSERT(timer, "Failed to create timer.");

    InitRandomTimer(timer.get());
  }

  nsCOMPtr<nsITimer> CancelRandomTimer() {
    nsCOMPtr<nsITimer> timer(RemoveRandomTimer());
    timer->Cancel();
    return timer;
  }

  nsCOMPtr<nsITimer> RemoveRandomTimer() {
    MOZ_RELEASE_ASSERT(mTimersOutstanding);

    if ((GetRandomType() == nsITimer::TYPE_ONE_SHOT && CountOneShotTimers()) ||
        mRepeatingTimers.empty()) {
      uint32_t delayToRemove = rand() % (FUZZ_MAX_TIMEOUT + 1);
      while (mOneShotTimersByDelay[delayToRemove].empty()) {
        // ++delayToRemove mod FUZZ_MAX_TIMEOUT + 1
        delayToRemove = (delayToRemove + 1) % (FUZZ_MAX_TIMEOUT + 1);
      }

      uint32_t indexToRemove =
          rand() % mOneShotTimersByDelay[delayToRemove].size();

      for (auto it = mOneShotTimersByDelay[delayToRemove].begin();
           it != mOneShotTimersByDelay[delayToRemove].end(); ++it) {
        if (indexToRemove) {
          --indexToRemove;
          continue;
        }

        nsCOMPtr<nsITimer> removed = *it;
        mOneShotTimersByDelay[delayToRemove].erase(it);
        --mTimersOutstanding;
        return removed;
      }
    } else {
      size_t indexToRemove = rand() % mRepeatingTimers.size();
      nsCOMPtr<nsITimer> removed(mRepeatingTimers[indexToRemove]);
      mRepeatingTimers.erase(mRepeatingTimers.begin() + indexToRemove);
      --mTimersOutstanding;
      return removed;
    }

    MOZ_CRASH("Unable to remove a timer");
  }

  void InitRandomTimer(nsITimer* aTimer) {
    // Between 0 and FUZZ_MAX_TIMEOUT
    uint32_t delay = rand() % (FUZZ_MAX_TIMEOUT + 1);
    uint32_t type = GetRandomType();
    nsresult rv = aTimer->InitWithCallback(this, delay, type);
    MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv), "Failed to set timer.");

    if (type == nsITimer::TYPE_ONE_SHOT) {
      mOneShotTimersByDelay[delay].push_back(aTimer);
    } else {
      mRepeatingTimers.push_back(aTimer);
    }
    ++mTimersOutstanding;
  }

  void CancelRepeatingTimer(nsITimer* aTimer) {
    for (auto it = mRepeatingTimers.begin(); it != mRepeatingTimers.end();
         ++it) {
      if (it->get() == aTimer) {
        mRepeatingTimers.erase(it);
        aTimer->Cancel();
        --mTimersOutstanding;
        return;
      }
    }
  }

  nsCOMPtr<nsIThread> mThread;
  // Scheduled timers, indexed by delay between 0-9 ms, in lists
  // with most recently scheduled last.
  std::list<nsCOMPtr<nsITimer>> mOneShotTimersByDelay[FUZZ_MAX_TIMEOUT + 1];
  std::vector<nsCOMPtr<nsITimer>> mRepeatingTimers;
  Atomic<bool> mStopped;
  Atomic<size_t> mTimersOutstanding;
};

NS_IMPL_ISUPPORTS(FuzzTestThreadState, nsITimerCallback)

TEST(Timers, FuzzTestTimers)
{
  static const size_t kNumThreads(10);
  AutoTestThread threads[kNumThreads];
  RefPtr<FuzzTestThreadState> threadStates[kNumThreads];

  for (size_t i = 0; i < kNumThreads; ++i) {
    threadStates[i] = new FuzzTestThreadState(&*threads[i]);
    threadStates[i]->Start();
  }

  PR_Sleep(PR_MillisecondsToInterval(20000));

  for (size_t i = 0; i < kNumThreads; ++i) {
    threadStates[i]->Stop();
  }

  // Wait at most 10 seconds for all outstanding timers to pop
  PRIntervalTime start = PR_IntervalNow();
  for (auto& threadState : threadStates) {
    while (threadState->HasTimersOutstanding()) {
      uint32_t elapsedMs = PR_IntervalToMilliseconds(PR_IntervalNow() - start);
      ASSERT_LE(elapsedMs, uint32_t(10000))
          << "Timed out waiting for all timers to pop";
      PR_Sleep(PR_MillisecondsToInterval(10));
    }
  }
}

TEST(Timers, ClosureCallback)
{
  AutoCreateAndDestroyReentrantMonitor newMon;
  ASSERT_TRUE(newMon);

  AutoTestThread testThread;
  ASSERT_TRUE(testThread);

  nsIThread* notifiedThread = nullptr;

  nsCOMPtr<nsITimer> timer;
  nsresult rv = NS_NewTimerWithCallback(
      getter_AddRefs(timer),
      [&](nsITimer*) {
        nsCOMPtr<nsIThread> current(do_GetCurrentThread());

        ReentrantMonitorAutoEnter mon(*newMon);
        ASSERT_FALSE(notifiedThread);
        notifiedThread = current;
        mon.Notify();
      },
      50, nsITimer::TYPE_ONE_SHOT, "(test) Timers.ClosureCallback", testThread);
  ASSERT_NS_SUCCEEDED(rv);

  ReentrantMonitorAutoEnter mon(*newMon);
  while (!notifiedThread) {
    mon.Wait();
  }
  ASSERT_EQ(notifiedThread, testThread);
}

static void SetTime(nsITimer* aTimer, void* aClosure) {
  *static_cast<TimeStamp*>(aClosure) = TimeStamp::Now();
}

TEST(Timers, HighResFuncCallback)
{
  TimeStamp first;
  TimeStamp second;
  TimeStamp third;
  nsCOMPtr<nsITimer> t1 = NS_NewTimer(GetCurrentSerialEventTarget());
  nsCOMPtr<nsITimer> t2 = NS_NewTimer(GetCurrentSerialEventTarget());
  nsCOMPtr<nsITimer> t3 = NS_NewTimer(GetCurrentSerialEventTarget());

  // Reverse order, since if the timers are not high-res we'd end up
  // out-of-order.
  MOZ_ALWAYS_SUCCEEDS(t3->InitHighResolutionWithNamedFuncCallback(
      &SetTime, &third, TimeDuration::FromMicroseconds(300),
      nsITimer::TYPE_ONE_SHOT, "TestTimers::HighResFuncCallback::third"));
  MOZ_ALWAYS_SUCCEEDS(t2->InitHighResolutionWithNamedFuncCallback(
      &SetTime, &second, TimeDuration::FromMicroseconds(200),
      nsITimer::TYPE_ONE_SHOT, "TestTimers::HighResFuncCallback::second"));
  MOZ_ALWAYS_SUCCEEDS(t1->InitHighResolutionWithNamedFuncCallback(
      &SetTime, &first, TimeDuration::FromMicroseconds(100),
      nsITimer::TYPE_ONE_SHOT, "TestTimers::HighResFuncCallback::first"));

  SpinEventLoopUntil<ProcessFailureBehavior::IgnoreAndContinue>(
      "TestTimers::HighResFuncCallback"_ns,
      [&] { return !first.IsNull() && !second.IsNull() && !third.IsNull(); });
}