github.com/Jeffail/benthos/v3@v3.65.0/lib/test/integration/integration_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"net"
     7  	"regexp"
     8  	"strings"
     9  	"testing"
    10  
    11  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    12  )
    13  
    14  func getFreePort() (int, error) {
    15  	addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
    16  	if err != nil {
    17  		return 0, err
    18  	}
    19  
    20  	listener, err := net.ListenTCP("tcp", addr)
    21  	if err != nil {
    22  		return 0, err
    23  	}
    24  	defer listener.Close()
    25  	return listener.Addr().(*net.TCPAddr).Port, nil
    26  }
    27  
    28  var registeredIntegrationTests = map[string]func(*testing.T){}
    29  
    30  // register an integration test that should only execute under the `integration`
    31  // build tag. Returns an empty struct so that it can be called at a file root.
    32  func registerIntegrationTest(name string, fn func(*testing.T)) struct{} {
    33  	if _, exists := registeredIntegrationTests[name]; exists {
    34  		panic(fmt.Sprintf("integration test double registered: %v", name))
    35  	}
    36  	registeredIntegrationTests[name] = fn
    37  	return struct{}{}
    38  }
    39  
    40  // Placing this in its own function allows us to only execute under the
    41  // integration build tag, but the tests themselves are always built.
    42  func TestIntegration(t *testing.T) {
    43  	if m := flag.Lookup("test.run").Value.String(); m == "" || regexp.MustCompile(strings.Split(m, "/")[0]).FindString(t.Name()) == "" {
    44  		t.Skip("Skipping as execution was not requested explicitly using go test -run ^TestIntegration$")
    45  	}
    46  
    47  	for k, test := range registeredIntegrationTests {
    48  		test := test
    49  		t.Run(k, test)
    50  	}
    51  }
    52  
    53  var registeredIntegrationBenchmarks = map[string]func(*testing.B){}
    54  
    55  // register an integration test that should only execute under the `integration`
    56  // build tag. Returns an empty struct so that it can be called at a file root.
    57  func registerIntegrationBench(name string, fn func(*testing.B)) struct{} {
    58  	if _, exists := registeredIntegrationBenchmarks[name]; exists {
    59  		panic(fmt.Sprintf("integration benchmark double registered: %v", name))
    60  	}
    61  	registeredIntegrationBenchmarks[name] = fn
    62  	return struct{}{}
    63  }
    64  
    65  func BenchmarkIntegration(b *testing.B) {
    66  	for k, test := range registeredIntegrationBenchmarks {
    67  		test := test
    68  		b.Run(k, test)
    69  	}
    70  }