go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/testing/testing_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "sync" 6 "testing" 7 "time" 8 9 "go.undefinedlabs.com/scopeagent/reflection" 10 ) 11 12 func TestLogBufferRegex(t *testing.T) { 13 test := StartTest(t) 14 defer test.End() 15 16 expectedLogLines := []string{ 17 "Hello World", 18 "Hello World With Spaces", 19 "Hello\n World\nMulti\n Line", 20 } 21 22 for _, item := range expectedLogLines { 23 t.Log(item) 24 } 25 26 logBuffer := extractTestOutput(t) 27 logs := string(*logBuffer) 28 for idx, matches := range findMatchesLogRegex(logs) { 29 if expectedLogLines[idx] != matches[3] { 30 t.FailNow() 31 } 32 } 33 } 34 35 func TestExtractSubTestLogBuffer(t *testing.T) { 36 t.Run("SubTest", TestLogBufferRegex) 37 } 38 39 func BenchmarkTestInit(b *testing.B) { 40 for i := 0; i < b.N; i++ { 41 tests := append(make([]testing.InternalTest, 0), 42 testing.InternalTest{Name: "Test01", F: func(t *testing.T) {}}, 43 testing.InternalTest{Name: "Test02", F: func(t *testing.T) {}}, 44 testing.InternalTest{Name: "Test03", F: func(t *testing.T) {}}, 45 testing.InternalTest{Name: "Test04", F: func(t *testing.T) {}}, 46 testing.InternalTest{Name: "Test05", F: func(t *testing.T) {}}, 47 ) 48 benchmarks := append(make([]testing.InternalBenchmark, 0), 49 testing.InternalBenchmark{Name: "Test01", F: func(b *testing.B) {}}, 50 testing.InternalBenchmark{Name: "Test02", F: func(b *testing.B) {}}, 51 testing.InternalBenchmark{Name: "Test03", F: func(b *testing.B) {}}, 52 testing.InternalBenchmark{Name: "Test04", F: func(b *testing.B) {}}, 53 testing.InternalBenchmark{Name: "Test05", F: func(b *testing.B) {}}, 54 ) 55 Init(testing.MainStart(nil, tests, benchmarks, nil)) 56 } 57 } 58 59 func BenchmarkLoggerPatcher(b *testing.B) { 60 for i := 0; i < b.N; i++ { 61 PatchTestingLogger() 62 UnpatchTestingLogger() 63 } 64 } 65 66 func TestLoggerPatcher(t *testing.T) { 67 tm := time.Now() 68 PatchTestingLogger() 69 wg := sync.WaitGroup{} 70 for i := 0; i < 1000; i++ { 71 wg.Add(1) 72 go func(x int) { 73 defer wg.Done() 74 t.Log(fmt.Sprintf("Hello world %d", x)) 75 }(i) 76 } 77 wg.Wait() 78 UnpatchTestingLogger() 79 if time.Since(tm) > 2*time.Second { 80 t.Fatal("Test is too slow") 81 } 82 } 83 84 func TestIsParallelByReflection(t *testing.T) { 85 t.Parallel() 86 tm := time.Now() 87 wg := sync.WaitGroup{} 88 for i := 0; i < 1000; i++ { 89 wg.Add(1) 90 go func() { 91 defer wg.Done() 92 _ = reflection.GetIsParallel(t) 93 }() 94 } 95 wg.Wait() 96 if time.Since(tm) > time.Second { 97 t.Fatal("Test is too slow") 98 } 99 }