|
| 1 | +//go:build unit |
| 2 | + |
| 3 | +package service |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func accWithWindowEnd(id int64, end *time.Time) accountWithLoad { |
| 13 | + return accountWithLoad{ |
| 14 | + account: &Account{ |
| 15 | + ID: id, |
| 16 | + Schedulable: true, |
| 17 | + Status: StatusActive, |
| 18 | + SessionWindowEnd: end, |
| 19 | + }, |
| 20 | + loadInfo: &AccountLoadInfo{AccountID: id}, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestFilterBySoonestReset_PicksSoonestFutureWindow(t *testing.T) { |
| 25 | + now := time.Now() |
| 26 | + soon := now.Add(1 * time.Hour) |
| 27 | + later := now.Add(24 * time.Hour) |
| 28 | + accounts := []accountWithLoad{ |
| 29 | + accWithWindowEnd(1, testTimePtr(later)), |
| 30 | + accWithWindowEnd(2, testTimePtr(soon)), |
| 31 | + accWithWindowEnd(3, testTimePtr(later)), |
| 32 | + } |
| 33 | + got := filterBySoonestReset(accounts) |
| 34 | + require.Len(t, got, 1) |
| 35 | + require.Equal(t, int64(2), got[0].account.ID, "重置时间最早的账号被选中") |
| 36 | +} |
| 37 | + |
| 38 | +func TestFilterBySoonestReset_IgnoresNilAndExpiredWindows(t *testing.T) { |
| 39 | + now := time.Now() |
| 40 | + expired := now.Add(-1 * time.Hour) |
| 41 | + active := now.Add(2 * time.Hour) |
| 42 | + accounts := []accountWithLoad{ |
| 43 | + accWithWindowEnd(1, nil), // 无活跃窗口 |
| 44 | + accWithWindowEnd(2, testTimePtr(expired)), // 已过期,视为无活跃窗口 |
| 45 | + accWithWindowEnd(3, testTimePtr(active)), // 唯一活跃窗口 |
| 46 | + } |
| 47 | + got := filterBySoonestReset(accounts) |
| 48 | + require.Len(t, got, 1) |
| 49 | + require.Equal(t, int64(3), got[0].account.ID, "仅保留拥有未来重置时间的账号") |
| 50 | +} |
| 51 | + |
| 52 | +func TestFilterBySoonestReset_NoActiveWindowReturnsAll(t *testing.T) { |
| 53 | + now := time.Now() |
| 54 | + expired := now.Add(-30 * time.Minute) |
| 55 | + accounts := []accountWithLoad{ |
| 56 | + accWithWindowEnd(1, nil), |
| 57 | + accWithWindowEnd(2, testTimePtr(expired)), |
| 58 | + } |
| 59 | + got := filterBySoonestReset(accounts) |
| 60 | + require.Len(t, got, 2, "没有任何账号拥有活跃窗口时,返回原集合不做过滤") |
| 61 | +} |
| 62 | + |
| 63 | +func TestFilterBySoonestReset_TiedSoonestKeepsAll(t *testing.T) { |
| 64 | + now := time.Now() |
| 65 | + end := now.Add(90 * time.Minute) |
| 66 | + accounts := []accountWithLoad{ |
| 67 | + accWithWindowEnd(1, testTimePtr(end)), |
| 68 | + accWithWindowEnd(2, testTimePtr(end)), |
| 69 | + accWithWindowEnd(3, testTimePtr(now.Add(5*time.Hour))), |
| 70 | + } |
| 71 | + got := filterBySoonestReset(accounts) |
| 72 | + require.Len(t, got, 2, "并列最早重置的账号都保留,交由后续 LRU 决定") |
| 73 | + ids := map[int64]bool{got[0].account.ID: true, got[1].account.ID: true} |
| 74 | + require.True(t, ids[1] && ids[2]) |
| 75 | +} |
| 76 | + |
| 77 | +func TestFilterBySoonestReset_SingleOrEmptyUnchanged(t *testing.T) { |
| 78 | + require.Empty(t, filterBySoonestReset(nil)) |
| 79 | + single := []accountWithLoad{accWithWindowEnd(1, nil)} |
| 80 | + require.Len(t, filterBySoonestReset(single), 1) |
| 81 | +} |
0 commit comments