github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/util/log_test.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/rs/zerolog"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestLogProgress40(t *testing.T) {
    16  	buf := bytes.NewBufferString("")
    17  	lg := zerolog.New(buf)
    18  	total := 40
    19  	logger := LogProgress(
    20  		lg,
    21  		DefaultLogProgressConfig(
    22  			"test",
    23  			total,
    24  		),
    25  	)
    26  	for i := 0; i < total; i++ {
    27  		logger(1)
    28  	}
    29  
    30  	expectedLogs := []string{
    31  		`test progress 1/40 (2.5%)`,
    32  		`test progress 5/40 (12.5%)`,
    33  		`test progress 9/40 (22.5%)`,
    34  		`test progress 13/40 (32.5%)`,
    35  		`test progress 17/40 (42.5%)`,
    36  		`test progress 21/40 (52.5%)`,
    37  		`test progress 25/40 (62.5%)`,
    38  		`test progress 29/40 (72.5%)`,
    39  		`test progress 33/40 (82.5%)`,
    40  		`test progress 37/40 (92.5%)`,
    41  		`test progress 40/40 (100.0%)`,
    42  	}
    43  
    44  	for _, log := range expectedLogs {
    45  		require.Contains(t, buf.String(), log, total)
    46  	}
    47  }
    48  
    49  func TestLogProgress1000(t *testing.T) {
    50  	for total := 11; total < 1000; total++ {
    51  		buf := bytes.NewBufferString("")
    52  		lg := zerolog.New(buf)
    53  		logger := LogProgress(
    54  			lg,
    55  			DefaultLogProgressConfig(
    56  				"test",
    57  				total,
    58  			),
    59  		)
    60  
    61  		for i := 0; i < total; i++ {
    62  			logger(1)
    63  		}
    64  
    65  		expectedLogs := []string{
    66  			fmt.Sprintf(`test progress 1/%d`, total),
    67  			fmt.Sprintf(`test progress %d/%d (100.0%%)`, total, total),
    68  		}
    69  
    70  		for _, log := range expectedLogs {
    71  			require.Contains(t, buf.String(), log, total)
    72  		}
    73  	}
    74  }
    75  
    76  func TestLogProgressMultipleGoroutines(t *testing.T) {
    77  	total := 1000
    78  
    79  	buf := bytes.NewBufferString("")
    80  	lg := zerolog.New(buf)
    81  	logger := LogProgress(
    82  		lg,
    83  		DefaultLogProgressConfig(
    84  			"test",
    85  			total,
    86  		),
    87  	)
    88  
    89  	wg := sync.WaitGroup{}
    90  	for i := 0; i < 10; i++ {
    91  		wg.Add(1)
    92  		go func() {
    93  			defer wg.Done()
    94  			for j := 0; j < 100; j++ {
    95  				logger(1)
    96  			}
    97  		}()
    98  	}
    99  
   100  	wg.Wait()
   101  
   102  	expectedLogs := []string{
   103  		fmt.Sprintf(`test progress 1/%d`, total),
   104  		fmt.Sprintf(`test progress %d/%d (100.0%%)`, total, total),
   105  	}
   106  
   107  	lines := strings.Count(buf.String(), "\n")
   108  	// every 10% + 1 for the final log
   109  	require.Equal(t, 11, lines)
   110  
   111  	for _, log := range expectedLogs {
   112  		require.Contains(t, buf.String(), log, total)
   113  	}
   114  }
   115  
   116  func TestLogProgressCustomSampler(t *testing.T) {
   117  	total := 1000
   118  
   119  	nth := uint32(total / 10) // sample every 10% by default
   120  	if nth == 0 {
   121  		nth = 1
   122  	}
   123  	sampler := newProgressLogsSampler(nth, 10*time.Millisecond)
   124  
   125  	buf := bytes.NewBufferString("")
   126  	lg := zerolog.New(buf)
   127  	logger := LogProgress(
   128  		lg,
   129  		NewLogProgressConfig(
   130  			"test",
   131  			total,
   132  			sampler,
   133  		),
   134  	)
   135  
   136  	for i := 0; i < total; i++ {
   137  		if i == 7 || i == 77 || i == 777 {
   138  			// s
   139  			time.Sleep(20 * time.Millisecond)
   140  		}
   141  		logger(1)
   142  	}
   143  
   144  	expectedLogs := []string{
   145  		fmt.Sprintf(`test progress 1/%d`, total),
   146  		fmt.Sprintf(`test progress %d/%d (100.0%%)`, total, total),
   147  	}
   148  
   149  	lines := strings.Count(buf.String(), "\n")
   150  	// every 10% + 1 for the final log + 3 for the custom sampler
   151  	require.Equal(t, 14, lines)
   152  
   153  	for _, log := range expectedLogs {
   154  		require.Contains(t, buf.String(), log, total)
   155  	}
   156  }