diff --git a/packages/flamenco-manager-go/src/flamenco-manager/flamenco/scheduler_test.go b/packages/flamenco-manager-go/src/flamenco-manager/flamenco/scheduler_test.go index a5c9238f7c763c6b4325c6ffe4e717052f191434..83d819ec7ce548447f94b31e1810f69a9c64b781 100644 --- a/packages/flamenco-manager-go/src/flamenco-manager/flamenco/scheduler_test.go +++ b/packages/flamenco-manager-go/src/flamenco-manager/flamenco/scheduler_test.go @@ -156,6 +156,7 @@ func (s *SchedulerTestSuite) TestSchedulerVerifyUpstreamCanceled(t *check.C) { } timeout := TimeoutAfter(1 * time.Second) + defer close(timeout) // Mock that the task with highest priority was actually canceled on the Server. httpmock.RegisterResponder( @@ -207,6 +208,7 @@ func (s *SchedulerTestSuite) TestSchedulerVerifyUpstreamPrioChange(t *check.C) { } timeout := TimeoutAfter(1 * time.Second) + defer close(timeout) // Mock that the task with highest priority was actually canceled on the Server. httpmock.RegisterResponder( @@ -264,6 +266,7 @@ func (s *SchedulerTestSuite) TestSchedulerVerifyUpstreamDeleted(t *check.C) { } timeout := TimeoutAfter(1 * time.Second) + defer close(timeout) // Mock that the task with highest priority was actually canceled on the Server. httpmock.RegisterResponder( diff --git a/packages/flamenco-manager-go/src/flamenco-manager/flamenco/task_updates_test.go b/packages/flamenco-manager-go/src/flamenco-manager/flamenco/task_updates_test.go index f912e85f763e4a923e7197fe344fb4087733f47f..48b3245d1001c816f6ac7b3d226b9da91f56a51e 100644 --- a/packages/flamenco-manager-go/src/flamenco-manager/flamenco/task_updates_test.go +++ b/packages/flamenco-manager-go/src/flamenco-manager/flamenco/task_updates_test.go @@ -53,6 +53,7 @@ func (s *TaskUpdatesTestSuite) TestCancelRunningTasks(t *check.C) { } timeout := TimeoutAfter(1 * time.Second) + defer close(timeout) // Mock that the task with highest priority was actually canceled on the Server. httpmock.RegisterResponder( @@ -81,7 +82,6 @@ func (s *TaskUpdatesTestSuite) TestCancelRunningTasks(t *check.C) { timedout := <-timeout assert.False(t, timedout, "HTTP POST to Flamenco Server not performed") - close(timeout) // Give the tup.Go() coroutine (and subsequent calls) time to run. // the "timeout <- false" call in the responder is triggered before diff --git a/packages/flamenco-manager-go/src/flamenco-manager/flamenco/timer.go b/packages/flamenco-manager-go/src/flamenco-manager/flamenco/timer.go index cbd638587045818fc9ac5ae781b2fd6fec4587ea..467ded9e2496bd884462b239b0c0358ff264bcfe 100644 --- a/packages/flamenco-manager-go/src/flamenco-manager/flamenco/timer.go +++ b/packages/flamenco-manager-go/src/flamenco-manager/flamenco/timer.go @@ -88,11 +88,23 @@ func UtcNow() *time.Time { return &now } +/* Sends a 'true' to the channel after the given timeout. + * Send a 'false' to the channel yourself if you want to notify the receiver that + * a timeout didn't happen. + * + * The channel is buffered with size 2, so both your 'false' and this routine's 'true' + * write won't block. + */ func TimeoutAfter(duration time.Duration) chan bool { - timeout := make(chan bool, 1) + timeout := make(chan bool, 2) go func() { time.Sleep(duration) + defer func() { + // Recover from a panic. This panic can happen when the caller closed the + // channel while we were sleeping. + recover() + }() timeout <- true }()