1// Copyright 2019 The gRPC 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 15package http2interop 16 17import ( 18 "path" 19 "runtime" 20 "strings" 21 "sync" 22 "testing" 23) 24 25// When a test is skipped or fails, runtime.Goexit() is called which destroys the callstack. 26// This means the name of the test case is lost, so we need to grab a copy of pc before. 27func Report(t testing.TB) { 28 // If the goroutine panics, Fatal()s, or Skip()s, the function name is at the 3rd callstack 29 // layer. On success, its at 1st. Since it's hard to check which happened, just try both. 30 pcs := make([]uintptr, 10) 31 total := runtime.Callers(1, pcs) 32 var name string 33 for _, pc := range pcs[:total] { 34 fn := runtime.FuncForPC(pc) 35 fullName := fn.Name() 36 if strings.HasPrefix(path.Ext(fullName), ".Test") { 37 // Skip the leading . 38 name = string([]byte(path.Ext(fullName))[1:]) 39 break 40 } 41 } 42 if name == "" { 43 return 44 } 45 46 allCaseInfos.lock.Lock() 47 defer allCaseInfos.lock.Unlock() 48 allCaseInfos.Cases = append(allCaseInfos.Cases, &caseInfo{ 49 Name: name, 50 Passed: !t.Failed() && !t.Skipped(), 51 Skipped: t.Skipped(), 52 Fatal: t.Failed() && !strings.HasPrefix(name, "TestSoon"), 53 }) 54} 55 56type caseInfo struct { 57 Name string `json:"name"` 58 Passed bool `json:"passed"` 59 Skipped bool `json:"skipped,omitempty"` 60 Fatal bool `json:"fatal,omitempty"` 61} 62 63type caseInfos struct { 64 lock sync.Mutex 65 Cases []*caseInfo `json:"cases"` 66} 67 68var ( 69 allCaseInfos = caseInfos{} 70) 71