...

Source file src/go.etcd.io/etcd/client/pkg/v3/testutil/testutil.go

Documentation: go.etcd.io/etcd/client/pkg/v3/testutil

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package testutil provides test utility functions.
    16  package testutil
    17  
    18  import (
    19  	"net/url"
    20  	"os"
    21  	"runtime"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  // WaitSchedule briefly sleeps in order to invoke the go scheduler.
    27  // TODO: improve this when we are able to know the schedule or status of target go-routine.
    28  func WaitSchedule() {
    29  	time.Sleep(10 * time.Millisecond)
    30  }
    31  
    32  func MustNewURLs(t *testing.T, urls []string) []url.URL {
    33  	if urls == nil {
    34  		return nil
    35  	}
    36  	var us []url.URL
    37  	for _, url := range urls {
    38  		u := MustNewURL(t, url)
    39  		us = append(us, *u)
    40  	}
    41  	return us
    42  }
    43  
    44  func MustNewURL(t *testing.T, s string) *url.URL {
    45  	u, err := url.Parse(s)
    46  	if err != nil {
    47  		t.Fatalf("parse %v error: %v", s, err)
    48  	}
    49  	return u
    50  }
    51  
    52  // FatalStack helps to fatal the test and print out the stacks of all running goroutines.
    53  func FatalStack(t *testing.T, s string) {
    54  	stackTrace := make([]byte, 1024*1024)
    55  	n := runtime.Stack(stackTrace, true)
    56  	t.Errorf("---> Test failed: %s", s)
    57  	t.Error(string(stackTrace[:n]))
    58  	t.Fatal(s)
    59  }
    60  
    61  // ConditionFunc returns true when a condition is met.
    62  type ConditionFunc func() (bool, error)
    63  
    64  // Poll calls a condition function repeatedly on a polling interval until it returns true, returns an error
    65  // or the timeout is reached. If the condition function returns true or an error before the timeout, Poll
    66  // immediately returns with the true value or the error. If the timeout is exceeded, Poll returns false.
    67  func Poll(interval time.Duration, timeout time.Duration, condition ConditionFunc) (bool, error) {
    68  	timeoutCh := time.After(timeout)
    69  	ticker := time.NewTicker(interval)
    70  	defer ticker.Stop()
    71  
    72  	for {
    73  		select {
    74  		case <-timeoutCh:
    75  			return false, nil
    76  		case <-ticker.C:
    77  			success, err := condition()
    78  			if err != nil {
    79  				return false, err
    80  			}
    81  			if success {
    82  				return true, nil
    83  			}
    84  		}
    85  	}
    86  }
    87  
    88  func SkipTestIfShortMode(t TB, reason string) {
    89  	if t != nil {
    90  		t.Helper()
    91  		if testing.Short() {
    92  			t.Skip(reason)
    93  		}
    94  	}
    95  }
    96  
    97  // ExitInShortMode closes the current process (with 0) if the short test mode detected.
    98  //
    99  // To be used in Test-main, where test context (testing.TB) is not available.
   100  //
   101  // Requires custom env-variable (GOLANG_TEST_SHORT) apart of `go test --short flag`.
   102  func ExitInShortMode(reason string) {
   103  	if os.Getenv("GOLANG_TEST_SHORT") == "true" {
   104  		os.Exit(0)
   105  	}
   106  }
   107  

View as plain text