github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/grpclogging/context_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package grpclogging_test
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  
    13  	"github.com/hyperledger/fabric/common/grpclogging"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	"go.uber.org/zap"
    17  	"go.uber.org/zap/zapcore"
    18  )
    19  
    20  var _ = Describe("Context", func() {
    21  	var inputFields []zapcore.Field
    22  
    23  	BeforeEach(func() {
    24  		inputFields = []zapcore.Field{
    25  			zap.String("string-key", "string-value"),
    26  			zap.Duration("duration-key", time.Second),
    27  			zap.Int("int-key", 42),
    28  		}
    29  	})
    30  
    31  	It("decorates a context with fields", func() {
    32  		ctx := grpclogging.WithFields(context.Background(), inputFields)
    33  		Expect(ctx).NotTo(Equal(context.Background()))
    34  
    35  		fields := grpclogging.Fields(ctx)
    36  		Expect(fields).NotTo(BeEmpty())
    37  	})
    38  
    39  	It("extracts fields from a decorated context as a slice of zapcore.Field", func() {
    40  		ctx := grpclogging.WithFields(context.Background(), inputFields)
    41  
    42  		fields := grpclogging.Fields(ctx)
    43  		Expect(fields).To(ConsistOf(inputFields))
    44  	})
    45  
    46  	It("extracts fields from a decorated context as a slice of interface{}", func() {
    47  		ctx := grpclogging.WithFields(context.Background(), inputFields)
    48  
    49  		zapFields := grpclogging.ZapFields(ctx)
    50  		Expect(zapFields).To(Equal(inputFields))
    51  	})
    52  
    53  	It("returns the same fields regardless of type", func() {
    54  		ctx := grpclogging.WithFields(context.Background(), inputFields)
    55  
    56  		fields := grpclogging.Fields(ctx)
    57  		zapFields := grpclogging.ZapFields(ctx)
    58  		Expect(zapFields).To(ConsistOf(fields))
    59  	})
    60  
    61  	Context("when the context isn't decorated", func() {
    62  		It("returns no fields", func() {
    63  			fields := grpclogging.Fields(context.Background())
    64  			Expect(fields).To(BeNil())
    65  
    66  			zapFields := grpclogging.ZapFields(context.Background())
    67  			Expect(zapFields).To(BeNil())
    68  		})
    69  	})
    70  })