github.com/Azure/aad-pod-identity@v1.8.17/pkg/stats/stats_test.go (about)

     1  package stats
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestBasics(t *testing.T) {
    10  	Init()
    11  	Put(Total, time.Second*20)
    12  	Aggregate(DeleteAzureAssignedIdentity, time.Second*40)
    13  	Aggregate(DeleteAzureAssignedIdentity, time.Second*40)
    14  	Increment(TotalPatchCalls, 100)
    15  	Increment(TotalPatchCalls, 200)
    16  	PrintSync()
    17  
    18  	expectedDuration := time.Second * 20
    19  	if globalStats[Total] != expectedDuration {
    20  		t.Fatalf("Expected '%s' statistic to have a value of %s, but got %s", Total, expectedDuration, globalStats[Total])
    21  	}
    22  
    23  	expectedDuration = time.Second * 80
    24  	if globalStats[DeleteAzureAssignedIdentity] != expectedDuration {
    25  		t.Fatalf("Expected '%s' statistic to have a value of %s, but got %s", DeleteAzureAssignedIdentity, expectedDuration, globalStats[DeleteAzureAssignedIdentity])
    26  	}
    27  
    28  	expectedCount := 300
    29  	if countStats[TotalPatchCalls] != expectedCount {
    30  		t.Fatalf("Expected '%s' statistic to have a value of %d, but got %d", TotalPatchCalls, expectedCount, globalStats[TotalPatchCalls])
    31  	}
    32  }
    33  
    34  func TestAggregateConcurrent(t *testing.T) {
    35  	Init()
    36  
    37  	begin := time.Now()
    38  	count := 100
    39  
    40  	var wg sync.WaitGroup
    41  	wg.Add(count)
    42  
    43  	for i := 0; i < count; i++ {
    44  		go func() {
    45  			defer wg.Done()
    46  			begin := time.Now()
    47  			time.Sleep(time.Millisecond * 10)
    48  			AggregateConcurrent(CreateAzureAssignedIdentity, begin, time.Now())
    49  		}()
    50  	}
    51  	wg.Wait()
    52  	PrintSync()
    53  
    54  	totalDuration := time.Since(begin)
    55  	if totalDuration < globalStats[CreateAzureAssignedIdentity] {
    56  		t.Fatalf("Expected the total duration to be shorter than %s, but got %s", totalDuration, globalStats[CreateAzureAssignedIdentity])
    57  	}
    58  }