Skip to content

Commit

Permalink
adjust simple sync (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakoush committed Oct 2, 2024
1 parent 8fa535a commit d5788ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 2 additions & 6 deletions scheduler/pkg/synchroniser/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewSimpleSynchroniser(timeout time.Duration) *SimpleSynchroniser {
s.isReady.Store(false)
s.triggered.Store(false)
s.wg.Add(1)
time.AfterFunc(s.timeout, s.done)
return s
}

Expand All @@ -48,12 +49,7 @@ func (s *SimpleSynchroniser) IsReady() bool {
}

func (s *SimpleSynchroniser) Signals(_ uint) {
if !s.IsReady() {
swapped := s.triggered.CompareAndSwap(false, true) // make sure we run only once
if swapped {
time.AfterFunc(s.timeout, s.done)
}
}
// pass
}

func (s *SimpleSynchroniser) WaitReady() {
Expand Down
22 changes: 15 additions & 7 deletions scheduler/pkg/synchroniser/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,29 @@ func TestSimpleSynchroniser(t *testing.T) {
type test struct {
name string
timeout time.Duration
signals uint
signal bool
}

tests := []test{
{
name: "Simple",
timeout: 100 * time.Millisecond,
signals: 1,
signal: true,
},
{
name: "Longer timeout",
timeout: 500 * time.Millisecond,
signals: 1,
signal: true,
},
{
name: "No timer",
timeout: 0 * time.Millisecond,
signals: 1,
signal: true,
},
{
name: "No signal",
timeout: 100 * time.Millisecond,
signal: false,
},
}

Expand All @@ -51,14 +56,17 @@ func TestSimpleSynchroniser(t *testing.T) {
s := NewSimpleSynchroniser(test.timeout)
startTime := time.Now()
g.Expect(s.IsReady()).To(BeFalse())
s.Signals(test.signals)
// this should have no effect
s.Signals(100000)
if test.signal {
s.Signals(1)
}
s.WaitReady()
elapsed := time.Since(startTime)
g.Expect(s.IsReady()).To(BeTrue())
g.Expect(elapsed).To(BeNumerically(">", test.timeout))

// this should have no effect
s.Signals(100000)
g.Expect(s.IsReady()).To(BeTrue())
// make sure we are graceful after this point
s.Signals(10)
g.Expect(s.IsReady()).To(BeTrue())
Expand Down

0 comments on commit d5788ed

Please sign in to comment.