Skip to content

Commit 1a31b37

Browse files
authored
Merge pull request #580 from agukrapo/event_timer_stop_panic
Makes event timer stop idempotent
2 parents e4cffe0 + 04812c8 commit 1a31b37

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

internal/event_timer.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type EventTimer struct {
1010
timer *time.Timer
1111
done chan struct{}
1212
wg sync.WaitGroup
13+
once sync.Once
1314
}
1415

1516
func NewEventTimer(task func()) *EventTimer {
@@ -45,7 +46,10 @@ func (t *EventTimer) Stop() {
4546
return
4647
}
4748

48-
close(t.done)
49+
t.once.Do(func() {
50+
close(t.done)
51+
})
52+
4953
t.wg.Wait()
5054
}
5155

internal/event_timer_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) quickfixengine.org All rights reserved.
2+
//
3+
// This file may be distributed under the terms of the quickfixengine.org
4+
// license as defined by quickfixengine.org and appearing in the file
5+
// LICENSE included in the packaging of this file.
6+
//
7+
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
8+
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
9+
// PARTICULAR PURPOSE.
10+
//
11+
// See http://www.quickfixengine.org/LICENSE for licensing information.
12+
//
13+
// Contact [email protected] if any conditions of this licensing
14+
// are not clear to you.
15+
16+
package internal
17+
18+
import (
19+
"testing"
20+
)
21+
22+
func TestEventTimer_Stop_idempotent(*testing.T) {
23+
t := NewEventTimer(func() {})
24+
25+
t.Stop()
26+
t.Stop()
27+
}

0 commit comments

Comments
 (0)