From e2a0431496a34276610c63504f4ff88e44d2f1b1 Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Tue, 16 May 2023 17:40:42 -0500
Subject: [PATCH] works error free with uniswap stuff

---
 lib/rob/schedulers/v1/pool/sink/sink.go | 11 +++++--
 lib/rob/schedulers/v1/scheduler_test.go | 40 +++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/lib/rob/schedulers/v1/pool/sink/sink.go b/lib/rob/schedulers/v1/pool/sink/sink.go
index d512ca00..52b67bdb 100644
--- a/lib/rob/schedulers/v1/pool/sink/sink.go
+++ b/lib/rob/schedulers/v1/pool/sink/sink.go
@@ -75,8 +75,15 @@ func (T *Sink) trySchedule(j job.Stalled) bool {
 		return false
 	}
 
-	stride := T.stride[j.Source]
-	if stride < T.floor {
+	stride, ok := T.stride[j.Source]
+	if !ok {
+		// set to max
+		stride = T.floor
+		if s, _, ok := T.scheduled.Max(); ok {
+			stride = s + 1
+		}
+		T.stride[j.Source] = stride
+	} else if stride < T.floor {
 		stride = T.floor
 		T.stride[j.Source] = stride
 	}
diff --git a/lib/rob/schedulers/v1/scheduler_test.go b/lib/rob/schedulers/v1/scheduler_test.go
index 5fe4d55e..da5f41a7 100644
--- a/lib/rob/schedulers/v1/scheduler_test.go
+++ b/lib/rob/schedulers/v1/scheduler_test.go
@@ -79,6 +79,17 @@ func testSource(sched *Scheduler, id int, dur time.Duration, constraints rob.Con
 	}
 }
 
+func testStarver(sched *Scheduler, id int, dur time.Duration, constraints rob.Constraints) {
+	for {
+		source := sched.NewSource()
+		w := Work{
+			Sender:   id,
+			Duration: dur,
+		}
+		source.Do(constraints, w)
+	}
+}
+
 func similar(v0, v1 int, vn ...int) bool {
 	const margin = 0.05 // 5% margin of error
 
@@ -361,3 +372,32 @@ func TestScheduler_LateSink(t *testing.T) {
 
 	t.Log("share of 0:", t0)
 }
+
+func TestScheduler_Starve(t *testing.T) {
+	var table ShareTable
+	sched := NewScheduler()
+
+	testSink(sched, &table, 0)
+
+	go testStarver(sched, 1, 10*time.Millisecond, 0)
+	go testStarver(sched, 2, 10*time.Millisecond, 0)
+	go testSource(sched, 0, 10*time.Millisecond, 0)
+
+	time.Sleep(20 * time.Second)
+	t0 := table.Get(0)
+	t1 := table.Get(1)
+	t2 := table.Get(2)
+
+	/*
+		Expectations:
+		- 0 should not be starved
+	*/
+
+	t.Log("share of 0:", t0)
+	t.Log("share of 1:", t1)
+	t.Log("share of 2:", t2)
+
+	if !similar(t0, t1, t2) {
+		t.Error("expected all executions to be similar (is 0 starving?)")
+	}
+}
-- 
GitLab