go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/fake_testreporter_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"fmt"
    25  	"runtime"
    26  	"runtime/debug"
    27  
    28  	"github.com/golang/mock/gomock"
    29  )
    30  
    31  // FakeTestStatus states how a fake TestingT finished.
    32  type FakeTestStatus int
    33  
    34  const (
    35  	// Finished is the default. This indicates that we ran to completion but
    36  	// may have recorded errors using Errorf.
    37  	Finished FakeTestStatus = iota
    38  
    39  	// Fatal indicates that we ended early with a Fatalf.
    40  	Fatal
    41  
    42  	// Panicked indicates that we aborted with a panic.
    43  	Panicked
    44  )
    45  
    46  func (f FakeTestStatus) String() string {
    47  	switch f {
    48  	case Finished:
    49  		return "Finished"
    50  	case Fatal:
    51  		return "Fatal"
    52  	case Panicked:
    53  		return "Panicked"
    54  	default:
    55  		return fmt.Sprintf("FakeTestStatus(%v)", int(f))
    56  	}
    57  }
    58  
    59  // FakeTestResult contains the result of using a fake TestingT.
    60  type FakeTestResult struct {
    61  	Errors     []string
    62  	Status     FakeTestStatus
    63  	Panic      interface{} // non-nil if we panicked
    64  	PanicTrace string      // non-empty if we panicked
    65  }
    66  
    67  // withFakeTestReporter yields a TestReporter that records its results and
    68  // exposes them in FakeTestResult.
    69  func withFakeTestReporter(f func(gomock.TestReporter)) FakeTestResult {
    70  	var (
    71  		r FakeTestResult
    72  		t = fakeTestReporter{&r}
    73  	)
    74  
    75  	done := make(chan struct{})
    76  	go func() {
    77  		defer func() {
    78  			if v := recover(); v != nil {
    79  				r.Panic = v
    80  				r.PanicTrace = string(debug.Stack())
    81  				r.Status = Panicked
    82  			}
    83  			close(done)
    84  		}()
    85  
    86  		f(&t)
    87  	}()
    88  	<-done
    89  
    90  	return r
    91  }
    92  
    93  type fakeTestReporter struct{ result *FakeTestResult }
    94  
    95  func (t *fakeTestReporter) Errorf(msg string, args ...interface{}) {
    96  	if len(args) > 0 {
    97  		msg = fmt.Sprintf(msg, args)
    98  	}
    99  	t.result.Errors = append(t.result.Errors, msg)
   100  }
   101  
   102  func (t *fakeTestReporter) Fatalf(msg string, args ...interface{}) {
   103  	t.Errorf(msg, args...)
   104  	t.result.Status = Fatal
   105  
   106  	// this kills the current goroutine, unwinding deferred functions.
   107  	runtime.Goexit()
   108  }