github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/log/timetracker_test.go (about)

     1  // Copyright 2017 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package log
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/btwiuse/jiri/color"
    15  )
    16  
    17  const commonPrefix = "seconds taken for operation:"
    18  const timeThreshold = 100 * time.Millisecond
    19  
    20  // Creates time trackers for passed operations, sleeps for |sleeptime|
    21  // duration and returns the logger output.
    22  // It creates logger using |loglevel| and |threshold| params.
    23  func runTimeTracker(loglevel LogLevel, threshold, sleeptime time.Duration, operations []string) *bytes.Buffer {
    24  	buf := bytes.NewBufferString("")
    25  	logger := NewLogger(loglevel, color.NewColor(color.ColorNever), false, 0, threshold, buf, nil)
    26  	var tts []*TimeTracker
    27  	for _, op := range operations {
    28  		tts = append(tts, logger.TrackTime(op))
    29  	}
    30  	time.Sleep(sleeptime)
    31  	for _, tt := range tts {
    32  		tt.Done()
    33  	}
    34  	return buf
    35  }
    36  
    37  // Tests time tracker and checks if it is logging the debug message.
    38  func TestTimeTrackerBasic(t *testing.T) {
    39  	t.Parallel()
    40  	buf := runTimeTracker(DebugLevel, timeThreshold, timeThreshold, []string{"new operation"})
    41  	if !strings.Contains(buf.String(), fmt.Sprintf("%s new operation", commonPrefix)) {
    42  		t.Fatalf("logger should have logged timing for this operation")
    43  	}
    44  }
    45  
    46  // Tests that logger does not log time if the loglevel is more than DebugLevel.
    47  func TestTimeTrackerLogLevel(t *testing.T) {
    48  	t.Parallel()
    49  	buf := runTimeTracker(InfoLevel, timeThreshold, timeThreshold, []string{"new operation"})
    50  	if len(buf.String()) != 0 {
    51  		t.Fatalf("Did not expect logging, got: %s", buf.String())
    52  	}
    53  }
    54  
    55  // Tests that code works fine with time trackers on more than one operation.
    56  func TestMultiTimeTracker(t *testing.T) {
    57  	t.Parallel()
    58  	buf := runTimeTracker(DebugLevel, timeThreshold, timeThreshold, []string{"operation 1", "operation 2"})
    59  	if !strings.Contains(buf.String(), fmt.Sprintf("%s operation 1", commonPrefix)) {
    60  		t.Fatalf("logger should have logged timing for operation 1")
    61  	}
    62  	if !strings.Contains(buf.String(), fmt.Sprintf("%s operation 2", commonPrefix)) {
    63  		t.Fatalf("logger should have logged timing for operation 2")
    64  	}
    65  }
    66  
    67  // Tests that logger only logs time when more than threshold.
    68  func TestTimeTrackerThreshold(t *testing.T) {
    69  	t.Parallel()
    70  	buf := runTimeTracker(DebugLevel, timeThreshold, timeThreshold/2, []string{"operation 1"})
    71  	if len(buf.String()) != 0 {
    72  		t.Fatalf("Did not expect logging, got: %s", buf.String())
    73  	}
    74  }