github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/shared/mlog/log_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package mlog_test
     5  
     6  import (
     7  	"context"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/masterhung0112/hk_server/v5/shared/mlog"
    14  )
    15  
    16  // Test race condition when shutting down advanced logging. This test must run with the -race flag in order to verify
    17  // that there is no race.
    18  func TestLogger_ShutdownAdvancedLoggingRace(t *testing.T) {
    19  	logger := mlog.NewLogger(&mlog.LoggerConfiguration{
    20  		EnableConsole: true,
    21  		ConsoleJson:   true,
    22  		EnableFile:    false,
    23  		FileLevel:     mlog.LevelInfo,
    24  	})
    25  	started := make(chan bool)
    26  	ctx, cancel := context.WithCancel(context.Background())
    27  	var wg sync.WaitGroup
    28  
    29  	wg.Add(1)
    30  	go func() {
    31  		defer wg.Done()
    32  		started <- true
    33  
    34  		for {
    35  			select {
    36  			case <-ctx.Done():
    37  				return
    38  			default:
    39  				logger.Debug("testing...")
    40  			}
    41  		}
    42  	}()
    43  
    44  	<-started
    45  
    46  	err := logger.ShutdownAdvancedLogging(ctx)
    47  	require.NoError(t, err)
    48  
    49  	cancel()
    50  	wg.Wait()
    51  }