go.uber.org/yarpc@v1.72.1/x/yarpctest/api/testing.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 api 22 23 import ( 24 "sync" 25 "testing" 26 ) 27 28 // Run will cast the testing.TB to it's sub and call the appropriate Run func. 29 func Run(name string, t testing.TB, f func(testing.TB)) { 30 if tt, ok := t.(*testing.T); ok { 31 tt.Run(name, func(ttt *testing.T) { f(ttt) }) 32 return 33 } 34 if tb, ok := t.(*testing.B); ok { 35 tb.Run(name, func(ttb *testing.B) { f(ttb) }) 36 return 37 } 38 t.Error("invalid test harness") 39 t.FailNow() 40 } 41 42 // SafeTestingTB is a struct that wraps a testing.TB in a mutex for safe concurrent 43 // usage. 44 type SafeTestingTB struct { 45 sync.Mutex 46 t testing.TB 47 } 48 49 // SetTestingTB safely sets the testing.TB. 50 func (s *SafeTestingTB) SetTestingTB(t testing.TB) { 51 s.Lock() 52 s.t = t 53 s.Unlock() 54 } 55 56 // GetTestingTB safely gets the testing.TB for the testable. 57 func (s *SafeTestingTB) GetTestingTB() testing.TB { 58 s.Lock() 59 t := s.t 60 s.Unlock() 61 return t 62 } 63 64 // SafeTestingTBOnStart is an embeddable struct that automatically grabs testing.TB 65 // objects on "Start" for lifecycles. 66 type SafeTestingTBOnStart struct { 67 SafeTestingTB 68 } 69 70 // Start safely sets the testing.TB for the testable. 71 func (s *SafeTestingTBOnStart) Start(t testing.TB) error { 72 s.SetTestingTB(t) 73 return nil 74 } 75 76 // NoopLifecycle is a convenience struct that can be embedded to make a struct 77 // implement the Start and Stop methods of Lifecycle. 78 type NoopLifecycle struct{} 79 80 // Start is a Noop. 81 func (b *NoopLifecycle) Start(t testing.TB) error { 82 return nil 83 } 84 85 // Stop is a Noop. 86 func (b *NoopLifecycle) Stop(t testing.TB) error { 87 return nil 88 } 89 90 // NoopStop is a convenience struct that can be embedded to make a struct 91 // implement the Stop method of Lifecycle. 92 type NoopStop struct{} 93 94 // Stop is a Noop. 95 func (b *NoopStop) Stop(t testing.TB) error { 96 return nil 97 }