github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/flogging/grpclogger_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flogging
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/op/go-logging"
    23  	"github.com/stretchr/testify/assert"
    24  	"google.golang.org/grpc/grpclog"
    25  )
    26  
    27  // from go-logging memory_test.go
    28  func MemoryRecordN(b *logging.MemoryBackend, n int) *logging.Record {
    29  	node := b.Head()
    30  	for i := 0; i < n; i++ {
    31  		if node == nil {
    32  			break
    33  		}
    34  		node = node.Next()
    35  	}
    36  	if node == nil {
    37  		return nil
    38  	}
    39  	return node.Record
    40  }
    41  
    42  func TestGRPCLogger(t *testing.T) {
    43  	initgrpclogger()
    44  	backend := logging.NewMemoryBackend(3)
    45  	logging.SetBackend(backend)
    46  	logging.SetLevel(defaultLevel, "")
    47  	SetModuleLevel(GRPCModuleID, "DEBUG")
    48  	messages := []string{"print test", "printf test", "println test"}
    49  	grpclog.Print(messages[0])
    50  	grpclog.Printf(messages[1])
    51  	grpclog.Println(messages[2])
    52  
    53  	for i, message := range messages {
    54  		assert.Equal(t, message, MemoryRecordN(backend, i).Message())
    55  		t.Log(MemoryRecordN(backend, i).Message())
    56  	}
    57  
    58  	// now make sure there's no logging at a level other than DEBUG
    59  	SetModuleLevel(GRPCModuleID, "INFO")
    60  	messages2 := []string{"print test2", "printf test2", "println test2"}
    61  	grpclog.Print(messages2[0])
    62  	grpclog.Printf(messages2[1])
    63  	grpclog.Println(messages2[2])
    64  
    65  	// should still be messages not messages2
    66  	for i, message := range messages {
    67  		assert.Equal(t, message, MemoryRecordN(backend, i).Message())
    68  		t.Log(MemoryRecordN(backend, i).Message())
    69  	}
    70  	// reset flogging
    71  	Reset()
    72  }