go.temporal.io/server@v1.23.0/common/log/sdk_logger_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package log
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/require"
    32  	"github.com/stretchr/testify/suite"
    33  	"go.temporal.io/sdk/log"
    34  
    35  	"go.temporal.io/server/common/log/tag"
    36  )
    37  
    38  type SdkLoggerSuite struct {
    39  	*require.Assertions
    40  	suite.Suite
    41  
    42  	controller       *gomock.Controller
    43  	sdkLogger        log.Logger
    44  	underlyingLogger *MockLogger
    45  }
    46  
    47  func TestSdkLoggerSuite(t *testing.T) {
    48  	suite.Run(t, &SdkLoggerSuite{})
    49  }
    50  
    51  func (s *SdkLoggerSuite) SetupTest() {
    52  	s.Assertions = require.New(s.T())
    53  	s.controller = gomock.NewController(s.T())
    54  
    55  	s.underlyingLogger = NewMockLogger(s.controller)
    56  	s.sdkLogger = NewSdkLogger(s.underlyingLogger)
    57  }
    58  
    59  func (s *SdkLoggerSuite) TearDownTest() {
    60  	s.controller.Finish()
    61  }
    62  
    63  func (s *SdkLoggerSuite) TestEvenKeyValPairs() {
    64  	s.underlyingLogger.EXPECT().Info("msg", tag.NewAnyTag("key1", "val1"), tag.NewAnyTag("key2", "val2"))
    65  	s.sdkLogger.Info("msg", "key1", "val1", "key2", "val2")
    66  }
    67  
    68  func (s *SdkLoggerSuite) TestOddKeyValPairs() {
    69  	s.underlyingLogger.EXPECT().Info("msg", tag.NewAnyTag("key1", "val1"), tag.NewAnyTag("key2", "no value"))
    70  	s.sdkLogger.Info("msg", "key1", "val1", "key2")
    71  }
    72  
    73  func (s *SdkLoggerSuite) TestKeyValPairsWithTag() {
    74  	s.underlyingLogger.EXPECT().Info("msg", tag.NewAnyTag("key1", "val1"), tag.NewStringTag("key3", "val3"), tag.NewAnyTag("key2", "val2"))
    75  	s.sdkLogger.Info("msg", "key1", "val1", tag.NewStringTag("key3", "val3"), "key2", "val2")
    76  
    77  	s.underlyingLogger.EXPECT().Info("msg", tag.NewAnyTag("key1", "val1"), tag.NewInt("key3", 3), tag.NewAnyTag("key2", "val2"))
    78  	s.sdkLogger.Info("msg", "key1", "val1", tag.NewInt("key3", 3), "key2", "val2")
    79  
    80  }
    81  
    82  func (s *SdkLoggerSuite) TestEmptyKeyValPairs() {
    83  	s.underlyingLogger.EXPECT().Info("msg")
    84  	s.sdkLogger.Info("msg")
    85  }
    86  
    87  func (s *SdkLoggerSuite) TestSingleKeyValPairs() {
    88  	s.underlyingLogger.EXPECT().Info("msg", tag.NewAnyTag("key1", "no value"))
    89  	s.sdkLogger.Info("msg", "key1")
    90  }