github.com/MetalBlockchain/metalgo@v1.11.9/utils/resource/usage_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package resource 5 6 import ( 7 "math" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 const epsilon = 1e-9 15 16 func TestGetSampleWeights(t *testing.T) { 17 tests := []struct { 18 name string 19 frequency time.Duration 20 halflife time.Duration 21 oldWeight float64 22 }{ 23 { 24 name: "simple equal values", 25 frequency: 2 * time.Second, 26 halflife: 2 * time.Second, 27 oldWeight: .5, 28 }, 29 { 30 name: "two periods values", 31 frequency: 2 * time.Second, 32 halflife: 4 * time.Second, 33 oldWeight: math.Sqrt(.5), 34 }, 35 } 36 for _, test := range tests { 37 t.Run(test.name, func(t *testing.T) { 38 require := require.New(t) 39 40 newWeight, oldWeight := getSampleWeights(test.frequency, test.halflife) 41 require.InDelta(1-test.oldWeight, newWeight, epsilon) 42 require.InDelta(test.oldWeight, oldWeight, epsilon) 43 }) 44 } 45 }