Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

bug: context.Background.WithCancel can't assignable to context.Backgroud #320

Closed
islishude opened this issue Aug 8, 2019 · 4 comments
Closed

Comments

@islishude
Copy link
Contributor

package main

import (
	"context"
	"fmt"

	"github.com/golang/mock/gomock"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	res := gomock.AssignableToTypeOf(context.Background()).Matches(ctx)
	fmt.Println(res) // fasle
}
@poy
Copy link
Collaborator

poy commented Aug 8, 2019

Yup, this looks like a bug. Thanks for filing it!

@poy poy added the type: bug label Aug 8, 2019
@poy
Copy link
Collaborator

poy commented Aug 8, 2019

After further review, it looks like what's going on is, Context.Background() is a *context.emptyCtx where as ctx is a *context.cancelCtx. So in fact, it should print false.

@poy poy removed the type: bug label Aug 8, 2019
@islishude
Copy link
Contributor Author

According to the documentation,

AssignableToTypeOf is a Matcher that matches if the parameter to the mock function is assignable to the type of the parameter to this function.

Context.Background is context.Context and context.cancelCtx is also context.Context.so AssignableToTypeOf(context.Background()) should match context.cancelCtx.

package main

import (
	"context"
	"fmt"
)

func main() {
	ctx := context.Background()
	ctxWithCancel, cancel := context.WithCancel(context.Background())
	defer cancel()
	ctx = ctxWithCancel // no error here
	fmt.Println(ctx.Err())
}

@islishude
Copy link
Contributor Author

relates to #365

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants