From 27491bfd8202f16c54adfac247e483e3a6e98a81 Mon Sep 17 00:00:00 2001 From: Leonid Titov Date: Thu, 22 Aug 2024 19:02:45 +0300 Subject: [PATCH] fix if only one target --- replicount/01-sim_test.go | 8 +++++++- replicount/replicount.go | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/replicount/01-sim_test.go b/replicount/01-sim_test.go index c6c7808..272a62d 100644 --- a/replicount/01-sim_test.go +++ b/replicount/01-sim_test.go @@ -30,6 +30,10 @@ func Test_001(t *testing.T) { } func Test_002(t *testing.T) { + replSet0 := []string{ + "0004a", + } + replSet1 := []string{ "0001", "0002", @@ -55,7 +59,9 @@ func Test_002(t *testing.T) { r.PollFunc = func() (idPtr *string) { var id string - if time.Now().Before(t0.Add(time.Second * 40)) { + if time.Now().Before(t0.Add(time.Second * 5)) { + id = replSet1[rand.Intn(len(replSet0))] + } else if time.Now().Before(t0.Add(time.Second * 40)) { id = replSet1[rand.Intn(len(replSet1))] } else { id = replSet2[rand.Intn(len(replSet2))] diff --git a/replicount/replicount.go b/replicount/replicount.go index 113899a..ada6f90 100644 --- a/replicount/replicount.go +++ b/replicount/replicount.go @@ -62,7 +62,7 @@ func New(of ...func(r *Replicount)) (r *Replicount) { state: mode_Slow, mainTimer: timmer.New(), fastModeLengthTimer: timmer.New(), - currentResult: &ChangeableObject{NumberOfReplicas: 1}, + currentResult: &ChangeableObject{NumberOfReplicas: 0}, } for _, f := range of { f(r) @@ -155,7 +155,7 @@ func (r *Replicount) scheduler() { } r.mainTimer.Restart( // immediate race-restart of the main timer r.SlowPollPeriodPerReplica / - time.Duration(r.currentResult.NumberOfReplicas) / + time.Duration(atLeastOne(r.currentResult.NumberOfReplicas)) / time.Duration(r.FastModeSpeedMultiple), ) r.fastModeLengthTimer.Restart(r.fastModeLength) @@ -163,7 +163,7 @@ func (r *Replicount) scheduler() { } // dqf func d := r.SlowPollPeriodPerReplica / - time.Duration(r.currentResult.NumberOfReplicas) + time.Duration(atLeastOne(r.currentResult.NumberOfReplicas)) r.mainTimer.Restart(d) }() // go func @@ -196,7 +196,7 @@ func (r *Replicount) scheduler() { } } // dqf func d := r.SlowPollPeriodPerReplica / - time.Duration(r.currentResult.NumberOfReplicas) / + time.Duration(atLeastOne(r.currentResult.NumberOfReplicas)) / time.Duration(r.FastModeSpeedMultiple) r.mainTimer.Restart(d) }() // go func @@ -240,3 +240,11 @@ func (r *Replicount) log(msg string) { r.LogFunc(msg) } } + +func atLeastOne(i int) int { + if i < 1 { + return 1 + } else { + return i + } +}