summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2023-02-10 18:13:03 -0500
committerWill Hawkins <[email protected]>2023-02-10 18:13:03 -0500
commit0eafb34d7e2c9971c9341878a7c1d3ffa034418b (patch)
tree1ce060ef70254b465baedfc535d418ee231f6aeb
parentc8350c13a09b8c19656cd32a065b69693b6117c5 (diff)
[Cleanup] Update timeoutat.TimeoutAt to remove extraneous go func
Also, add a test for timeoutat.TimeoutAt.
-rw-r--r--timeoutat/timeoutat.go24
-rw-r--r--timeoutat/timeoutat_test.go27
2 files changed, 38 insertions, 13 deletions
diff --git a/timeoutat/timeoutat.go b/timeoutat/timeoutat.go
index bf8227e..d61dba2 100644
--- a/timeoutat/timeoutat.go
+++ b/timeoutat/timeoutat.go
@@ -29,19 +29,17 @@ func TimeoutAt(
) (response chan interface{}) {
response = make(chan interface{})
go func(ctx context.Context) {
- go func() {
- if debug.IsDebug(debugLevel) {
- fmt.Printf("Timeout expected to end at %v\n", when)
- }
- select {
- case <-time.After(when.Sub(time.Now())):
- case <-ctx.Done():
- }
- response <- struct{}{}
- if debug.IsDebug(debugLevel) {
- fmt.Printf("Timeout ended at %v\n", time.Now())
- }
- }()
+ if debug.IsDebug(debugLevel) {
+ fmt.Printf("Timeout expected to end at %v\n", when)
+ }
+ select {
+ case <-time.After(when.Sub(time.Now())):
+ case <-ctx.Done():
+ }
+ response <- struct{}{}
+ if debug.IsDebug(debugLevel) {
+ fmt.Printf("Timeout ended at %v\n", time.Now())
+ }
}(ctx)
return
}
diff --git a/timeoutat/timeoutat_test.go b/timeoutat/timeoutat_test.go
new file mode 100644
index 0000000..b0c91fe
--- /dev/null
+++ b/timeoutat/timeoutat_test.go
@@ -0,0 +1,27 @@
+package timeoutat
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/network-quality/goresponsiveness/debug"
+)
+
+func TestTimeoutAt(t *testing.T) {
+ testTime := 5 * time.Second
+ testTimeLimit := 6 * time.Second
+
+ now := time.Now()
+ select {
+ case <-TimeoutAt(context.Background(), time.Now().Add(testTime), debug.NoDebug):
+
+ }
+ then := time.Now()
+
+ actualTime := then.Sub(now)
+
+ if actualTime >= testTimeLimit {
+ t.Fatalf("Should have taken 5 seconds but it really took %v!", actualTime)
+ }
+}