diff --git a/.travis.yml b/.travis.yml index ec747cdc..22cd317b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,8 +15,8 @@ install: - go mod vendor script: - - go test -v -mod=vendor ./... - - $GOPATH/bin/goveralls -service=travis-ci || true + - go test -race -mod=vendor -covermode=atomic -coverpkg=github.com/s7techlab/cckit/... -coverprofile=profile.cov ./... + - $GOPATH/bin/goveralls -coverprofile=profile.cov -service=travis-ci || true go: - 1.13.x diff --git a/gateway/service/context.go b/gateway/service/context.go index 7339e04f..fba4aac8 100644 --- a/gateway/service/context.go +++ b/gateway/service/context.go @@ -45,8 +45,8 @@ func ContextWithDoOption(ctx context.Context, doOpts ...api.DoOption) context.Co } func DoOptionFromContext(ctx context.Context) []api.DoOption { - doOpts := ctx.Value(CtxDoOptionKey).([]api.DoOption) - if doOpts == nil { + doOpts, ok := ctx.Value(CtxDoOptionKey).([]api.DoOption) + if !ok { doOpts = []api.DoOption{} } return doOpts diff --git a/gateway/service/context_test.go b/gateway/service/context_test.go new file mode 100644 index 00000000..5dfca160 --- /dev/null +++ b/gateway/service/context_test.go @@ -0,0 +1,73 @@ +package service_test + +import ( + "context" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/s7techlab/hlf-sdk-go/api" + "github.com/s7techlab/hlf-sdk-go/client/chaincode" + "github.com/s7techlab/hlf-sdk-go/client/chaincode/txwaiter" + + "github.com/s7techlab/cckit/gateway/service" +) + +func TestContext(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Context Suite") +} + +var ( + doOptsEmpty = []api.DoOption{} + doOptsAll = []api.DoOption{chaincode.WithTxWaiter(txwaiter.All)} + doOptsSelf = []api.DoOption{chaincode.WithTxWaiter(txwaiter.Self)} +) + +var _ = Describe(`DoOption`, func() { + + It("Set Default DoOption", func() { + ctx := service.ContextWithDefaultDoOption(context.Background(), doOptsSelf...) + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsSelf)) + }) + + It("Default dont allow update on ctx DoOption", func() { + ctx := service.ContextWithDefaultDoOption(context.Background(), doOptsAll...) + ctx = service.ContextWithDefaultDoOption(ctx, doOptsSelf...) + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsAll)) + }) + + It("Set DoOption to Context", func() { + ctx := service.ContextWithDoOption(context.Background(), doOptsSelf...) + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsSelf)) + }) + + It("Update DoOption to Context", func() { + ctx := service.ContextWithDoOption(context.Background(), doOptsAll...) + ctx = service.ContextWithDoOption(ctx, doOptsSelf...) + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsSelf)) + }) + + It("Update DoOption to Context", func() { + ctx := service.ContextWithDoOption(context.Background(), doOptsAll...) + ctx = service.ContextWithDoOption(ctx, doOptsSelf...) + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsSelf)) + }) + + It("Allow get DoOptions from empty Context", func() { + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsEmpty)) + }) + + It("Allow get DoOptions from filled Context", func() { + ctx := service.ContextWithDoOption(ctx, doOptsSelf...) + result := service.DoOptionFromContext(ctx) + Expect(result).To(Equal(doOptsSelf)) + }) +})