mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 17:50:12 +00:00
fix(mcp): harden dashboard dispatch lifecycle
Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type linearizationClock struct {
|
||||
mu sync.Mutex
|
||||
times []time.Time
|
||||
latest time.Time
|
||||
}
|
||||
|
||||
func newLinearizationClock(oldTime, newTime time.Time) *linearizationClock {
|
||||
return &linearizationClock{
|
||||
times: []time.Time{oldTime, oldTime, newTime},
|
||||
latest: newTime,
|
||||
}
|
||||
}
|
||||
|
||||
func (clock *linearizationClock) Now() time.Time {
|
||||
clock.mu.Lock()
|
||||
now := clock.latest
|
||||
if len(clock.times) > 0 {
|
||||
now = clock.times[0]
|
||||
clock.times = clock.times[1:]
|
||||
}
|
||||
clock.mu.Unlock()
|
||||
return now
|
||||
}
|
||||
|
||||
func TestLinearizationClockReturnsLatestTimeAfterSequenceIsConsumed(t *testing.T) {
|
||||
oldTime := time.Unix(1_700_000_000, 0)
|
||||
newTime := oldTime.Add(time.Second)
|
||||
clock := newLinearizationClock(oldTime, newTime)
|
||||
|
||||
for range 3 {
|
||||
clock.Now()
|
||||
}
|
||||
|
||||
if got := clock.Now(); !got.Equal(newTime) {
|
||||
t.Fatalf("clock returned %s after sequence was consumed, want %s", got, newTime)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user