blob: 1b589c6530430a94ec49ae0235d780b6c37ec53e [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package task_test
import (
"testing"
"time"
"android.googlesource.com/platform/tools/gpu/framework/assert"
"android.googlesource.com/platform/tools/gpu/framework/log"
"android.googlesource.com/platform/tools/gpu/framework/task"
"golang.org/x/net/context"
)
func TestContextCancel(t *testing.T) {
ctx := assert.Context(t)
before := log.Background()
after, cancel := task.WithCancel(before)
assert.For(ctx, "Cancel builds new context").That(before.Unwrap()).NotEquals(after.Unwrap())
assert.For(ctx, "Stopped before cancel").That(task.Stopped(after)).Equals(false)
assert.For(ctx, "StopReason before cancel").That(task.StopReason(after)).IsNil()
cancel()
assert.For(ctx, "Stopped after cancel").That(task.Stopped(after)).Equals(true)
assert.For(ctx, "StopReason after cancel").That(task.StopReason(after)).Equals(context.Canceled)
}
func TestContextTimeout(t *testing.T) {
ctx := assert.Context(t)
before := log.Background()
duration := ExpectBlocking
after, _ := task.WithTimeout(before, duration)
select {
case <-task.ShouldStop(after):
// expected
case <-time.After(ExpectBlocking + ExpectNonBlocking):
// should have cancelled by now
ctx.Error().Log("context was not cancelled in time")
}
}