From 4c8992a07cfafd46392f693d83ebb97aa320e207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Wed, 31 Jan 2024 11:03:52 +0800 Subject: [PATCH] feat(coroutine): Added `Wait` (#85) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flcă‚› --- coroutine/wait.go | 13 +++++++++++++ coroutine/wait_test.go | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 coroutine/wait.go create mode 100644 coroutine/wait_test.go diff --git a/coroutine/wait.go b/coroutine/wait.go new file mode 100644 index 00000000..cba97016 --- /dev/null +++ b/coroutine/wait.go @@ -0,0 +1,13 @@ +package coroutine + +import "sync" + +func Wait(fn func()) { + var wg sync.WaitGroup + wg.Add(1) + go func() { + fn() + wg.Done() + }() + wg.Wait() +} diff --git a/coroutine/wait_test.go b/coroutine/wait_test.go new file mode 100644 index 00000000..a6465069 --- /dev/null +++ b/coroutine/wait_test.go @@ -0,0 +1,19 @@ +package coroutine + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestWait(t *testing.T) { + var msg string + + Wait(func() { + time.Sleep(time.Second) + msg = "hello" + }) + + assert.Equal(t, "hello", msg) +}