Skip to content

Commit

Permalink
chore: record continuous maintain loop records
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie0129 committed Feb 6, 2025
1 parent da06c3f commit 209e6ea
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
24 changes: 19 additions & 5 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,30 @@ func (r *MaintainLoopRecorder) AddRecord() {
r.LastMaintainLoopTimes = append(r.LastMaintainLoopTimes, time.Now())
}

// GetRecordsIn returns the number of records in the last duration.
// GetRecordsIn returns the number of continuous records in the last duration.
func (r *MaintainLoopRecorder) GetRecordsIn(last time.Duration) int {
r.mu.Lock()
defer r.mu.Unlock()

// Find continuous records from the end of the list.
// Continuous records are defined as the time difference between
// two adjacent records is less than loopInterval+1 second.
count := 0
for _, t := range r.LastMaintainLoopTimes {
if time.Since(t) <= last {
count++
for i := len(r.LastMaintainLoopTimes) - 1; i >= 0; i-- {
record := r.LastMaintainLoopTimes[i]
if time.Since(record) > last {
break
}

theRecordAfter := record
if i+1 < len(r.LastMaintainLoopTimes) {
theRecordAfter = r.LastMaintainLoopTimes[i+1]
}

if theRecordAfter.Sub(record) >= loopInterval+time.Second {
break
}
count++
}

return count
Expand Down Expand Up @@ -185,7 +199,7 @@ func maintainLoopInner() bool {
"maintainLoopCount": maintainLoopCount,
"expectedMaintainLoopCount": expectedMaintainLoopCount,
"minMaintainLoopCount": minMaintainLoopCount,
}).Infof("Battery charge is below lower limit, but too many missed maintain loops are missed. Rapid sleep/wake?")
}).Infof("Battery charge is below lower limit, but too many missed maintain loops are missed. Will wait until maintain loops are stable")
return true
}

Expand Down
71 changes: 71 additions & 0 deletions loop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"sync"
"testing"
"time"
)

func TestMaintainLoopRecorder_GetRecordsIn(t *testing.T) {
type fields struct {
MaxRecordCount int
LastMaintainLoopTimes []time.Time
mu *sync.Mutex
}
type args struct {
last time.Duration
}
tests := []struct {
name string
fields fields
args args
want int
}{
{
name: "test noncontinuous records",
fields: fields{
MaxRecordCount: 10,
LastMaintainLoopTimes: []time.Time{
time.Now().Add(-time.Second * 31).Add(-10 * time.Millisecond),
time.Now().Add(-time.Second * 20).Add(-10 * time.Millisecond),
time.Now().Add(-time.Second * 10).Add(-10 * time.Millisecond),
},
mu: &sync.Mutex{},
},
args: args{
last: time.Second * 40,
},
want: 2,
},
{
name: "test continuous records",
fields: fields{
MaxRecordCount: 10,
LastMaintainLoopTimes: []time.Time{
time.Now().Add(-time.Second * 60).Add(-10 * time.Millisecond),
time.Now().Add(-time.Second * 30).Add(-10 * time.Millisecond),
time.Now().Add(-time.Second * 20).Add(-10 * time.Millisecond),
time.Now().Add(-time.Second * 10).Add(-10 * time.Millisecond),
},
mu: &sync.Mutex{},
},
args: args{
last: time.Second * 50,
},
want: 3,
},
}
for _, tt := range tests {
loopInterval = time.Second * 10
t.Run(tt.name, func(t *testing.T) {
r := &MaintainLoopRecorder{
MaxRecordCount: tt.fields.MaxRecordCount,
LastMaintainLoopTimes: tt.fields.LastMaintainLoopTimes,
mu: tt.fields.mu,
}
if got := r.GetRecordsIn(tt.args.last); got != tt.want {
t.Errorf("GetRecordsIn() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 209e6ea

Please sign in to comment.