-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make bootstrapping handle its own timeouts
Currently, an engine registers timeouts into the handler, which schedules the timeouts on behalf of the the engine. The handler then notifies the engine when the timeout expired. However, the only engine that uses this mechanism is the bootstrapping engine, and not the other engine types such as the snowman and state sync engines. It therefore makes sense to consolidate the timeout handling instead of delegating them to the handler. By moving the timeout handling closer to the bootstrapper, we can make the API of the common.Engine be slimmer by removing the Timeout() method from it. Signed-off-by: Yacov Manevich <[email protected]>
- Loading branch information
Showing
18 changed files
with
185 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package common | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimeoutHandler(t *testing.T) { | ||
for _, testCase := range []struct { | ||
desc string | ||
clock func(time.Duration) (<-chan time.Time, func() bool) | ||
body func(*timeoutHandler, *sync.WaitGroup, *PreemptionSignal) | ||
}{ | ||
{ | ||
desc: "Function invoked upon timeout", | ||
clock: func(d time.Duration) (<-chan time.Time, func() bool) { | ||
timer := time.NewTimer(d) | ||
return timer.C, timer.Stop | ||
}, | ||
body: func(th *timeoutHandler, wg *sync.WaitGroup, _ *PreemptionSignal) { | ||
th.RegisterTimeout(time.Millisecond) | ||
wg.Wait() | ||
}, | ||
}, | ||
{ | ||
desc: "Preemption makes the function be invoked immediately despite no clock tick", | ||
body: func(th *timeoutHandler, wg *sync.WaitGroup, signal *PreemptionSignal) { | ||
th.RegisterTimeout(time.Hour) | ||
signal.Preempt() | ||
wg.Wait() | ||
}, | ||
}, | ||
} { | ||
t.Run(testCase.desc, func(t *testing.T) { | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
var invoked int | ||
|
||
var preemptionsignal PreemptionSignal | ||
|
||
th := NewTimeoutHandler(func() { | ||
invoked++ | ||
wg.Done() | ||
}, preemptionsignal.Listen()) | ||
if testCase.clock != nil { | ||
th.newTimer = testCase.clock | ||
} | ||
|
||
testCase.body(th, &wg, &preemptionsignal) | ||
require.Equal(t, 1, invoked) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.