Skip to content

Commit 1f5a7e2

Browse files
committed
Sort history
1 parent ca71f62 commit 1f5a7e2

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

api/v1alpha1/function_lifecycle.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v1alpha1
22

33
import (
44
"fmt"
5+
"sort"
56
"time"
67

78
"k8s.io/apimachinery/pkg/api/meta"
@@ -243,7 +244,10 @@ func (f *Function) RecordHistoryEvent(message string, options ...historyEventOpt
243244
opt(&entry)
244245
}
245246

246-
f.Status.History = append([]FunctionStatusHistoryEntry{entry}, f.Status.History...)
247+
f.Status.History = append(f.Status.History, entry)
248+
sort.Slice(f.Status.History, func(i, j int) bool {
249+
return f.Status.History[i].Time.After(f.Status.History[j].Time.Time)
250+
})
247251
if len(f.Status.History) > MaxHistoryEntries {
248252
f.Status.History = f.Status.History[:MaxHistoryEntries]
249253
}

api/v1alpha1/function_lifecycle_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v1alpha1
33
import (
44
"fmt"
55
"testing"
6+
"time"
67

78
"k8s.io/apimachinery/pkg/api/meta"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,7 +29,7 @@ func TestRecordHistoryEvent(t *testing.T) {
2829
{
2930
name: "prepends event to existing history",
3031
existingHistory: []FunctionStatusHistoryEntry{
31-
{Time: metav1.Now(), Message: "Older event"},
32+
{Time: metav1.NewTime(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)), Message: "Older event"},
3233
},
3334
newMessage: "Newer event",
3435
expectedLen: 2,
@@ -38,10 +39,11 @@ func TestRecordHistoryEvent(t *testing.T) {
3839
{
3940
name: "trims oldest entries when exceeding max",
4041
existingHistory: func() []FunctionStatusHistoryEntry {
42+
base := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
4143
entries := make([]FunctionStatusHistoryEntry, MaxHistoryEntries)
4244
for i := range entries {
4345
entries[i] = FunctionStatusHistoryEntry{
44-
Time: metav1.Now(),
46+
Time: metav1.NewTime(base.Add(time.Duration(i) * time.Minute)),
4547
Message: fmt.Sprintf("Event %d", i),
4648
}
4749
}
@@ -50,7 +52,7 @@ func TestRecordHistoryEvent(t *testing.T) {
5052
newMessage: "Overflow event",
5153
expectedLen: MaxHistoryEntries,
5254
expectedFirst: "Overflow event",
53-
expectedLast: fmt.Sprintf("Event %d", MaxHistoryEntries-2),
55+
expectedLast: fmt.Sprintf("Event %d", 1),
5456
},
5557
}
5658

@@ -76,9 +78,10 @@ func TestRecordHistoryEvent(t *testing.T) {
7678

7779
func TestRecordHistoryEventFIFOOrder(t *testing.T) {
7880
f := &Function{}
81+
base := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
7982

8083
for i := 0; i < MaxHistoryEntries+5; i++ {
81-
f.RecordHistoryEvent(fmt.Sprintf("Event %d", i))
84+
f.RecordHistoryEvent(fmt.Sprintf("Event %d", i), WithHistoryEventTime(base.Add(time.Duration(i)*time.Minute)))
8285
}
8386

8487
if len(f.Status.History) != MaxHistoryEntries {
@@ -93,6 +96,28 @@ func TestRecordHistoryEventFIFOOrder(t *testing.T) {
9396
}
9497
}
9598

99+
func TestRecordHistoryEventSortsByTime(t *testing.T) {
100+
f := &Function{}
101+
now := time.Date(2025, 6, 15, 12, 0, 0, 0, time.UTC)
102+
103+
f.RecordHistoryEvent("Second event", WithHistoryEventTime(now))
104+
f.RecordHistoryEvent("First event (older)", WithHistoryEventTime(now.Add(-1*time.Hour)))
105+
f.RecordHistoryEvent("Third event", WithHistoryEventTime(now.Add(1*time.Hour)))
106+
107+
if len(f.Status.History) != 3 {
108+
t.Fatalf("expected 3 entries, got %d", len(f.Status.History))
109+
}
110+
if f.Status.History[0].Message != "Third event" {
111+
t.Errorf("expected first entry to be newest, got %q", f.Status.History[0].Message)
112+
}
113+
if f.Status.History[1].Message != "Second event" {
114+
t.Errorf("expected second entry to be middle, got %q", f.Status.History[1].Message)
115+
}
116+
if f.Status.History[2].Message != "First event (older)" {
117+
t.Errorf("expected last entry to be oldest, got %q", f.Status.History[2].Message)
118+
}
119+
}
120+
96121
func TestRecordHistoryEventSetsTime(t *testing.T) {
97122
f := &Function{}
98123
f.RecordHistoryEvent("test event")

0 commit comments

Comments
 (0)