aboutsummaryrefslogtreecommitdiffstats
path: root/gui_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'gui_test.go')
-rw-r--r--gui_test.go27
1 files changed, 27 insertions, 0 deletions
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
+ }
+}