diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-08-22 21:12:12 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-08-22 21:12:12 -0400 |
| commit | d8743bbc53c8db43151ef89d21264b531bea3d4c (patch) | |
| tree | ba941b7aa904bbde985d3a76ca90028f4bf8a475 /gui_test.go | |
| parent | 59cf78dbab8549761207b706f9856898cb5cd9c2 (diff) | |
| download | gui-d8743bbc53c8db43151ef89d21264b531bea3d4c.zip | |
test attachHandler
Diffstat (limited to 'gui_test.go')
| -rw-r--r-- | gui_test.go | 27 |
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 + } +} |