From 642bcfd76e105de1490fb35953485630be6f8336 Mon Sep 17 00:00:00 2001 From: Agustin Krapovickas Date: Sun, 10 Sep 2023 17:01:17 -0300 Subject: [PATCH] Makes event timer stop idempotent --- internal/event_timer.go | 6 +++++- internal/event_timer_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 internal/event_timer_test.go diff --git a/internal/event_timer.go b/internal/event_timer.go index 0b7630cbc..442fb60e8 100644 --- a/internal/event_timer.go +++ b/internal/event_timer.go @@ -10,6 +10,7 @@ type EventTimer struct { timer *time.Timer done chan struct{} wg sync.WaitGroup + once sync.Once } func NewEventTimer(task func()) *EventTimer { @@ -45,7 +46,10 @@ func (t *EventTimer) Stop() { return } - close(t.done) + t.once.Do(func() { + close(t.done) + }) + t.wg.Wait() } diff --git a/internal/event_timer_test.go b/internal/event_timer_test.go new file mode 100644 index 000000000..452fef6ce --- /dev/null +++ b/internal/event_timer_test.go @@ -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() +}