Fix race in mock mode and address frontend type checks

This commit is contained in:
rcourtman 2025-10-16 08:28:17 +00:00
parent 6fdef61710
commit 61b492e93b
2 changed files with 19 additions and 15 deletions

View file

@ -12,11 +12,11 @@ import {
pathForTab, pathForTab,
tabFromPath, tabFromPath,
} from '../Alerts'; } from '../Alerts';
import type { RawOverrideConfig } from '@/types/alerts';
describe('normalizeMetricDelayMap', () => { describe('normalizeMetricDelayMap', () => {
it('returns empty object when input is nullish', () => { it('returns empty object when input is nullish', () => {
expect(normalizeMetricDelayMap(undefined)).toEqual({}); expect(normalizeMetricDelayMap(undefined)).toEqual({});
// @ts-expect-error - testing runtime null handling
expect(normalizeMetricDelayMap(null)).toEqual({}); expect(normalizeMetricDelayMap(null)).toEqual({});
}); });
@ -139,17 +139,19 @@ describe('threshold helper utilities', () => {
it('extracts trigger values and ignores non-threshold keys', () => { it('extracts trigger values and ignores non-threshold keys', () => {
const result = extractTriggerValues({ const result = extractTriggerValues({
cpu: { trigger: 80, clear: 70 }, cpu: { trigger: 80, clear: 70 },
memory: 85, memory: { trigger: 85, clear: 75 },
disabled: true, disabled: true,
poweredOffSeverity: 'warning', poweredOffSeverity: 'warning',
networkIn: false, customFlag: true,
customLegacy: 42,
label: 'ignored', label: 'ignored',
}); } as RawOverrideConfig);
expect(result).toEqual({ expect(result).toEqual({
cpu: 80, cpu: 80,
memory: 85, memory: 85,
networkIn: 0, customFlag: 0,
customLegacy: 42,
}); });
}); });

View file

@ -120,29 +120,31 @@ func disableMockMode() {
func startUpdateLoopLocked() { func startUpdateLoopLocked() {
stopUpdateLoopLocked() stopUpdateLoopLocked()
stopUpdatesCh = make(chan struct{}) stopCh := make(chan struct{})
updateTicker = time.NewTicker(updateInterval) ticker := time.NewTicker(updateInterval)
stopUpdatesCh = stopCh
updateTicker = ticker
go func() { go func(stop <-chan struct{}, tick *time.Ticker) {
for { for {
select { select {
case <-updateTicker.C: case <-tick.C:
cfg := GetConfig() cfg := GetConfig()
updateMetrics(cfg) updateMetrics(cfg)
case <-stopUpdatesCh: case <-stop:
return return
} }
} }
}() }(stopCh, ticker)
} }
func stopUpdateLoopLocked() { func stopUpdateLoopLocked() {
if updateTicker != nil { if ticker := updateTicker; ticker != nil {
updateTicker.Stop() ticker.Stop()
updateTicker = nil updateTicker = nil
} }
if stopUpdatesCh != nil { if ch := stopUpdatesCh; ch != nil {
close(stopUpdatesCh) close(ch)
stopUpdatesCh = nil stopUpdatesCh = nil
} }
} }