|
| 1 | +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package common |
| 5 | + |
| 6 | +import ( |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTimeoutScheduler(t *testing.T) { |
| 13 | + for _, testCase := range []struct { |
| 14 | + expectedInvocationCount int |
| 15 | + desc string |
| 16 | + shouldPreempt bool |
| 17 | + clock chan time.Time |
| 18 | + initClock func(chan time.Time) |
| 19 | + advanceTime func(chan time.Time) |
| 20 | + }{ |
| 21 | + { |
| 22 | + desc: "multiple pendingTimeout one after the other with preemption", |
| 23 | + expectedInvocationCount: 10, |
| 24 | + shouldPreempt: true, |
| 25 | + clock: make(chan time.Time, 1), |
| 26 | + initClock: func(_ chan time.Time) {}, |
| 27 | + advanceTime: func(_ chan time.Time) {}, |
| 28 | + }, |
| 29 | + { |
| 30 | + desc: "multiple pendingTimeout one after the other", |
| 31 | + expectedInvocationCount: 10, |
| 32 | + clock: make(chan time.Time, 1), |
| 33 | + initClock: func(clock chan time.Time) { |
| 34 | + clock <- time.Now() |
| 35 | + }, |
| 36 | + advanceTime: func(clock chan time.Time) { |
| 37 | + clock <- time.Now() |
| 38 | + }, |
| 39 | + }, |
| 40 | + } { |
| 41 | + t.Run(testCase.desc, func(_ *testing.T) { |
| 42 | + // Not enough invocations means the test would stall. |
| 43 | + // Too many invocations means a negative counter panic. |
| 44 | + var wg sync.WaitGroup |
| 45 | + wg.Add(testCase.expectedInvocationCount) |
| 46 | + |
| 47 | + testCase.initClock(testCase.clock) |
| 48 | + |
| 49 | + var preemptionSignal PreemptionSignal |
| 50 | + ps := preemptionSignal.Listen() |
| 51 | + |
| 52 | + if testCase.shouldPreempt { |
| 53 | + preemptionSignal.Preempt() |
| 54 | + } |
| 55 | + |
| 56 | + // Order enforces timeouts to be registered once after another, |
| 57 | + // in order to make the tests deterministic. |
| 58 | + order := make(chan struct{}) |
| 59 | + |
| 60 | + newTimer := func(_ time.Duration) *time.Timer { |
| 61 | + // We use a duration of 0 to not leave a lingering timer |
| 62 | + // after the test finishes. |
| 63 | + // Then we replace the time channel to have control over the timer. |
| 64 | + timer := time.NewTimer(0) |
| 65 | + timer.C = testCase.clock |
| 66 | + return timer |
| 67 | + } |
| 68 | + |
| 69 | + onTimeout := func() { |
| 70 | + order <- struct{}{} |
| 71 | + wg.Done() |
| 72 | + testCase.advanceTime(testCase.clock) |
| 73 | + } |
| 74 | + |
| 75 | + ts := NewTimeoutScheduler(onTimeout, ps, newTimer) |
| 76 | + |
| 77 | + for i := 0; i < testCase.expectedInvocationCount; i++ { |
| 78 | + ts.RegisterTimeout(time.Hour) |
| 79 | + <-order |
| 80 | + } |
| 81 | + |
| 82 | + wg.Wait() |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestTimeoutSchedulerConcurrentRegister(_ *testing.T) { |
| 88 | + clock := make(chan time.Time, 2) |
| 89 | + newTimer := func(_ time.Duration) *time.Timer { |
| 90 | + // We use a duration of 0 to not leave a lingering timer |
| 91 | + // after the test finishes. |
| 92 | + // Then we replace the time channel to have control over the timer. |
| 93 | + timer := time.NewTimer(0) |
| 94 | + timer.C = clock |
| 95 | + return timer |
| 96 | + } |
| 97 | + |
| 98 | + var wg sync.WaitGroup |
| 99 | + wg.Add(1) |
| 100 | + |
| 101 | + onTimeout := func() { |
| 102 | + wg.Done() |
| 103 | + } |
| 104 | + |
| 105 | + roChan := make(<-chan struct{}) |
| 106 | + |
| 107 | + ts := NewTimeoutScheduler(onTimeout, roChan, newTimer) |
| 108 | + |
| 109 | + ts.RegisterTimeout(time.Hour) // First timeout is registered |
| 110 | + ts.RegisterTimeout(time.Hour) // Second should not |
| 111 | + |
| 112 | + // Clock ticks are after registering, in order to ensure onTimeout() isn't fired until second registration is invoked. |
| 113 | + clock <- time.Now() |
| 114 | + clock <- time.Now() |
| 115 | + |
| 116 | + wg.Wait() |
| 117 | +} |
0 commit comments