commit
						9b9ea55797
					
				|  | @ -79,6 +79,10 @@ | ||||||
| 			"ImportPath": "github.com/yvasiyarov/newrelic_platform_go", | 			"ImportPath": "github.com/yvasiyarov/newrelic_platform_go", | ||||||
| 			"Rev": "b21fdbd4370f3717f3bbd2bf41c223bc273068e6" | 			"Rev": "b21fdbd4370f3717f3bbd2bf41c223bc273068e6" | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			"ImportPath": "golang.org/x/net/context", | ||||||
|  | 			"Rev": "1dfe7915deaf3f80b962c163b918868d8a6d8974" | ||||||
|  | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			"ImportPath": "gopkg.in/check.v1", | 			"ImportPath": "gopkg.in/check.v1", | ||||||
| 			"Rev": "64131543e7896d5bcc6bd5a76287eb75ea96c673" | 			"Rev": "64131543e7896d5bcc6bd5a76287eb75ea96c673" | ||||||
|  |  | ||||||
|  | @ -0,0 +1,432 @@ | ||||||
|  | // Copyright 2014 The Go Authors. All rights reserved.
 | ||||||
|  | // Use of this source code is governed by a BSD-style
 | ||||||
|  | // license that can be found in the LICENSE file.
 | ||||||
|  | 
 | ||||||
|  | // Package context defines the Context type, which carries deadlines,
 | ||||||
|  | // cancelation signals, and other request-scoped values across API boundaries
 | ||||||
|  | // and between processes.
 | ||||||
|  | //
 | ||||||
|  | // Incoming requests to a server should create a Context, and outgoing calls to
 | ||||||
|  | // servers should accept a Context.  The chain of function calls between must
 | ||||||
|  | // propagate the Context, optionally replacing it with a modified copy created
 | ||||||
|  | // using WithDeadline, WithTimeout, WithCancel, or WithValue.
 | ||||||
|  | //
 | ||||||
|  | // Programs that use Contexts should follow these rules to keep interfaces
 | ||||||
|  | // consistent across packages and enable static analysis tools to check context
 | ||||||
|  | // propagation:
 | ||||||
|  | //
 | ||||||
|  | // Do not store Contexts inside a struct type; instead, pass a Context
 | ||||||
|  | // explicitly to each function that needs it.  The Context should be the first
 | ||||||
|  | // parameter, typically named ctx:
 | ||||||
|  | //
 | ||||||
|  | // 	func DoSomething(ctx context.Context, arg Arg) error {
 | ||||||
|  | // 		// ... use ctx ...
 | ||||||
|  | // 	}
 | ||||||
|  | //
 | ||||||
|  | // Do not pass a nil Context, even if a function permits it.  Pass context.TODO
 | ||||||
|  | // if you are unsure about which Context to use.
 | ||||||
|  | //
 | ||||||
|  | // Use context Values only for request-scoped data that transits processes and
 | ||||||
|  | // APIs, not for passing optional parameters to functions.
 | ||||||
|  | //
 | ||||||
|  | // The same Context may be passed to functions running in different goroutines;
 | ||||||
|  | // Contexts are safe for simultaneous use by multiple goroutines.
 | ||||||
|  | //
 | ||||||
|  | // See http://blog.golang.org/context for example code for a server that uses
 | ||||||
|  | // Contexts.
 | ||||||
|  | package context | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"sync" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // A Context carries a deadline, a cancelation signal, and other values across
 | ||||||
|  | // API boundaries.
 | ||||||
|  | //
 | ||||||
|  | // Context's methods may be called by multiple goroutines simultaneously.
 | ||||||
