github.com/grafana/pyroscope@v1.18.0/pkg/test/idempotence.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // AssertIdempotent asserts that the test is valid when run multiple times.
     8  func AssertIdempotent(t *testing.T, fn func(*testing.T)) {
     9  	t.Helper()
    10  	for i := 0; i < 2; i++ {
    11  		fn(t)
    12  		if t.Failed() {
    13  			if i > 0 {
    14  				t.Fatal("the function is not idempotent")
    15  			}
    16  			return
    17  		}
    18  	}
    19  }
    20  
    21  func AssertIdempotentSubtest(t *testing.T, fn func(*testing.T)) func(*testing.T) {
    22  	t.Helper()
    23  	return func(t *testing.T) {
    24  		for i := 0; i < 2; i++ {
    25  			fn(t)
    26  			if t.Failed() {
    27  				if i > 0 {
    28  					t.Fatal("the function is not idempotent")
    29  				}
    30  				return
    31  			}
    32  		}
    33  	}
    34  }