Skip to content

Commit

Permalink
Merge pull request #580 from agukrapo/event_timer_stop_panic
Browse files Browse the repository at this point in the history
Makes event timer stop idempotent
  • Loading branch information
ackleymi committed Oct 25, 2023
2 parents e4cffe0 + 04812c8 commit 1a31b37
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/event_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type EventTimer struct {
timer *time.Timer
done chan struct{}
wg sync.WaitGroup
once sync.Once
}

func NewEventTimer(task func()) *EventTimer {
Expand Down Expand Up @@ -45,7 +46,10 @@ func (t *EventTimer) Stop() {
return
}

close(t.done)
t.once.Do(func() {
close(t.done)
})

t.wg.Wait()
}

Expand Down
27 changes: 27 additions & 0 deletions internal/event_timer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact ask@quickfixengine.org if any conditions of this licensing
// are not clear to you.

package internal

import (
"testing"
)

func TestEventTimer_Stop_idempotent(*testing.T) {
t := NewEventTimer(func() {})

t.Stop()
t.Stop()
}

0 comments on commit 1a31b37

Please sign in to comment.