diff --git a/lib/rob/schedulers/v1/scheduler_test.go b/lib/rob/schedulers/v1/scheduler_test.go index 9413fae9be5fe03a2547da94b0adad2eba631ab5..609dd01961d54c269c2d98a82e112bc2a71b44a6 100644 --- a/lib/rob/schedulers/v1/scheduler_test.go +++ b/lib/rob/schedulers/v1/scheduler_test.go @@ -42,7 +42,10 @@ func testSink(sched *Scheduler, table *ShareTable) { w := sink.Read() switch v := w.(type) { case Work: - time.Sleep(v.Duration) + // dummy load + start := time.Now() + for time.Since(start) < v.Duration { + } table.Inc(v.Sender) close(v.Done) } @@ -82,9 +85,44 @@ func TestScheduler(t *testing.T) { log.Println("share of 1:", t1) log.Println("share of 2:", t2) log.Println("share of 3:", t3) - log.Println("total:", - time.Duration((t0+t1)*10)*time.Millisecond+ - time.Duration(t2*50)*time.Millisecond+ - time.Duration(t3*100)*time.Millisecond, - ) + + /* + Expectations: + - 0 and 1 should be similar and have roughly 10x of 3 + - 2 should have about twice as many executions as 3 + */ +} + +func TestScheduler_Late(t *testing.T) { + var table ShareTable + sched := NewScheduler() + go testSink(sched, &table) + + go testSource(sched, 0, 10*time.Millisecond) + go testSource(sched, 1, 10*time.Millisecond) + + time.Sleep(10 * time.Second) + + go testSource(sched, 2, 10*time.Millisecond) + go testSource(sched, 3, 10*time.Millisecond) + + time.Sleep(10 * time.Second) + t0 := table.Get(0) + t1 := table.Get(1) + t2 := table.Get(2) + t3 := table.Get(3) + log.Println("share of 0:", t0) + log.Println("share of 1:", t1) + log.Println("share of 2:", t2) + log.Println("share of 3:", t3) + + /* + Expectations: + - 0 and 1 should be similar + - 2 and 3 should be similar + - 0 and 1 should have more than three times as many executions as 2 and 3 + + IF THEY ARE ROUGHLY SIMILAR, THIS TEST IS A FAIL!!!! 0 AND 1 SHOULD NOT STALL WHEN 2 AND 3 ARE INTRODUCED + i need to make these automatic, but it's easy enough to eyeball it + */ } diff --git a/lib/rob/schedulers/v1/sink.go b/lib/rob/schedulers/v1/sink.go index 947d9e7c9c21d7e6089bb3d72a4a8e0c0ea52380..7efedfd8515a5c97218120cac5cc49cc2e8ac5f6 100644 --- a/lib/rob/schedulers/v1/sink.go +++ b/lib/rob/schedulers/v1/sink.go @@ -18,6 +18,7 @@ type Sink struct { start time.Time queue rbtree.RBTree[time.Duration, *Source] + floor time.Duration ready chan struct{} mu sync.Mutex @@ -51,6 +52,11 @@ func (T *Sink) _enqueue(source *Source) { runtime, _ := T.runtime[source.id] + if runtime < T.floor { + runtime = T.floor + T.runtime[source.id] = runtime + } + for { // find unique runtime (usually will only run once) if v, ok := T.queue.Get(runtime); ok { @@ -99,6 +105,7 @@ func (T *Sink) Read() any { continue } T.queue.Delete(runtime) + T.floor = runtime break }