github.com/uber/kraken@v0.1.4/utils/testutil/testutil.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package testutil
    15  
    16  import (
    17  	"fmt"
    18  	"io/ioutil"
    19  	"net"
    20  	"net/http"
    21  	"os"
    22  	"time"
    23  )
    24  
    25  // PollUntilTrue calls f until f returns true. Returns error if true is not received
    26  // within timeout.
    27  func PollUntilTrue(timeout time.Duration, f func() bool) error {
    28  	timer := time.NewTimer(timeout)
    29  	for {
    30  		result := make(chan bool, 1)
    31  		go func() {
    32  			result <- f()
    33  		}()
    34  		select {
    35  		case ok := <-result:
    36  			if ok {
    37  				return nil
    38  			}
    39  			time.Sleep(100 * time.Millisecond)
    40  		case <-timer.C:
    41  			return fmt.Errorf("timed out after %.2f seconds", timeout.Seconds())
    42  		}
    43  	}
    44  }
    45  
    46  // Cleanup contains a list of function that are called to cleanup a fixture
    47  type Cleanup struct {
    48  	funcs []func()
    49  }
    50  
    51  // Add adds function to funcs list
    52  func (c *Cleanup) Add(f ...func()) {
    53  	c.funcs = append(c.funcs, f...)
    54  }
    55  
    56  // AppendFront append funcs from another cleanup in front of the funcs list
    57  func (c *Cleanup) AppendFront(c1 *Cleanup) {
    58  	c.funcs = append(c1.funcs, c.funcs...)
    59  }
    60  
    61  // Recover runs cleanup functions after test exit with exception
    62  func (c *Cleanup) Recover() {
    63  	if err := recover(); err != nil {
    64  		c.run()
    65  		panic(err)
    66  	}
    67  }
    68  
    69  // Run runs cleanup functions when a test finishes running
    70  func (c *Cleanup) Run() {
    71  	c.run()
    72  }
    73  
    74  func (c *Cleanup) run() {
    75  	for _, f := range c.funcs {
    76  		f()
    77  	}
    78  }
    79  
    80  // StartServer starts an HTTP server with h. Returns address the server is
    81  // listening on, and a closure for stopping the server.
    82  func StartServer(h http.Handler) (addr string, stop func()) {
    83  	l, err := net.Listen("tcp", "localhost:0")
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	s := &http.Server{Handler: h}
    88  	go s.Serve(l)
    89  	return l.Addr().String(), func() { s.Close() }
    90  }
    91  
    92  // TempFile creates a temporary file. Returns its name and cleanup function.
    93  func TempFile(data []byte) (string, func()) {
    94  	var cleanup Cleanup
    95  	defer cleanup.Recover()
    96  
    97  	f, err := ioutil.TempFile(".", "")
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	cleanup.Add(func() { os.Remove(f.Name()) })
   102  	defer f.Close()
   103  	if _, err := f.Write(data); err != nil {
   104  		panic(err)
   105  	}
   106  
   107  	return f.Name(), cleanup.Run
   108  }