Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes event timer stop idempotent #580

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading