Skip to content

Commit

Permalink
Makes event timer stop idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
agukrapo committed Sep 10, 2023
1 parent ab15e58 commit 642bcfd
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 [email protected] 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 642bcfd

Please sign in to comment.