From e33b7c05563c67d58a7c070d2ceb12416c5b1a0a Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 1 Dec 2023 20:13:53 -0600 Subject: [PATCH] util: added test --- util/util_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 util/util_test.go diff --git a/util/util_test.go b/util/util_test.go new file mode 100644 index 0000000..b92be3c --- /dev/null +++ b/util/util_test.go @@ -0,0 +1,35 @@ +package util_test + +import ( + "sync" + "testing" + "time" + + "github.com/CPunch/gopenfusion/util" + "github.com/matryer/is" +) + +func TestWaitWithTimeout(t *testing.T) { + is := is.New(t) + wg := &sync.WaitGroup{} + go func() { + time.Sleep(2 * time.Second) + wg.Done() + }() + + wg.Add(1) + is.True(!util.WaitWithTimeout(wg, 1)) // timeout should occur + is.True(util.WaitWithTimeout(wg, 2)) // timeout shouldn't occur +} + +func TestSelectWithTimeout(t *testing.T) { + is := is.New(t) + ch := make(chan struct{}) + go func() { + time.Sleep(2 * time.Second) + close(ch) + }() + + is.True(!util.SelectWithTimeout(ch, 1)) // timeout should occur + is.True(util.SelectWithTimeout(ch, 2)) // timeout shouldn't occur +}