|  | type Context interface { | ||||||
|  | 	// Deadline returns the time when work done on behalf of this context
 | ||||||
|  | 	// should be canceled.  Deadline returns ok==false when no deadline is
 | ||||||
|  | 	// set.  Successive calls to Deadline return the same results.
 | ||||||
|  | 	Deadline() (deadline time.Time, ok bool) | ||||||
|  | 
 | ||||||
|  | 	// Done returns a channel that's closed when work done on behalf of this
 | ||||||
|  | 	// context should be canceled.  Done may return nil if this context can
 | ||||||
|  | 	// never be canceled.  Successive calls to Done return the same value.
 | ||||||
|  | 	//
 | ||||||
|  | 	// WithCancel arranges for Done to be closed when cancel is called;
 | ||||||
|  | 	// WithDeadline arranges for Done to be closed when the deadline
 | ||||||
|  | 	// expires; WithTimeout arranges for Done to be closed when the timeout
 | ||||||
|  | 	// elapses.
 | ||||||
|  | 	//
 | ||||||
|  | 	// Done is provided for use in select statements:
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// DoSomething calls DoSomethingSlow and returns as soon as
 | ||||||
|  | 	// 	// it returns or ctx.Done is closed.
 | ||||||
|  | 	// 	func DoSomething(ctx context.Context) (Result, error) {
 | ||||||
|  | 	// 		c := make(chan Result, 1)
 | ||||||
|  | 	// 		go func() { c <- DoSomethingSlow(ctx) }()
 | ||||||
|  | 	// 		select {
 | ||||||
|  | 	// 		case res := <-c:
 | ||||||
|  | 	// 			return res, nil
 | ||||||
|  | 	// 		case <-ctx.Done():
 | ||||||
|  | 	// 			return nil, ctx.Err()
 | ||||||
|  | 	// 		}
 | ||||||
|  | 	// 	}
 | ||||||
|  | 	//
 | ||||||
|  | 	// See http://blog.golang.org/pipelines for more examples of how to use
 | ||||||
|  | 	// a Done channel for cancelation.
 | ||||||
|  | 	Done() <-chan struct{} | ||||||
|  | 
 | ||||||
|  | 	// Err returns a non-nil error value after Done is closed.  Err returns
 | ||||||
|  | 	// Canceled if the context was canceled or DeadlineExceeded if the
 | ||||||
|  | 	// context's deadline passed.  No other values for Err are defined.
 | ||||||
|  | 	// After Done is closed, successive calls to Err return the same value.
 | ||||||
|  | 	Err() error | ||||||
|  | 
 | ||||||
|  | 	// Value returns the value associated with this context for key, or nil
 | ||||||
|  | 	// if no value is associated with key.  Successive calls to Value with
 | ||||||
|  | 	// the same key returns the same result.
 | ||||||
|  | 	//
 | ||||||
|  | 	// Use context values only for request-scoped data that transits
 | ||||||
|  | 	// processes and API boundaries, not for passing optional parameters to
 | ||||||
|  | 	// functions.
 | ||||||
|  | 	//
 | ||||||
|  | 	// A key identifies a specific value in a Context.  Functions that wish
 | ||||||
|  | 	// to store values in Context typically allocate a key in a global
 | ||||||
|  | 	// variable then use that key as the argument to context.WithValue and
 | ||||||
|  | 	// Context.Value.  A key can be any type that supports equality;
 | ||||||
|  | 	// packages should define keys as an unexported type to avoid
 | ||||||
|  | 	// collisions.
 | ||||||
|  | 	//
 | ||||||
|  | 	// Packages that define a Context key should provide type-safe accessors
 | ||||||
|  | 	// for the values stores using that key:
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// Package user defines a User type that's stored in Contexts.
 | ||||||
|  | 	// 	package user
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	import "golang.org/x/net/context"
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// User is the type of value stored in the Contexts.
 | ||||||
|  | 	// 	type User struct {...}
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// key is an unexported type for keys defined in this package.
 | ||||||
|  | 	// 	// This prevents collisions with keys defined in other packages.
 | ||||||
|  | 	// 	type key int
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// userKey is the key for user.User values in Contexts.  It is
 | ||||||
|  | 	// 	// unexported; clients use user.NewContext and user.FromContext
 | ||||||
|  | 	// 	// instead of using this key directly.
 | ||||||
|  | 	// 	var userKey key = 0
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// NewContext returns a new Context that carries value u.
 | ||||||
|  | 	// 	func NewContext(ctx context.Context, u *User) context.Context {
 | ||||||
|  | 	// 		return context.WithValue(ctx, userKey, u)
 | ||||||
|  | 	// 	}
 | ||||||
|  | 	//
 | ||||||
|  | 	// 	// FromContext returns the User value stored in ctx, if any.
 | ||||||
|  | 	// 	func FromContext(ctx context.Context) (*User, bool) {
 | ||||||
|  | 	// 		u, ok := ctx.Value(userKey).(*User)
 | ||||||
|  | 	// 		return u, ok
 | ||||||
|  | 	// 	}
 | ||||||
|  | 	Value(key interface{}) interface{} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Canceled is the error returned by Context.Err when the context is canceled.
 | ||||||
|  | var Canceled = errors.New("context canceled") | ||||||
|  | 
 | ||||||
|  | // DeadlineExceeded is the error returned by Context.Err when the context's
 | ||||||
|  | // deadline passes.
 | ||||||
|  | var DeadlineExceeded = errors.New("context deadline exceeded") | ||||||
|  | 
 | ||||||
|  | // An emptyCtx is never canceled, has no values, and has no deadline.  It is not
 | ||||||
|  | // struct{}, since vars of this type must have distinct addresses.
 | ||||||
|  | type emptyCtx int | ||||||
|  | 
 | ||||||
|  | func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (*emptyCtx) Done() <-chan struct{} { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (*emptyCtx) Err() error { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (*emptyCtx) Value(key interface{}) interface{} { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (e *emptyCtx) String() string { | ||||||
|  | 	switch e { | ||||||
|  | 	case background: | ||||||
|  | 		return "context.Background" | ||||||
|  | 	case todo: | ||||||
|  | 		return "context.TODO" | ||||||
|  | 	} | ||||||
|  | 	return "unknown empty Context" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | var ( | ||||||
|  | 	background = new(emptyCtx) | ||||||
|  | 	todo       = new(emptyCtx) | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // Background returns a non-nil, empty Context. It is never canceled, has no
 | ||||||
|  | // values, and has no deadline.  It is typically used by the main function,
 | ||||||
|  | // initialization, and tests, and as the top-level Context for incoming
 | ||||||
|  | // requests.
 | ||||||
|  | func Background() Context { | ||||||
|  | 	return background | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // TODO returns a non-nil, empty Context.  Code should use context.TODO when
 | ||||||
|  | // it's unclear which Context to use or it's is not yet available (because the
 | ||||||
|  | // surrounding function has not yet been extended to accept a Context
 | ||||||
|  | // parameter).  TODO is recognized by static analysis tools that determine
 | ||||||
|  | // whether Contexts are propagated correctly in a program.
 | ||||||
|  | func TODO() Context { | ||||||
|  | 	return todo | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // A CancelFunc tells an operation to abandon its work.
 | ||||||
|  | // A CancelFunc does not wait for the work to stop.
 | ||||||
|  | // After the first call, subsequent calls to a CancelFunc do nothing.
 | ||||||
|  | type CancelFunc func() | ||||||
|  | 
 | ||||||
|  | // WithCancel returns a copy of parent with a new Done channel. The returned
 | ||||||
|  | // context's Done channel is closed when the returned cancel function is called
 | ||||||
|  | // or when the parent context's Done channel is closed, whichever happens first.
 | ||||||
|  | func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { | ||||||
|  | 	c := newCancelCtx(parent) | ||||||
|  | 	propagateCancel(parent, &c) | ||||||
|  | 	return &c, func() { c.cancel(true, Canceled) } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // newCancelCtx returns an initialized cancelCtx.
 | ||||||
|  | func newCancelCtx(parent Context) cancelCtx { | ||||||
|  | 	return cancelCtx{ | ||||||
|  | 		Context: parent, | ||||||
|  | 		done:    make(chan struct{}), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // propagateCancel arranges for child to be canceled when parent is.
 | ||||||
|  | func propagateCancel(parent Context, child canceler) { | ||||||
|  | 	if parent.Done() == nil { | ||||||
|  | 		return // parent is never canceled
 | ||||||
|  | 	} | ||||||
|  | 	if p, ok := parentCancelCtx(parent); ok { | ||||||
|  | 		p.mu.Lock() | ||||||
|  | 		if p.err != nil { | ||||||
|  | 			// parent has already been canceled
 | ||||||
|  | 			child.cancel(false, p.err) | ||||||
|  | 		} else { | ||||||
|  | 			if p.children == nil { | ||||||
|  | 				p.children = make(map[canceler]bool) | ||||||
|  | 			} | ||||||
|  | 			p.children[child] = true | ||||||
|  | 		} | ||||||
|  | 		p.mu.Unlock() | ||||||
|  | 	} else { | ||||||
|  | 		go func() { | ||||||
|  | 			select { | ||||||
|  | 			case <-parent.Done(): | ||||||
|  | 				child.cancel(false, parent.Err()) | ||||||
|  | 			case <-child.Done(): | ||||||
|  | 			} | ||||||
|  | 		}() | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // parentCancelCtx follows a chain of parent references until it finds a
 | ||||||
|  | // *cancelCtx.  This function understands how each of the concrete types in this
 | ||||||
|  | // package represents its parent.
 | ||||||
|  | func parentCancelCtx(parent Context) (*cancelCtx, bool) { | ||||||
|  | 	for { | ||||||
|  | 		switch c := parent.(type) { | ||||||
|  | 		case *cancelCtx: | ||||||
|  | 			return c, true | ||||||
|  | 		case *timerCtx: | ||||||
|  | 			return &c.cancelCtx, true | ||||||
|  | 		case *valueCtx: | ||||||
|  | 			parent = c.Context | ||||||
|  | 		default: | ||||||
|  | 			return nil, false | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // A canceler is a context type that can be canceled directly.  The
 | ||||||
|  | // implementations are *cancelCtx and *timerCtx.
 | ||||||
|  | type canceler interface { | ||||||
|  | 	cancel(removeFromParent bool, err error) | ||||||
|  | 	Done() <-chan struct{} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // A cancelCtx can be canceled.  When canceled, it also cancels any children
 | ||||||
|  | // that implement canceler.
 | ||||||
|  | type cancelCtx struct { | ||||||
|  | 	Context | ||||||
|  | 
 | ||||||
|  | 	done chan struct{} // closed by the first cancel call.
 | ||||||
|  | 
 | ||||||
|  | 	mu       sync.Mutex | ||||||
|  | 	children map[canceler]bool // set to nil by the first cancel call
 | ||||||
|  | 	err      error             // set to non-nil by the first cancel call
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *cancelCtx) Done() <-chan struct{} { | ||||||
|  | 	return c.done | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *cancelCtx) Err() error { | ||||||
|  | 	c.mu.Lock() | ||||||
|  | 	defer c.mu.Unlock() | ||||||
|  | 	return c.err | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *cancelCtx) String() string { | ||||||
|  | 	return fmt.Sprintf("%v.WithCancel", c.Context) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // cancel closes c.done, cancels each of c's children, and, if
 | ||||||
|  | // removeFromParent is true, removes c from its parent's children.
 | ||||||
|  | func (c *cancelCtx) cancel(removeFromParent bool, err error) { | ||||||
|  | 	if err == nil { | ||||||
|  | 		panic("context: internal error: missing cancel error") | ||||||
|  | 	} | ||||||
|  | 	c.mu.Lock() | ||||||
|  | 	if c.err != nil { | ||||||
|  | 		c.mu.Unlock() | ||||||
|  | 		return // already canceled
 | ||||||
|  | 	} | ||||||
|  | 	c.err = err | ||||||
|  | 	close(c.done) | ||||||
|  | 	for child := range c.children { | ||||||
|  | 		// NOTE: acquiring the child's lock while holding parent's lock.
 | ||||||
|  | 		child.cancel(false, err) | ||||||
|  | 	} | ||||||
|  | 	c.children = nil | ||||||
|  | 	c.mu.Unlock() | ||||||
|  | 
 | ||||||
|  | 	if removeFromParent { | ||||||
|  | 		if p, ok := parentCancelCtx(c.Context); ok { | ||||||
|  | 			p.mu.Lock() | ||||||
|  | 			if p.children != nil { | ||||||
|  | 				delete(p.children, c) | ||||||
|  | 			} | ||||||
|  | 			p.mu.Unlock() | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // WithDeadline returns a copy of the parent context with the deadline adjusted
 | ||||||
|  | // to be no later than d.  If the parent's deadline is already earlier than d,
 | ||||||
|  | // WithDeadline(parent, d) is semantically equivalent to parent.  The returned
 | ||||||
|  | // context's Done channel is closed when the deadline expires, when the returned
 | ||||||
|  | // cancel function is called, or when the parent context's Done channel is
 | ||||||
|  | // closed, whichever happens first.
 | ||||||
|  | //
 | ||||||
|  | // Canceling this context releases resources associated with the deadline
 | ||||||
|  | // timer, so code should call cancel as soon as the operations running in this
 | ||||||
|  | // Context complete.
 | ||||||
|  | func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) { | ||||||
|  | 	if cur, ok := parent.Deadline(); ok && cur.Before(deadline) { | ||||||
|  | 		// The current deadline is already sooner than the new one.
 | ||||||
|  | 		return WithCancel(parent) | ||||||
|  | 	} | ||||||
|  | 	c := &timerCtx{ | ||||||
|  | 		cancelCtx: newCancelCtx(parent), | ||||||
|  | 		deadline:  deadline, | ||||||
|  | 	} | ||||||
|  | 	propagateCancel(parent, c) | ||||||
|  | 	d := deadline.Sub(time.Now()) | ||||||
|  | 	if d <= 0 { | ||||||
|  | 		c.cancel(true, DeadlineExceeded) // deadline has already passed
 | ||||||
|  | 		return c, func() { c.cancel(true, Canceled) } | ||||||
|  | 	} | ||||||
|  | 	c.mu.Lock() | ||||||
|  | 	defer c.mu.Unlock() | ||||||
|  | 	if c.err == nil { | ||||||
|  | 		c.timer = time.AfterFunc(d, func() { | ||||||
|  | 			c.cancel(true, DeadlineExceeded) | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | 	return c, func() { c.cancel(true, Canceled) } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // A timerCtx carries a timer and a deadline.  It embeds a cancelCtx to
 | ||||||
|  | // implement Done and Err.  It implements cancel by stopping its timer then
 | ||||||
|  | // delegating to cancelCtx.cancel.
 | ||||||
|  | type timerCtx struct { | ||||||
|  | 	cancelCtx | ||||||
|  | 	timer *time.Timer // Under cancelCtx.mu.
 | ||||||
|  | 
 | ||||||
|  | 	deadline time.Time | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *timerCtx) Deadline() (deadline time.Time, ok bool) { | ||||||
|  | 	return c.deadline, true | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *timerCtx) String() string { | ||||||
|  | 	return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now())) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *timerCtx) cancel(removeFromParent bool, err error) { | ||||||
|  | 	c.cancelCtx.cancel(removeFromParent, err) | ||||||
|  | 	c.mu.Lock() | ||||||
|  | 	if c.timer != nil { | ||||||
|  | 		c.timer.Stop() | ||||||
|  | 		c.timer = nil | ||||||
|  | 	} | ||||||
|  | 	c.mu.Unlock() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
 | ||||||
|  | //
 | ||||||
|  | // Canceling this context releases resources associated with the deadline
 | ||||||
|  | // timer, so code should call cancel as soon as the operations running in this
 | ||||||
|  | // Context complete:
 | ||||||
|  | //
 | ||||||
|  | // 	func slowOperationWithTimeout(ctx context.Context) (Result, error) {
 | ||||||
|  | // 		ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
 | ||||||
|  | // 		defer cancel()  // releases resources if slowOperation completes before timeout elapses
 | ||||||
|  | // 		return slowOperation(ctx)
 | ||||||
|  | // 	}
 | ||||||
|  | func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { | ||||||
|  | 	return WithDeadline(parent, time.Now().Add(timeout)) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // WithValue returns a copy of parent in which the value associated with key is
 | ||||||
|  | // val.
 | ||||||
|  | //
 | ||||||
|  | // Use context Values only for request-scoped data that transits processes and
 | ||||||
|  | // APIs, not for passing optional parameters to functions.
 | ||||||
|  | func WithValue(parent Context, key interface{}, val interface{}) Context { | ||||||
|  | 	return &valueCtx{parent, key, val} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // A valueCtx carries a key-value pair.  It implements Value for that key and
 | ||||||
|  | // delegates all other calls to the embedded Context.
 | ||||||
|  | type valueCtx struct { | ||||||
|  | 	Context | ||||||
|  | 	key, val interface{} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *valueCtx) String() string { | ||||||
|  | 	return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *valueCtx) Value(key interface{}) interface{} { | ||||||
|  | 	if c.key == key { | ||||||
|  | 		return c.val | ||||||
|  | 	} | ||||||
|  | 	return c.Context.Value(key) | ||||||
|  | } | ||||||
|  | @ -0,0 +1,553 @@ | ||||||
|  | // Copyright 2014 The Go Authors. All rights reserved.
 | ||||||
|  | // Use of this source code is governed by a BSD-style
 | ||||||
|  | // license that can be found in the LICENSE file.
 | ||||||
|  | 
 | ||||||
|  | package context | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"math/rand" | ||||||
|  | 	"runtime" | ||||||
|  | 	"strings" | ||||||
|  | 	"sync" | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // otherContext is a Context that's not one of the types defined in context.go.
 | ||||||
|  | // This lets us test code paths that differ based on the underlying type of the
 | ||||||
|  | // Context.
 | ||||||
|  | type otherContext struct { | ||||||
|  | 	Context | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestBackground(t *testing.T) { | ||||||
|  | 	c := Background() | ||||||
|  | 	if c == nil { | ||||||
|  | 		t.Fatalf("Background returned nil") | ||||||
|  | 	} | ||||||
|  | 	select { | ||||||
|  | 	case x := <-c.Done(): | ||||||
|  | 		t.Errorf("<-c.Done() == %v want nothing (it should block)", x) | ||||||
|  | 	default: | ||||||
|  | 	} | ||||||
|  | 	if got, want := fmt.Sprint(c), "context.Background"; got != want { | ||||||
|  | 		t.Errorf("Background().String() = %q want %q", got, want) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestTODO(t *testing.T) { | ||||||
|  | 	c := TODO() | ||||||
|  | 	if c == nil { | ||||||
|  | 		t.Fatalf("TODO returned nil") | ||||||
|  | 	} | ||||||
|  | 	select { | ||||||
|  | 	case x := <-c.Done(): | ||||||
|  | 		t.Errorf("<-c.Done() == %v want nothing (it should block)", x) | ||||||
|  | 	default: | ||||||
|  | 	} | ||||||
|  | 	if got, want := fmt.Sprint(c), "context.TODO"; got != want { | ||||||
|  | 		t.Errorf("TODO().String() = %q want %q", got, want) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestWithCancel(t *testing.T) { | ||||||
|  | 	c1, cancel := WithCancel(Background()) | ||||||
|  | 
 | ||||||
|  | 	if got, want := fmt.Sprint(c1), "context.Background.WithCancel"; got != want { | ||||||
|  | 		t.Errorf("c1.String() = %q want %q", got, want) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	o := otherContext{c1} | ||||||
|  | 	c2, _ := WithCancel(o) | ||||||
|  | 	contexts := []Context{c1, o, c2} | ||||||
|  | 
 | ||||||
|  | 	for i, c := range contexts { | ||||||
|  | 		if d := c.Done(); d == nil { | ||||||
|  | 			t.Errorf("c[%d].Done() == %v want non-nil", i, d) | ||||||
|  | 		} | ||||||
|  | 		if e := c.Err(); e != nil { | ||||||
|  | 			t.Errorf("c[%d].Err() == %v want nil", i, e) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		select { | ||||||
|  | 		case x := <-c.Done(): | ||||||
|  | 			t.Errorf("<-c.Done() == %v want nothing (it should block)", x) | ||||||
|  | 		default: | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	cancel() | ||||||
|  | 	time.Sleep(100 * time.Millisecond) // let cancelation propagate
 | ||||||
|  | 
 | ||||||
|  | 	for i, c := range contexts { | ||||||
|  | 		select { | ||||||
|  | 		case <-c.Done(): | ||||||
|  | 		default: | ||||||
|  | 			t.Errorf("<-c[%d].Done() blocked, but shouldn't have", i) | ||||||
|  | 		} | ||||||
|  | 		if e := c.Err(); e != Canceled { | ||||||
|  | 			t.Errorf("c[%d].Err() == %v want %v", i, e, Canceled) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestParentFinishesChild(t *testing.T) { | ||||||
|  | 	// Context tree:
 | ||||||
|  | 	// parent -> cancelChild
 | ||||||
|  | 	// parent -> valueChild -> timerChild
 | ||||||
|  | 	parent, cancel := WithCancel(Background()) | ||||||
|  | 	cancelChild, stop := WithCancel(parent) | ||||||
|  | 	defer stop() | ||||||
|  | 	valueChild := WithValue(parent, "key", "value") | ||||||
|  | 	timerChild, stop := WithTimeout(valueChild, 10000*time.Hour) | ||||||
|  | 	defer stop() | ||||||
|  | 
 | ||||||
|  | 	select { | ||||||
|  | 	case x := <-parent.Done(): | ||||||
|  | 		t.Errorf("<-parent.Done() == %v want nothing (it should block)", x) | ||||||
|  | 	case x := <-cancelChild.Done(): | ||||||
|  | 		t.Errorf("<-cancelChild.Done() == %v want nothing (it should block)", x) | ||||||
|  | 	case x := <-timerChild.Done(): | ||||||
|  | 		t.Errorf("<-timerChild.Done() == %v want nothing (it should block)", x) | ||||||
|  | 	case x := <-valueChild.Done(): | ||||||
|  | 		t.Errorf("<-valueChild.Done() == %v want nothing (it should block)", x) | ||||||
|  | 	default: | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// The parent's children should contain the two cancelable children.
 | ||||||
|  | 	pc := parent.(*cancelCtx) | ||||||
|  | 	cc := cancelChild.(*cancelCtx) | ||||||
|  | 	tc := timerChild.(*timerCtx) | ||||||
|  | 	pc.mu.Lock() | ||||||
|  | 	if len(pc.children) != 2 || !pc.children[cc] || !pc.children[tc] { | ||||||
|  | 		t.Errorf("bad linkage: pc.children = %v, want %v and %v", | ||||||
|  | 			pc.children, cc, tc) | ||||||
|  | 	} | ||||||
|  | 	pc.mu.Unlock() | ||||||
|  | 
 | ||||||
|  | 	if p, ok := parentCancelCtx(cc.Context); !ok || p != pc { | ||||||
|  | 		t.Errorf("bad linkage: parentCancelCtx(cancelChild.Context) = %v, %v want %v, true", p, ok, pc) | ||||||
|  | 	} | ||||||
|  | 	if p, ok := parentCancelCtx(tc.Context); !ok || p != pc { | ||||||
|  | 		t.Errorf("bad linkage: parentCancelCtx(timerChild.Context) = %v, %v want %v, true", p, ok, pc) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	cancel() | ||||||
|  | 
 | ||||||
|  | 	pc.mu.Lock() | ||||||
|  | 	if len(pc.children) != 0 { | ||||||
|  | 		t.Errorf("pc.cancel didn't clear pc.children = %v", pc.children) | ||||||
|  | 	} | ||||||
|  | 	pc.mu.Unlock() | ||||||
|  | 
 | ||||||
|  | 	// parent and children should all be finished.
 | ||||||
|  | 	check := func(ctx Context, name string) { | ||||||
|  | 		select { | ||||||
|  | 		case <-ctx.Done(): | ||||||
|  | 		default: | ||||||
|  | 			t.Errorf("<-%s.Done() blocked, but shouldn't have", name) | ||||||
|  | 		} | ||||||
|  | 		if e := ctx.Err(); e != Canceled { | ||||||
|  | 			t.Errorf("%s.Err() == %v want %v", name, e, Canceled) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	check(parent, "parent") | ||||||
|  | 	check(cancelChild, "cancelChild") | ||||||
|  | 	check(valueChild, "valueChild") | ||||||
|  | 	check(timerChild, "timerChild") | ||||||
|  | 
 | ||||||
|  | 	// WithCancel should return a canceled context on a canceled parent.
 | ||||||
|  | 	precanceledChild := WithValue(parent, "key", "value") | ||||||
|  | 	select { | ||||||
|  | 	case <-precanceledChild.Done(): | ||||||
|  | 	default: | ||||||
|  | 		t.Errorf("<-precanceledChild.Done() blocked, but shouldn't have") | ||||||
|  | 	} | ||||||
|  | 	if e := precanceledChild.Err(); e != Canceled { | ||||||
|  | 		t.Errorf("precanceledChild.Err() == %v want %v", e, Canceled) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestChildFinishesFirst(t *testing.T) { | ||||||
|  | 	cancelable, stop := WithCancel(Background()) | ||||||
|  | 	defer stop() | ||||||
|  | 	for _, parent := range []Context{Background(), cancelable} { | ||||||
|  | 		child, cancel := WithCancel(parent) | ||||||
|  | 
 | ||||||
|  | 		select { | ||||||
|  | 		case x := <-parent.Done(): | ||||||
|  | 			t.Errorf("<-parent.Done() == %v want nothing (it should block)", x) | ||||||
|  | 		case x := <-child.Done(): | ||||||
|  | 			t.Errorf("<-child.Done() == %v want nothing (it should block)", x) | ||||||
|  | 		default: | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		cc := child.(*cancelCtx) | ||||||
|  | 		pc, pcok := parent.(*cancelCtx) // pcok == false when parent == Background()
 | ||||||
|  | 		if p, ok := parentCancelCtx(cc.Context); ok != pcok || (ok && pc != p) { | ||||||
|  | 			t.Errorf("bad linkage: parentCancelCtx(cc.Context) = %v, %v want %v, %v", p, ok, pc, pcok) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if pcok { | ||||||
|  | 			pc.mu.Lock() | ||||||
|  | 			if len(pc.children) != 1 || !pc.children[cc] { | ||||||
|  | 				t.Errorf("bad linkage: pc.children = %v, cc = %v", pc.children, cc) | ||||||
|  | 			} | ||||||
|  | 			pc.mu.Unlock() | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		cancel() | ||||||
|  | 
 | ||||||
|  | 		if pcok { | ||||||
|  | 			pc.mu.Lock() | ||||||
|  | 			if len(pc.children) != 0 { | ||||||
|  | 				t.Errorf("child's cancel didn't remove self from pc.children = %v", pc.children) | ||||||
|  | 			} | ||||||
|  | 			pc.mu.Unlock() | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// child should be finished.
 | ||||||
|  | 		select { | ||||||
|  | 		case <-child.Done(): | ||||||
|  | 		default: | ||||||
|  | 			t.Errorf("<-child.Done() blocked, but shouldn't have") | ||||||
|  | 		} | ||||||
|  | 		if e := child.Err(); e != Canceled { | ||||||
|  | 			t.Errorf("child.Err() == %v want %v", e, Canceled) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// parent should not be finished.
 | ||||||
|  | 		select { | ||||||
|  | 		case x := <-parent.Done(): | ||||||
|  | 			t.Errorf("<-parent.Done() == %v want nothing (it should block)", x) | ||||||
|  | 		default: | ||||||
|  | 		} | ||||||
|  | 		if e := parent.Err(); e != nil { | ||||||
|  | 			t.Errorf("parent.Err() == %v want nil", e) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func testDeadline(c Context, wait time.Duration, t *testing.T) { | ||||||
|  | 	select { | ||||||
|  | 	case <-time.After(wait): | ||||||
|  | 		t.Fatalf("context should have timed out") | ||||||
|  | 	case <-c.Done(): | ||||||
|  | 	} | ||||||
|  | 	if e := c.Err(); e != DeadlineExceeded { | ||||||
|  | 		t.Errorf("c.Err() == %v want %v", e, DeadlineExceeded) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestDeadline(t *testing.T) { | ||||||
|  | 	c, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) | ||||||
|  | 	if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) { | ||||||
|  | 		t.Errorf("c.String() = %q want prefix %q", got, prefix) | ||||||
|  | 	} | ||||||
|  | 	testDeadline(c, 200*time.Millisecond, t) | ||||||
|  | 
 | ||||||
|  | 	c, _ = WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) | ||||||
|  | 	o := otherContext{c} | ||||||
|  | 	testDeadline(o, 200*time.Millisecond, t) | ||||||
|  | 
 | ||||||
|  | 	c, _ = WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) | ||||||
|  | 	o = otherContext{c} | ||||||
|  | 	c, _ = WithDeadline(o, time.Now().Add(300*time.Millisecond)) | ||||||
|  | 	testDeadline(c, 200*time.Millisecond, t) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestTimeout(t *testing.T) { | ||||||
|  | 	c, _ := WithTimeout(Background(), 100*time.Millisecond) | ||||||
|  | 	if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) { | ||||||
|  | 		t.Errorf("c.String() = %q want prefix %q", got, prefix) | ||||||
|  | 	} | ||||||
|  | 	testDeadline(c, 200*time.Millisecond, t) | ||||||
|  | 
 | ||||||
|  | 	c, _ = WithTimeout(Background(), 100*time.Millisecond) | ||||||
|  | 	o := otherContext{c} | ||||||
|  | 	testDeadline(o, 200*time.Millisecond, t) | ||||||
|  | 
 | ||||||
|  | 	c, _ = WithTimeout(Background(), 100*time.Millisecond) | ||||||
|  | 	o = otherContext{c} | ||||||
|  | 	c, _ = WithTimeout(o, 300*time.Millisecond) | ||||||
|  | 	testDeadline(c, 200*time.Millisecond, t) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestCanceledTimeout(t *testing.T) { | ||||||
|  | 	c, _ := WithTimeout(Background(), 200*time.Millisecond) | ||||||
|  | 	o := otherContext{c} | ||||||
|  | 	c, cancel := WithTimeout(o, 400*time.Millisecond) | ||||||
|  | 	cancel() | ||||||
|  | 	time.Sleep(100 * time.Millisecond) // let cancelation propagate
 | ||||||
|  | 	select { | ||||||
|  | 	case <-c.Done(): | ||||||
|  | 	default: | ||||||
|  | 		t.Errorf("<-c.Done() blocked, but shouldn't have") | ||||||
|  | 	} | ||||||
|  | 	if e := c.Err(); e != Canceled { | ||||||
|  | 		t.Errorf("c.Err() == %v want %v", e, Canceled) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type key1 int | ||||||
|  | type key2 int | ||||||
|  | 
 | ||||||
|  | var k1 = key1(1) | ||||||
|  | var k2 = key2(1) // same int as k1, different type
 | ||||||
|  | var k3 = key2(3) // same type as k2, different int
 | ||||||
|  | 
 | ||||||
|  | func TestValues(t *testing.T) { | ||||||
|  | 	check := func(c Context, nm, v1, v2, v3 string) { | ||||||
|  | 		if v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 { | ||||||
|  | 			t.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0) | ||||||
|  | 		} | ||||||
|  | 		if v, ok := c.Value(k2).(string); ok == (len(v2) == 0) || v != v2 { | ||||||
|  | 			t.Errorf(`%s.Value(k2).(string) = %q, %t want %q, %t`, nm, v, ok, v2, len(v2) != 0) | ||||||
|  | 		} | ||||||
|  | 		if v, ok := c.Value(k3).(string); ok == (len(v3) == 0) || v != v3 { | ||||||
|  | 			t.Errorf(`%s.Value(k3).(string) = %q, %t want %q, %t`, nm, v, ok, v3, len(v3) != 0) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	c0 := Background() | ||||||
|  | 	check(c0, "c0", "", "", "") | ||||||
|  | 
 | ||||||
|  | 	c1 := WithValue(Background(), k1, "c1k1") | ||||||
|  | 	check(c1, "c1", "c1k1", "", "") | ||||||
|  | 
 | ||||||
|  | 	if got, want := fmt.Sprint(c1), `context.Background.WithValue(1, "c1k1")`; got != want { | ||||||
|  | 		t.Errorf("c.String() = %q want %q", got, want) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	c2 := WithValue(c1, k2, "c2k2") | ||||||
|  | 	check(c2, "c2", "c1k1", "c2k2", "") | ||||||
|  | 
 | ||||||
|  | 	c3 := WithValue(c2, k3, "c3k3") | ||||||
|  | 	check(c3, "c2", "c1k1", "c2k2", "c3k3") | ||||||
|  | 
 | ||||||
|  | 	c4 := WithValue(c3, k1, nil) | ||||||
|  | 	check(c4, "c4", "", "c2k2", "c3k3") | ||||||
|  | 
 | ||||||
|  | 	o0 := otherContext{Background()} | ||||||
|  | 	check(o0, "o0", "", "", "") | ||||||
|  | 
 | ||||||
|  | 	o1 := otherContext{WithValue(Background(), k1, "c1k1")} | ||||||
|  | 	check(o1, "o1", "c1k1", "", "") | ||||||
|  | 
 | ||||||
|  | 	o2 := WithValue(o1, k2, "o2k2") | ||||||
|  | 	check(o2, "o2", "c1k1", "o2k2", "") | ||||||
|  | 
 | ||||||
|  | 	o3 := otherContext{c4} | ||||||
|  | 	check(o3, "o3", "", "c2k2", "c3k3") | ||||||
|  | 
 | ||||||
|  | 	o4 := WithValue(o3, k3, nil) | ||||||
|  | 	check(o4, "o4", "", "c2k2", "") | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestAllocs(t *testing.T) { | ||||||
|  | 	bg := Background() | ||||||
|  | 	for _, test := range []struct { | ||||||
|  | 		desc       string | ||||||
|  | 		f          func() | ||||||
|  | 		limit      float64 | ||||||
|  | 		gccgoLimit float64 | ||||||
|  | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			desc:       "Background()", | ||||||
|  | 			f:          func() { Background() }, | ||||||
|  | 			limit:      0, | ||||||
|  | 			gccgoLimit: 0, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			desc: fmt.Sprintf("WithValue(bg, %v, nil)", k1), | ||||||
|  | 			f: func() { | ||||||
|  | 				c := WithValue(bg, k1, nil) | ||||||
|  | 				c.Value(k1) | ||||||
|  | 			}, | ||||||
|  | 			limit:      3, | ||||||
|  | 			gccgoLimit: 3, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			desc: "WithTimeout(bg, 15*time.Millisecond)", | ||||||
|  | 			f: func() { | ||||||
|  | 				c, _ := WithTimeout(bg, 15*time.Millisecond) | ||||||
|  | 				<-c.Done() | ||||||
|  | 			}, | ||||||
|  | 			limit:      8, | ||||||
|  | 			gccgoLimit: 13, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			desc: "WithCancel(bg)", | ||||||
|  | 			f: func() { | ||||||
|  | 				c, cancel := WithCancel(bg) | ||||||
|  | 				cancel() | ||||||
|  | 				<-c.Done() | ||||||
|  | 			}, | ||||||
|  | 			limit:      5, | ||||||
|  | 			gccgoLimit: 8, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			desc: "WithTimeout(bg, 100*time.Millisecond)", | ||||||
|  | 			f: func() { | ||||||
|  | 				c, cancel := WithTimeout(bg, 100*time.Millisecond) | ||||||
|  | 				cancel() | ||||||
|  | 				<-c.Done() | ||||||
|  | 			}, | ||||||
|  | 			limit:      8, | ||||||
|  | 			gccgoLimit: 25, | ||||||
|  | 		}, | ||||||
|  | 	} { | ||||||
|  | 		limit := test.limit | ||||||
|  | 		if runtime.Compiler == "gccgo" { | ||||||
|  | 			// gccgo does not yet do escape analysis.
 | ||||||
|  | 			// TOOD(iant): Remove this when gccgo does do escape analysis.
 | ||||||
|  | 			limit = test.gccgoLimit | ||||||
|  | 		} | ||||||
|  | 		if n := testing.AllocsPerRun(100, test.f); n > limit { | ||||||
|  | 			t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit)) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestSimultaneousCancels(t *testing.T) { | ||||||
|  | 	root, cancel := WithCancel(Background()) | ||||||
|  | 	m := map[Context]CancelFunc{root: cancel} | ||||||
|  | 	q := []Context{root} | ||||||
|  | 	// Create a tree of contexts.
 | ||||||
|  | 	for len(q) != 0 && len(m) < 100 { | ||||||
|  | 		parent := q[0] | ||||||
|  | 		q = q[1:] | ||||||
|  | 		for i := 0; i < 4; i++ { | ||||||
|  | 			ctx, cancel := WithCancel(parent) | ||||||
|  | 			m[ctx] = cancel | ||||||
|  | 			q = append(q, ctx) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	// Start all the cancels in a random order.
 | ||||||
|  | 	var wg sync.WaitGroup | ||||||
|  | 	wg.Add(len(m)) | ||||||
|  | 	for _, cancel := range m { | ||||||
|  | 		go func(cancel CancelFunc) { | ||||||
|  | 			cancel() | ||||||
|  | 			wg.Done() | ||||||
|  | 		}(cancel) | ||||||
|  | 	} | ||||||
|  | 	// Wait on all the contexts in a random order.
 | ||||||
|  | 	for ctx := range m { | ||||||
|  | 		select { | ||||||
|  | 		case <-ctx.Done(): | ||||||
|  | 		case <-time.After(1 * time.Second): | ||||||
|  | 			buf := make([]byte, 10<<10) | ||||||
|  | 			n := runtime.Stack(buf, true) | ||||||
|  | 			t.Fatalf("timed out waiting for <-ctx.Done(); stacks:\n%s", buf[:n]) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	// Wait for all the cancel functions to return.
 | ||||||
|  | 	done := make(chan struct{}) | ||||||
|  | 	go func() { | ||||||
|  | 		wg.Wait() | ||||||
|  | 		close(done) | ||||||
|  | 	}() | ||||||
|  | 	select { | ||||||
|  | 	case <-done: | ||||||
|  | 	case <-time.After(1 * time.Second): | ||||||
|  | 		buf := make([]byte, 10<<10) | ||||||
|  | 		n := runtime.Stack(buf, true) | ||||||
|  | 		t.Fatalf("timed out waiting for cancel functions; stacks:\n%s", buf[:n]) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestInterlockedCancels(t *testing.T) { | ||||||
|  | 	parent, cancelParent := WithCancel(Background()) | ||||||
|  | 	child, cancelChild := WithCancel(parent) | ||||||
|  | 	go func() { | ||||||
|  | 		parent.Done() | ||||||
|  | 		cancelChild() | ||||||
|  | 	}() | ||||||
|  | 	cancelParent() | ||||||
|  | 	select { | ||||||
|  | 	case <-child.Done(): | ||||||
|  | 	case <-time.After(1 * time.Second): | ||||||
|  | 		buf := make([]byte, 10<<10) | ||||||
|  | 		n := runtime.Stack(buf, true) | ||||||
|  | 		t.Fatalf("timed out waiting for child.Done(); stacks:\n%s", buf[:n]) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestLayersCancel(t *testing.T) { | ||||||
|  | 	testLayers(t, time.Now().UnixNano(), false) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestLayersTimeout(t *testing.T) { | ||||||
|  | 	testLayers(t, time.Now().UnixNano(), true) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func testLayers(t *testing.T, seed int64, testTimeout bool) { | ||||||
|  | 	rand.Seed(seed) | ||||||
|  | 	errorf := func(format string, a ...interface{}) { | ||||||
|  | 		t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...) | ||||||
|  | 	} | ||||||
|  | 	const ( | ||||||
|  | 		timeout   = 200 * time.Millisecond | ||||||
|  | 		minLayers = 30 | ||||||
|  | 	) | ||||||
|  | 	type value int | ||||||
|  | 	var ( | ||||||
|  | 		vals      []*value | ||||||
|  | 		cancels   []CancelFunc | ||||||
|  | 		numTimers int | ||||||
|  | 		ctx       = Background() | ||||||
|  | 	) | ||||||
|  | 	for i := 0; i < minLayers || numTimers == 0 || len(cancels) == 0 || len(vals) == 0; i++ { | ||||||
|  | 		switch rand.Intn(3) { | ||||||
|  | 		case 0: | ||||||
|  | 			v := new(value) | ||||||
|  | 			ctx = WithValue(ctx, v, v) | ||||||
|  | 			vals = append(vals, v) | ||||||
|  | 		case 1: | ||||||
|  | 			var cancel CancelFunc | ||||||
|  | 			ctx, cancel = WithCancel(ctx) | ||||||
|  | 			cancels = append(cancels, cancel) | ||||||
|  | 		case 2: | ||||||
|  | 			var cancel CancelFunc | ||||||
|  | 			ctx, cancel = WithTimeout(ctx, timeout) | ||||||
|  | 			cancels = append(cancels, cancel) | ||||||
|  | 			numTimers++ | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	checkValues := func(when string) { | ||||||
|  | 		for _, key := range vals { | ||||||
|  | 			if val := ctx.Value(key).(*value); key != val { | ||||||
|  | 				errorf("%s: ctx.Value(%p) = %p want %p", when, key, val, key) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	select { | ||||||
|  | 	case <-ctx.Done(): | ||||||
|  | 		errorf("ctx should not be canceled yet") | ||||||
|  | 	default: | ||||||
|  | 	} | ||||||
|  | 	if s, prefix := fmt.Sprint(ctx), "context.Background."; !strings.HasPrefix(s, prefix) { | ||||||
|  | 		t.Errorf("ctx.String() = %q want prefix %q", s, prefix) | ||||||
|  | 	} | ||||||
|  | 	t.Log(ctx) | ||||||
|  | 	checkValues("before cancel") | ||||||
|  | 	if testTimeout { | ||||||
|  | 		select { | ||||||
|  | 		case <-ctx.Done(): | ||||||
|  | 		case <-time.After(timeout + timeout/10): | ||||||
|  | 			errorf("ctx should have timed out") | ||||||
|  | 		} | ||||||
|  | 		checkValues("after timeout") | ||||||
|  | 	} else { | ||||||
|  | 		cancel := cancels[rand.Intn(len(cancels))] | ||||||
|  | 		cancel() | ||||||
|  | 		select { | ||||||
|  | 		case <-ctx.Done(): | ||||||
|  | 		default: | ||||||
|  | 			errorf("ctx should be canceled") | ||||||
|  | 		} | ||||||
|  | 		checkValues("after cancel") | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										26
									
								
								Godeps/_workspace/src/golang.org/x/net/context/withtimeout_test.go
								
								
									generated
								
								
									vendored
								
								
									Normal file
								
							
							
						
						
									
										26
									
								
								Godeps/_workspace/src/golang.org/x/net/context/withtimeout_test.go
								
								
									generated
								
								
									vendored
								
								
									Normal file
								
							|  | @ -0,0 +1,26 @@ | ||||||
|  | // Copyright 2014 The Go Authors. All rights reserved.
 | ||||||
|  | // Use of this source code is governed by a BSD-style
 | ||||||
|  | // license that can be found in the LICENSE file.
 | ||||||
|  | 
 | ||||||
|  | package context_test | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
|  | 	"golang.org/x/net/context" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func ExampleWithTimeout() { | ||||||
|  | 	// Pass a context with a timeout to tell a blocking function that it
 | ||||||
|  | 	// should abandon its work after the timeout elapses.
 | ||||||
|  | 	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) | ||||||
|  | 	select { | ||||||
|  | 	case <-time.After(200 * time.Millisecond): | ||||||
|  | 		fmt.Println("overslept") | ||||||
|  | 	case <-ctx.Done(): | ||||||
|  | 		fmt.Println(ctx.Err()) // prints "context deadline exceeded"
 | ||||||
|  | 	} | ||||||
|  | 	// Output:
 | ||||||
|  | 	// context deadline exceeded
 | ||||||
|  | } | ||||||
							
								
								
									
										44
									
								
								auth/auth.go
								
								
								
								
							
							
						
						
									
										44
									
								
								auth/auth.go
								
								
								
								
							|  | @ -18,7 +18,7 @@ | ||||||
| //			resource := auth.Resource{Type: "customerOrder", Name: orderNumber}
 | //			resource := auth.Resource{Type: "customerOrder", Name: orderNumber}
 | ||||||
| // 			access := auth.Access{Resource: resource, Action: "update"}
 | // 			access := auth.Access{Resource: resource, Action: "update"}
 | ||||||
| //
 | //
 | ||||||
| // 			if err := accessController.Authorized(r, access); err != nil {
 | // 			if ctx, err := accessController.Authorized(ctx, access); err != nil {
 | ||||||
| //				if challenge, ok := err.(auth.Challenge) {
 | //				if challenge, ok := err.(auth.Challenge) {
 | ||||||
| //					// Let the challenge write the response.
 | //					// Let the challenge write the response.
 | ||||||
| //					challenge.ServeHTTP(w, r)
 | //					challenge.ServeHTTP(w, r)
 | ||||||
|  | @ -31,10 +31,35 @@ | ||||||
| package auth | package auth | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  | 
 | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // Common errors used with this package.
 | ||||||
|  | var ( | ||||||
|  | 	ErrNoRequestContext = errors.New("no http request in context") | ||||||
|  | 	ErrNoAuthUserInfo   = errors.New("no auth user info in context") | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // RequestFromContext returns the http request in the given context.
 | ||||||
|  | // Returns ErrNoRequestContext if the context does not have an http
 | ||||||
|  | // request associated with it.
 | ||||||
|  | func RequestFromContext(ctx context.Context) (*http.Request, error) { | ||||||
|  | 	if r, ok := ctx.Value("http.request").(*http.Request); r != nil && ok { | ||||||
|  | 		return r, nil | ||||||
|  | 	} | ||||||
|  | 	return nil, ErrNoRequestContext | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // UserInfo carries information about
 | ||||||
|  | // an autenticated/authorized client.
 | ||||||
|  | type UserInfo struct { | ||||||
|  | 	Name string | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // Resource describes a resource by type and name.
 | // Resource describes a resource by type and name.
 | ||||||
| type Resource struct { | type Resource struct { | ||||||
| 	Type string | 	Type string | ||||||
|  | @ -65,13 +90,16 @@ type Challenge interface { | ||||||
| // and required access levels for a request. Implementations can support both
 | // and required access levels for a request. Implementations can support both
 | ||||||
| // complete denial and http authorization challenges.
 | // complete denial and http authorization challenges.
 | ||||||
| type AccessController interface { | type AccessController interface { | ||||||
| 	// Authorized returns non-nil if the request is granted access. If one or
 | 	// Authorized returns a non-nil error if the context is granted access and
 | ||||||
| 	// more Access structs are provided, the requested access will be compared
 | 	// returns a new authorized context. If one or more Access structs are
 | ||||||
| 	// with what is available to the request. If the error is non-nil, access
 | 	// provided, the requested access will be compared with what is available
 | ||||||
| 	// should always be denied. The error may be of type Challenge, in which
 | 	// to the context. The given context will contain a "http.request" key with
 | ||||||
| 	// case the caller may have the Challenge handle the request or choose
 | 	// a `*http.Request` value. If the error is non-nil, access should always
 | ||||||
| 	// what action to take based on the Challenge header or response status.
 | 	// be denied. The error may be of type Challenge, in which case the caller
 | ||||||
| 	Authorized(req *http.Request, access ...Access) error | 	// may have the Challenge handle the request or choose what action to take
 | ||||||
|  | 	// based on the Challenge header or response status. The returned context
 | ||||||
|  | 	// object should have a "auth.user" value set to a UserInfo struct.
 | ||||||
|  | 	Authorized(ctx context.Context, access ...Access) (context.Context, error) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // InitFunc is the type of an AccessController factory function and is used
 | // InitFunc is the type of an AccessController factory function and is used
 | ||||||
|  |  | ||||||
|  | @ -13,6 +13,7 @@ import ( | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/docker/distribution/auth" | 	"github.com/docker/distribution/auth" | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // accessController provides a simple implementation of auth.AccessController
 | // accessController provides a simple implementation of auth.AccessController
 | ||||||
|  | @ -41,7 +42,12 @@ func newAccessController(options map[string]interface{}) (auth.AccessController, | ||||||
| 
 | 
 | ||||||
| // Authorized simply checks for the existence of the authorization header,
 | // Authorized simply checks for the existence of the authorization header,
 | ||||||
| // responding with a bearer challenge if it doesn't exist.
 | // responding with a bearer challenge if it doesn't exist.
 | ||||||
| func (ac *accessController) Authorized(req *http.Request, accessRecords ...auth.Access) error { | func (ac *accessController) Authorized(ctx context.Context, accessRecords ...auth.Access) (context.Context, error) { | ||||||
|  | 	req, err := auth.RequestFromContext(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if req.Header.Get("Authorization") == "" { | 	if req.Header.Get("Authorization") == "" { | ||||||
| 		challenge := challenge{ | 		challenge := challenge{ | ||||||
| 			realm:   ac.realm, | 			realm:   ac.realm, | ||||||
|  | @ -56,10 +62,10 @@ func (ac *accessController) Authorized(req *http.Request, accessRecords ...auth. | ||||||
| 			challenge.scope = strings.Join(scopes, " ") | 			challenge.scope = strings.Join(scopes, " ") | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		return &challenge | 		return nil, &challenge | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return nil | 	return context.WithValue(ctx, "auth.user", auth.UserInfo{Name: "silly"}), nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type challenge struct { | type challenge struct { | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/docker/distribution/auth" | 	"github.com/docker/distribution/auth" | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestSillyAccessController(t *testing.T) { | func TestSillyAccessController(t *testing.T) { | ||||||
|  | @ -15,7 +16,9 @@ func TestSillyAccessController(t *testing.T) { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||||
| 		if err := ac.Authorized(r); err != nil { | 		ctx := context.WithValue(nil, "http.request", r) | ||||||
|  | 		authCtx, err := ac.Authorized(ctx) | ||||||
|  | 		if err != nil { | ||||||
| 			switch err := err.(type) { | 			switch err := err.(type) { | ||||||
| 			case auth.Challenge: | 			case auth.Challenge: | ||||||
| 				err.ServeHTTP(w, r) | 				err.ServeHTTP(w, r) | ||||||
|  | @ -25,6 +28,15 @@ func TestSillyAccessController(t *testing.T) { | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		userInfo, ok := authCtx.Value("auth.user").(auth.UserInfo) | ||||||
|  | 		if !ok { | ||||||
|  | 			t.Fatal("silly accessController did not set auth.user context") | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if userInfo.Name != "silly" { | ||||||
|  | 			t.Fatalf("expected user name %q, got %q", "silly", userInfo.Name) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		w.WriteHeader(http.StatusNoContent) | 		w.WriteHeader(http.StatusNoContent) | ||||||
| 	})) | 	})) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -11,9 +11,9 @@ import ( | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/docker/libtrust" |  | ||||||
| 
 |  | ||||||
| 	"github.com/docker/distribution/auth" | 	"github.com/docker/distribution/auth" | ||||||
|  | 	"github.com/docker/libtrust" | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // accessSet maps a typed, named resource to
 | // accessSet maps a typed, named resource to
 | ||||||
|  | @ -217,18 +217,23 @@ func newAccessController(options map[string]interface{}) (auth.AccessController, | ||||||
| 
 | 
 | ||||||
| // Authorized handles checking whether the given request is authorized
 | // Authorized handles checking whether the given request is authorized
 | ||||||
| // for actions on resources described by the given access items.
 | // for actions on resources described by the given access items.
 | ||||||
| func (ac *accessController) Authorized(req *http.Request, accessItems ...auth.Access) error { | func (ac *accessController) Authorized(ctx context.Context, accessItems ...auth.Access) (context.Context, error) { | ||||||
| 	challenge := &authChallenge{ | 	challenge := &authChallenge{ | ||||||
| 		realm:     ac.realm, | 		realm:     ac.realm, | ||||||
| 		service:   ac.service, | 		service:   ac.service, | ||||||
| 		accessSet: newAccessSet(accessItems...), | 		accessSet: newAccessSet(accessItems...), | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	req, err := auth.RequestFromContext(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	parts := strings.Split(req.Header.Get("Authorization"), " ") | 	parts := strings.Split(req.Header.Get("Authorization"), " ") | ||||||
| 
 | 
 | ||||||
| 	if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" { | 	if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" { | ||||||
| 		challenge.err = ErrTokenRequired | 		challenge.err = ErrTokenRequired | ||||||
| 		return challenge | 		return nil, challenge | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	rawToken := parts[1] | 	rawToken := parts[1] | ||||||
|  | @ -236,7 +241,7 @@ func (ac *accessController) Authorized(req *http.Request, accessItems ...auth.Ac | ||||||
| 	token, err := NewToken(rawToken) | 	token, err := NewToken(rawToken) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		challenge.err = err | 		challenge.err = err | ||||||
| 		return challenge | 		return nil, challenge | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	verifyOpts := VerifyOptions{ | 	verifyOpts := VerifyOptions{ | ||||||
|  | @ -248,18 +253,18 @@ func (ac *accessController) Authorized(req *http.Request, accessItems ...auth.Ac | ||||||
| 
 | 
 | ||||||
| 	if err = token.Verify(verifyOpts); err != nil { | 	if err = token.Verify(verifyOpts); err != nil { | ||||||
| 		challenge.err = err | 		challenge.err = err | ||||||
| 		return challenge | 		return nil, challenge | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	accessSet := token.accessSet() | 	accessSet := token.accessSet() | ||||||
| 	for _, access := range accessItems { | 	for _, access := range accessItems { | ||||||
| 		if !accessSet.contains(access) { | 		if !accessSet.contains(access) { | ||||||
| 			challenge.err = ErrInsufficientScope | 			challenge.err = ErrInsufficientScope | ||||||
| 			return challenge | 			return nil, challenge | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return nil | 	return context.WithValue(ctx, "auth.user", auth.UserInfo{Name: token.Claims.Subject}), nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // init handles registering the token auth backend.
 | // init handles registering the token auth backend.
 | ||||||
|  |  | ||||||
|  | @ -47,7 +47,7 @@ type ClaimSet struct { | ||||||
| 	JWTID      string `json:"jti"` | 	JWTID      string `json:"jti"` | ||||||
| 
 | 
 | ||||||
| 	// Private claims
 | 	// Private claims
 | ||||||
| 	Access []*ResourceActions | 	Access []*ResourceActions `json:"access"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Header describes the header section of a JSON Web Token.
 | // Header describes the header section of a JSON Web Token.
 | ||||||
|  |  | ||||||
|  | @ -17,6 +17,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/docker/distribution/auth" | 	"github.com/docker/distribution/auth" | ||||||
| 	"github.com/docker/libtrust" | 	"github.com/docker/libtrust" | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func makeRootKeys(numKeys int) ([]libtrust.PrivateKey, error) { | func makeRootKeys(numKeys int) ([]libtrust.PrivateKey, error) { | ||||||
|  | @ -282,7 +283,8 @@ func TestAccessController(t *testing.T) { | ||||||
| 		Action: "baz", | 		Action: "baz", | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	err = accessController.Authorized(req, testAccess) | 	ctx := context.WithValue(nil, "http.request", req) | ||||||
|  | 	authCtx, err := accessController.Authorized(ctx, testAccess) | ||||||
| 	challenge, ok := err.(auth.Challenge) | 	challenge, ok := err.(auth.Challenge) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		t.Fatal("accessController did not return a challenge") | 		t.Fatal("accessController did not return a challenge") | ||||||
|  | @ -292,6 +294,10 @@ func TestAccessController(t *testing.T) { | ||||||
| 		t.Fatalf("accessControler did not get expected error - got %s - expected %s", challenge, ErrTokenRequired) | 		t.Fatalf("accessControler did not get expected error - got %s - expected %s", challenge, ErrTokenRequired) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if authCtx != nil { | ||||||
|  | 		t.Fatalf("expected nil auth context but got %s", authCtx) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// 2. Supply an invalid token.
 | 	// 2. Supply an invalid token.
 | ||||||
| 	token, err := makeTestToken( | 	token, err := makeTestToken( | ||||||
| 		issuer, service, | 		issuer, service, | ||||||
|  | @ -308,7 +314,7 @@ func TestAccessController(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.compactRaw())) | 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.compactRaw())) | ||||||
| 
 | 
 | ||||||
| 	err = accessController.Authorized(req, testAccess) | 	authCtx, err = accessController.Authorized(ctx, testAccess) | ||||||
| 	challenge, ok = err.(auth.Challenge) | 	challenge, ok = err.(auth.Challenge) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		t.Fatal("accessController did not return a challenge") | 		t.Fatal("accessController did not return a challenge") | ||||||
|  | @ -318,6 +324,10 @@ func TestAccessController(t *testing.T) { | ||||||
| 		t.Fatalf("accessControler did not get expected error - got %s - expected %s", challenge, ErrTokenRequired) | 		t.Fatalf("accessControler did not get expected error - got %s - expected %s", challenge, ErrTokenRequired) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if authCtx != nil { | ||||||
|  | 		t.Fatalf("expected nil auth context but got %s", authCtx) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// 3. Supply a token with insufficient access.
 | 	// 3. Supply a token with insufficient access.
 | ||||||
| 	token, err = makeTestToken( | 	token, err = makeTestToken( | ||||||
| 		issuer, service, | 		issuer, service, | ||||||
|  | @ -330,7 +340,7 @@ func TestAccessController(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.compactRaw())) | 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.compactRaw())) | ||||||
| 
 | 
 | ||||||
| 	err = accessController.Authorized(req, testAccess) | 	authCtx, err = accessController.Authorized(ctx, testAccess) | ||||||
| 	challenge, ok = err.(auth.Challenge) | 	challenge, ok = err.(auth.Challenge) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		t.Fatal("accessController did not return a challenge") | 		t.Fatal("accessController did not return a challenge") | ||||||
|  | @ -340,6 +350,10 @@ func TestAccessController(t *testing.T) { | ||||||
| 		t.Fatalf("accessControler did not get expected error - got %s - expected %s", challenge, ErrInsufficientScope) | 		t.Fatalf("accessControler did not get expected error - got %s - expected %s", challenge, ErrInsufficientScope) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if authCtx != nil { | ||||||
|  | 		t.Fatalf("expected nil auth context but got %s", authCtx) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// 4. Supply the token we need, or deserve, or whatever.
 | 	// 4. Supply the token we need, or deserve, or whatever.
 | ||||||
| 	token, err = makeTestToken( | 	token, err = makeTestToken( | ||||||
| 		issuer, service, | 		issuer, service, | ||||||
|  | @ -348,7 +362,7 @@ func TestAccessController(t *testing.T) { | ||||||
| 			Name:    testAccess.Name, | 			Name:    testAccess.Name, | ||||||
| 			Actions: []string{testAccess.Action}, | 			Actions: []string{testAccess.Action}, | ||||||
| 		}}, | 		}}, | ||||||
| 		rootKeys[0], 1, // Everything is valid except the key which signed it.
 | 		rootKeys[0], 1, | ||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
|  | @ -356,7 +370,17 @@ func TestAccessController(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.compactRaw())) | 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.compactRaw())) | ||||||
| 
 | 
 | ||||||
| 	if err = accessController.Authorized(req, testAccess); err != nil { | 	authCtx, err = accessController.Authorized(ctx, testAccess) | ||||||
|  | 	if err != nil { | ||||||
| 		t.Fatalf("accessController returned unexpected error: %s", err) | 		t.Fatalf("accessController returned unexpected error: %s", err) | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	userInfo, ok := authCtx.Value("auth.user").(auth.UserInfo) | ||||||
|  | 	if !ok { | ||||||
|  | 		t.Fatal("token accessController did not set auth.user context") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if userInfo.Name != "foo" { | ||||||
|  | 		t.Fatalf("expected user name %q, got %q", "foo", userInfo.Name) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ import ( | ||||||
| 	"github.com/docker/distribution/storagedriver" | 	"github.com/docker/distribution/storagedriver" | ||||||
| 	"github.com/docker/distribution/storagedriver/factory" | 	"github.com/docker/distribution/storagedriver/factory" | ||||||
| 	"github.com/gorilla/mux" | 	"github.com/gorilla/mux" | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // App is a global registry application object. Shared resources can be placed
 | // App is a global registry application object. Shared resources can be placed
 | ||||||
|  | @ -189,6 +190,12 @@ func (ssrw *singleStatusResponseWriter) WriteHeader(status int) { | ||||||
| 	ssrw.ResponseWriter.WriteHeader(status) | 	ssrw.ResponseWriter.WriteHeader(status) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // WithRequest adds an http request to the given context and requents
 | ||||||
|  | // a new context with an "http.request" value.
 | ||||||
|  | func WithRequest(ctx context.Context, r *http.Request) context.Context { | ||||||
|  | 	return context.WithValue(ctx, "http.request", r) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // dispatcher returns a handler that constructs a request specific context and
 | // dispatcher returns a handler that constructs a request specific context and
 | ||||||
| // handler, using the dispatch factory function.
 | // handler, using the dispatch factory function.
 | ||||||
| func (app *App) dispatcher(dispatch dispatchFunc) http.Handler { | func (app *App) dispatcher(dispatch dispatchFunc) http.Handler { | ||||||
|  | @ -301,7 +308,8 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if err := app.accessController.Authorized(r, accessRecords...); err != nil { | 	authCtx, err := app.accessController.Authorized(WithRequest(nil, r), accessRecords...) | ||||||
|  | 	if err != nil { | ||||||
| 		switch err := err.(type) { | 		switch err := err.(type) { | ||||||
| 		case auth.Challenge: | 		case auth.Challenge: | ||||||
| 			w.Header().Set("Content-Type", "application/json; charset=utf-8") | 			w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||||||
|  | @ -322,6 +330,10 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// The authorized context should contain an auth.UserInfo
 | ||||||
|  | 	// object. If it doesn't, just use the zero value for now.
 | ||||||
|  | 	context.AuthUserInfo, _ = authCtx.Value("auth.user").(auth.UserInfo) | ||||||
|  | 
 | ||||||
| 	// At this point, the request should have access to the repository under
 | 	// At this point, the request should have access to the repository under
 | ||||||
| 	// the requested operation. Make is available on the context.
 | 	// the requested operation. Make is available on the context.
 | ||||||
| 	context.Repository = app.registry.Repository(repo) | 	context.Repository = app.registry.Repository(repo) | ||||||
|  | @ -332,11 +344,8 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont | ||||||
| // eventBridge returns a bridge for the current request, configured with the
 | // eventBridge returns a bridge for the current request, configured with the
 | ||||||
| // correct actor and source.
 | // correct actor and source.
 | ||||||
| func (app *App) eventBridge(ctx *Context, r *http.Request) notifications.Listener { | func (app *App) eventBridge(ctx *Context, r *http.Request) notifications.Listener { | ||||||
| 	// TODO(stevvooe): Need to extract user data from request context using
 |  | ||||||
| 	// auth system. Would prefer to do this during logging refactor and
 |  | ||||||
| 	// addition of user and google context type.
 |  | ||||||
| 	actor := notifications.ActorRecord{ | 	actor := notifications.ActorRecord{ | ||||||
| 		Name: "--todo--", | 		Name: ctx.AuthUserInfo.Name, | ||||||
| 	} | 	} | ||||||
| 	request := notifications.NewRequestRecord(ctx.RequestID, r) | 	request := notifications.NewRequestRecord(ctx.RequestID, r) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ package registry | ||||||
| import ( | import ( | ||||||
| 	"github.com/Sirupsen/logrus" | 	"github.com/Sirupsen/logrus" | ||||||
| 	"github.com/docker/distribution/api/v2" | 	"github.com/docker/distribution/api/v2" | ||||||
|  | 	"github.com/docker/distribution/auth" | ||||||
| 	"github.com/docker/distribution/storage" | 	"github.com/docker/distribution/storage" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -25,6 +26,9 @@ type Context struct { | ||||||
| 	// handler *must not* start the response via http.ResponseWriter.
 | 	// handler *must not* start the response via http.ResponseWriter.
 | ||||||
| 	Errors v2.Errors | 	Errors v2.Errors | ||||||
| 
 | 
 | ||||||
|  | 	// AuthUserInfo contains information about an authorized client.
 | ||||||
|  | 	AuthUserInfo auth.UserInfo | ||||||
|  | 
 | ||||||
| 	// vars contains the extracted gorilla/mux variables that can be used for
 | 	// vars contains the extracted gorilla/mux variables that can be used for
 | ||||||
| 	// assignment.
 | 	// assignment.
 | ||||||
| 	vars map[string]string | 	vars map[string]string | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue