From d8743bbc53c8db43151ef89d21264b531bea3d4c Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 22 Aug 2024 21:12:12 -0400 Subject: test attachHandler --- gui_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 gui_test.go (limited to 'gui_test.go') diff --git a/gui_test.go b/gui_test.go new file mode 100644 index 0000000..584544a --- /dev/null +++ b/gui_test.go @@ -0,0 +1,27 @@ +package gui + +import "time" + +const timeout = 1 * time.Second + +// trySend returns true if v can be sent to c within timeout, or false otherwise. +func trySend[T any](c chan<- T, v T, timeout time.Duration) bool { + timer := time.NewTimer(timeout) + select { + case c <- v: + return true + case <-timer.C: + return false + } +} + +// tryRecv returns the value received from c, or false if no value is received within timeout. +func tryRecv[T any](c <-chan T, timeout time.Duration) (*T, bool) { + timer := time.NewTimer(timeout) + select { + case v := <-c: + return &v, true + case <-timer.C: + return nil, false + } +} -- cgit v1.2.3