go.mway.dev/chrono@v0.6.1-0.20240126030049-189c5aef20d2/rate/recorder_test.go (about) 1 // Copyright (c) 2023 Matt Way 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to 5 // deal in the Software without restriction, including without limitation the 6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 // sell copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 // IN THE THE SOFTWARE. 20 21 package rate_test 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/require" 28 "go.mway.dev/chrono/clock" 29 "go.mway.dev/chrono/rate" 30 ) 31 32 func TestRecorder(t *testing.T) { 33 var ( 34 clk = clock.NewFakeClock() 35 recorder = rate.NewRecorderWithClock(clk) 36 ) 37 38 // A basic rate of 1M/s. 39 clk.Add(time.Second) 40 recorder.Add(1_000_000) 41 rate := recorder.Rate() 42 require.EqualValues(t, 1_000_000, rate.Per(time.Second)) 43 44 // Add another million without updating the clock, for a new rate of 2M/s. 45 recorder.Add(1_000_000) 46 rate = recorder.Rate() 47 require.EqualValues(t, 2_000_000, rate.Per(time.Second)) 48 require.EqualValues(t, 4_000_000, rate.Per(2*time.Second)) 49 require.EqualValues(t, 1_000_000, rate.Per(500*time.Millisecond)) 50 51 // Add another second to the clock, doubling the amount of time that has 52 // elapsed; the rate should drop by half. 53 clk.Add(time.Second) 54 rate = recorder.Rate() 55 require.EqualValues(t, 1_000_000, rate.Per(time.Second)) 56 require.EqualValues(t, 2_000_000, rate.Per(2*time.Second)) 57 require.EqualValues(t, 10_000_000, rate.Per(10*time.Second)) 58 59 // Reset the timer to ensure that it starts fresh. 60 recorder.Reset() 61 recorder.Add(1_000_000) 62 clk.Add(1000 * time.Second) 63 rate = recorder.Reset() 64 require.EqualValues(t, 1_000, rate.Per(time.Second)) 65 66 // Get a new rate after having called TakeRate to ensure that it's fresh. 67 recorder.Add(1_000_000) 68 clk.Add(time.Second) 69 rate = recorder.Rate() 70 require.EqualValues(t, 1_000_000, rate.Per(time.Second)) 71 } 72 73 func TestRecorderRealTime(t *testing.T) { 74 recorder := rate.NewRecorder() 75 recorder.Reset() 76 recorder.Add(1_000_000) 77 78 rate := recorder.Reset() 79 require.True(t, rate.Per(time.Nanosecond) > 0.0) 80 }