blob: 90f203e66e741b18e33560cbb349e0b592a7c5b3 [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 log_test
import (
"fmt"
"strings"
"testing"
"android.googlesource.com/platform/tools/gpu/framework/log"
"golang.org/x/net/context"
)
func TestContext(t *testing.T) {
background := log.Background()
if !strings.Contains(fmt.Sprint(background.Unwrap()), fmt.Sprint(context.Background())) {
t.Errorf("log.Background did not use context.Background")
}
todo := log.TODO()
if !strings.Contains(fmt.Sprint(todo.Unwrap()), fmt.Sprint(context.TODO())) {
t.Errorf("log.TODO did not use context.TODO`")
}
if background.Unwrap() == todo.Unwrap() {
t.Errorf("TODO and Background use the same context")
}
}
func TestContextNotContext(t *testing.T) {
// the context package is very fussy about the types that actually implement context.Context
// all cancellation behaviour is broken if you implement your own version
// it would be very easy to accidentally pass a log.Context to something that expected a context.Context, so to make
// sure that never happens we enforce that it does not satisfy the interface, and expect log.Context.Unwrap to be
// explicitly called when needed.
var ctx interface{} = log.Background()
var ok bool
if _, ok = ctx.(context.Context); ok {
t.Error("log.Context should not implement context.Context")
}
}