Source file src/internal/trace/oldtrace_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package trace_test
     6  
     7  import (
     8  	"internal/trace"
     9  	"internal/trace/testtrace"
    10  	"io"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  )
    15  
    16  func TestOldtrace(t *testing.T) {
    17  	traces, err := filepath.Glob("./internal/oldtrace/testdata/*_good")
    18  	if err != nil {
    19  		t.Fatalf("failed to glob for tests: %s", err)
    20  	}
    21  	var testedUserRegions bool
    22  	for _, p := range traces {
    23  		p := p
    24  		testName, err := filepath.Rel("./internal/oldtrace/testdata", p)
    25  		if err != nil {
    26  			t.Fatalf("failed to relativize testdata path: %s", err)
    27  		}
    28  		t.Run(testName, func(t *testing.T) {
    29  			f, err := os.Open(p)
    30  			if err != nil {
    31  				t.Fatalf("failed to open test %q: %s", p, err)
    32  			}
    33  			defer f.Close()
    34  
    35  			tr, err := trace.NewReader(f)
    36  			if err != nil {
    37  				t.Fatalf("failed to create reader: %s", err)
    38  			}
    39  
    40  			v := testtrace.NewValidator()
    41  			v.Go121 = true
    42  			for {
    43  				ev, err := tr.ReadEvent()
    44  				if err != nil {
    45  					if err == io.EOF {
    46  						break
    47  					}
    48  					t.Fatalf("couldn't read converted event: %s", err)
    49  				}
    50  				if err := v.Event(ev); err != nil {
    51  					t.Fatalf("converted event did not validate; event: \n%s\nerror: %s", ev, err)
    52  				}
    53  
    54  				if testName == "user_task_region_1_21_good" {
    55  					testedUserRegions = true
    56  					validRegions := map[string]struct{}{
    57  						"post-existing region": struct{}{},
    58  						"region0":              struct{}{},
    59  						"region1":              struct{}{},
    60  					}
    61  					// Check that we correctly convert user regions. These
    62  					// strings were generated by
    63  					// runtime/trace.TestUserTaskRegion, which is the basis for
    64  					// the user_task_region_* test cases. We only check for the
    65  					// Go 1.21 traces because earlier traces used different
    66  					// strings.
    67  					switch ev.Kind() {
    68  					case trace.EventRegionBegin, trace.EventRegionEnd:
    69  						if _, ok := validRegions[ev.Region().Type]; !ok {
    70  							t.Fatalf("converted event has unexpected region type:\n%s", ev)
    71  						}
    72  					case trace.EventTaskBegin, trace.EventTaskEnd:
    73  						if ev.Task().Type != "task0" {
    74  							t.Fatalf("converted event has unexpected task type name:\n%s", ev)
    75  						}
    76  					case trace.EventLog:
    77  						l := ev.Log()
    78  						if l.Task != 1 || l.Category != "key0" || l.Message != "0123456789abcdef" {
    79  							t.Fatalf("converted event has unexpected user log:\n%s", ev)
    80  						}
    81  					}
    82  				}
    83  			}
    84  		})
    85  	}
    86  	if !testedUserRegions {
    87  		t.Fatal("didn't see expected test case user_task_region_1_21_good")
    88  	}
    89  }
    90  

View as plain text