mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
The module already called itself github.com/luxfi/node while containing no node in Go: cmd/luxd was twenty-one lines that printed the size of a few types, and the Go column of the matrix measured a VM plugin. A repo that fetches the thing it claims to be has not replaced it, so the Go sources move in — 1515 files, the daemon and everything under it — and the build reaches outside this module for nothing. Rust and C++ sit beside it unchanged and are still measured against it by the differential. That is the point of one repo: the reference implementation and the two ports are the same checkout, so parity is a thing you can run rather than a thing you assert. Verified: builds, and the closure carries no lux-private — checked at build time rather than believed. The daemon starts twelve chains on a local network (P X C Q Z A B G K M F, plus D through its plugin) and reports healthy.
134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package timer
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/luxfi/metric"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/luxfi/ids"
|
|
)
|
|
|
|
// Test that Initialize works
|
|
func TestAdaptiveTimeoutManagerInit(t *testing.T) {
|
|
type test struct {
|
|
config AdaptiveTimeoutConfig
|
|
expectedErr error
|
|
}
|
|
|
|
tests := []*test{
|
|
{
|
|
config: AdaptiveTimeoutConfig{
|
|
InitialTimeout: time.Second,
|
|
MinimumTimeout: 2 * time.Second,
|
|
MaximumTimeout: 3 * time.Second,
|
|
TimeoutCoefficient: 2,
|
|
TimeoutHalflife: 5 * time.Minute,
|
|
},
|
|
expectedErr: errInitialTimeoutBelowMinimum,
|
|
},
|
|
{
|
|
config: AdaptiveTimeoutConfig{
|
|
InitialTimeout: 5 * time.Second,
|
|
MinimumTimeout: 2 * time.Second,
|
|
MaximumTimeout: 3 * time.Second,
|
|
TimeoutCoefficient: 2,
|
|
TimeoutHalflife: 5 * time.Minute,
|
|
},
|
|
expectedErr: errInitialTimeoutAboveMaximum,
|
|
},
|
|
{
|
|
config: AdaptiveTimeoutConfig{
|
|
InitialTimeout: 2 * time.Second,
|
|
MinimumTimeout: 2 * time.Second,
|
|
MaximumTimeout: 3 * time.Second,
|
|
TimeoutCoefficient: 0.9,
|
|
TimeoutHalflife: 5 * time.Minute,
|
|
},
|
|
expectedErr: errTooSmallTimeoutCoefficient,
|
|
},
|
|
{
|
|
config: AdaptiveTimeoutConfig{
|
|
InitialTimeout: 2 * time.Second,
|
|
MinimumTimeout: 2 * time.Second,
|
|
MaximumTimeout: 3 * time.Second,
|
|
TimeoutCoefficient: 1,
|
|
},
|
|
expectedErr: errNonPositiveHalflife,
|
|
},
|
|
{
|
|
config: AdaptiveTimeoutConfig{
|
|
InitialTimeout: 2 * time.Second,
|
|
MinimumTimeout: 2 * time.Second,
|
|
MaximumTimeout: 3 * time.Second,
|
|
TimeoutCoefficient: 1,
|
|
TimeoutHalflife: -1 * time.Second,
|
|
},
|
|
expectedErr: errNonPositiveHalflife,
|
|
},
|
|
{
|
|
config: AdaptiveTimeoutConfig{
|
|
InitialTimeout: 2 * time.Second,
|
|
MinimumTimeout: 2 * time.Second,
|
|
MaximumTimeout: 3 * time.Second,
|
|
TimeoutCoefficient: 1,
|
|
TimeoutHalflife: 5 * time.Minute,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
_, err := NewAdaptiveTimeoutManager(&test.config, metric.NewNoOp().Registry())
|
|
require.ErrorIs(t, err, test.expectedErr)
|
|
}
|
|
}
|
|
|
|
func TestAdaptiveTimeoutManager(t *testing.T) {
|
|
tm, err := NewAdaptiveTimeoutManager(
|
|
&AdaptiveTimeoutConfig{
|
|
InitialTimeout: time.Millisecond,
|
|
MinimumTimeout: time.Millisecond,
|
|
MaximumTimeout: time.Hour,
|
|
TimeoutHalflife: 5 * time.Minute,
|
|
TimeoutCoefficient: 1.25,
|
|
},
|
|
metric.NewNoOp().Registry(),
|
|
)
|
|
require.NoError(t, err)
|
|
go tm.Dispatch()
|
|
|
|
var lock sync.Mutex
|
|
|
|
numSuccessful := 5
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(numSuccessful)
|
|
|
|
callback := new(func())
|
|
*callback = func() {
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
|
|
numSuccessful--
|
|
if numSuccessful > 0 {
|
|
tm.Put(ids.RequestID{Op: byte(numSuccessful)}, true, *callback)
|
|
}
|
|
if numSuccessful >= 0 {
|
|
wg.Done()
|
|
}
|
|
if numSuccessful%2 == 0 {
|
|
tm.Remove(ids.RequestID{Op: byte(numSuccessful)})
|
|
tm.Put(ids.RequestID{Op: byte(numSuccessful)}, true, *callback)
|
|
}
|
|
}
|
|
(*callback)()
|
|
(*callback)()
|
|
|
|
wg.Wait()
|
|
}
|