github.com/haraldrudell/parl@v0.4.176/g0/thread-data_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package g0
     7  
     8  import (
     9  	"strings"
    10  	"sync"
    11  	"testing"
    12  
    13  	"github.com/haraldrudell/parl"
    14  	"github.com/haraldrudell/parl/pdebug"
    15  )
    16  
    17  const (
    18  	threadDataLabel = "myThreadName"
    19  )
    20  
    21  func TestThreadDataZeroValue(t *testing.T) {
    22  
    23  	// check zero-value results
    24  	var threadData ThreadData
    25  	// a zero-value threadData should not be valid
    26  	if threadData.ThreadID().IsValid() {
    27  		t.Error("threadData.ThreadID().IsValid() true")
    28  	}
    29  	// a zero-value threadData should not have Create
    30  	if threadData.Create().IsSet() {
    31  		t.Error("threadData.Create().IsSet() true")
    32  	}
    33  	if threadData.Func().IsSet() {
    34  		t.Error("threadData.Func().IsSet() true")
    35  	}
    36  	if name := threadData.Name(); name != "" {
    37  		t.Errorf("threadData.Name() not empty: %q", name)
    38  	}
    39  	if short := threadData.Short(); short != ThreadDataEmpty {
    40  		t.Errorf("threadData.Short() zero-value: %q exp %q", short, ThreadDataEmpty)
    41  	}
    42  	if short := threadData.String(); short != ThreadDataEmpty {
    43  		t.Errorf("threadData.String() zero-value: %q exp %q", short, ThreadDataEmpty)
    44  	}
    45  
    46  	// check nil returns
    47  	var threadDatap *ThreadData
    48  	if short := threadDatap.Short(); short != ThreadDataNil {
    49  		t.Errorf("nil.Short(): %q exp %q", short, ThreadDataNil)
    50  	}
    51  }
    52  
    53  func TestThreadData(t *testing.T) {
    54  	var expCreateShort = ".(*SomeType).SomeCode"
    55  	var expFuncShort = ".(*SomeType).SomeFunction"
    56  
    57  	var exp string
    58  	var threadData ThreadData
    59  
    60  	// check populated returns
    61  	var someType SomeType
    62  	someType.SomeCode(&threadData)
    63  
    64  	// ID: 36 IsMain: false status: running
    65  	// github.com/haraldrudell/parl/g0.(*SomeType).SomeMethod(0x140001482c0, 0x0?)
    66  	// 	thread-data_test.go:135
    67  	// github.com/haraldrudell/parl/g0.(*SomeType).SomeFunction(0x14000106b60?, 0x102ef8280?)
    68  	// 	thread-data_test.go:132
    69  	// cre: github.com/haraldrudell/parl/g0.(*SomeType).SomeCode in goroutine 35-thread-data_test.go:126
    70  	//t.Logf("stack: \n%s", someType.stack)
    71  
    72  	// ThreadID: 20
    73  	// Create: File: "/opt/sw/parl/g0/thread-data_test.go" Line: 147 FuncName: "github.com/haraldrudell/parl/g0.(*SomeType).SomeCode in goroutine 19"
    74  	// Func: File: "/opt/sw/parl/g0/thread-data_test.go" Line: 153 FuncName: "github.com/haraldrudell/parl/g0.(*SomeType).SomeFunction"
    75  	// Name: myThreadName
    76  	// t.Logf("\n"+
    77  	// 	"ThreadID: %s\n"+
    78  	// 	"Create: %s\n"+
    79  	// 	"Func: %s\n"+
    80  	// 	"Name: %s",
    81  	// 	threadData.ThreadID().String(),
    82  	// 	threadData.Create().Dump(),
    83  	// 	threadData.Func().Dump(),
    84  	// 	threadData.Name(),
    85  	// )
    86  
    87  	threadID, createLocation, funcLocation, label := threadData.Get()
    88  	if threadData.ThreadID() != threadID {
    89  		t.Error("bad threadData.threadID()")
    90  	}
    91  	if *threadData.Create() != createLocation {
    92  		t.Error("bad threadData.Create()")
    93  	}
    94  	if *threadData.Func() != funcLocation {
    95  		t.Error("bad threadData.Func()")
    96  	}
    97  	if threadData.Name() != label {
    98  		t.Error("bad threadData.Name()")
    99  	}
   100  	if threadID != someType.stack.ID() {
   101  		t.Errorf("bad ID %q exp %q", threadID, someType.stack.ID())
   102  	}
   103  	if !strings.Contains(createLocation.Short(), expCreateShort) {
   104  		t.Errorf("createLocation.Short(): %q exp %q", createLocation.Short(), expCreateShort)
   105  	}
   106  	if !strings.Contains(funcLocation.Short(), expFuncShort) {
   107  		t.Errorf("funcLocation.Short(): %q exp %q", funcLocation.Short(), expFuncShort)
   108  	}
   109  	if label != threadDataLabel {
   110  		t.Errorf("label: %q exp %q", label, threadDataLabel)
   111  	}
   112  	exp = label + ":" + threadID.String()
   113  	if short := threadData.Short(); short != exp {
   114  		t.Errorf("threadData.Short(): %q exp %q", short, exp)
   115  	}
   116  	if short := threadData.String(); !strings.Contains(short, exp) {
   117  		t.Errorf("threadData.String(): %q exp %q", short, exp)
   118  	}
   119  	var creator, _, _ = someType.stack.Creator()
   120  	threadData.SetCreator(creator)
   121  }
   122  
   123  // ITEST= go test -v -run '^TestThreadDataValues$' ./g0
   124  func TestThreadDataValues(t *testing.T) {
   125  
   126  	var threadData ThreadData
   127  	t.Logf("zero-value ThreadID: %q", threadData.ThreadID())
   128  	t.Logf("zero-value Create().Short(): %q", threadData.Create().Short())
   129  	t.Logf("zero-value Name(): %q", threadData.Name())
   130  	t.Logf("zero-value Short(): %q", threadData.Name())
   131  	t.Logf("zero-value String(): %q", &threadData)
   132  
   133  	var someType SomeType
   134  	someType.SomeCode(&threadData)
   135  	t.Logf("ThreadID: %q", threadData.ThreadID())
   136  	t.Logf("Create().Short(): %q", threadData.Create().Short())
   137  	t.Logf("Func().Short(): %q", threadData.Func().Short())
   138  	t.Logf("Name(): %q", threadData.Name())
   139  	t.Logf("Short(): %q", threadData.Short())
   140  	t.Logf("String(): %q", &threadData)
   141  
   142  }
   143  
   144  type SomeType struct {
   145  	wg    sync.WaitGroup
   146  	stack parl.Stack
   147  }
   148  
   149  func (s *SomeType) SomeCode(threadData *ThreadData) {
   150  	s.wg.Add(1)
   151  	go s.SomeFunction(threadData)
   152  	s.wg.Wait()
   153  }
   154  func (s *SomeType) SomeFunction(threadData *ThreadData) {
   155  	defer s.wg.Done()
   156  
   157  	s.SomeMethod(threadData)
   158  }
   159  func (s *SomeType) SomeMethod(threadData *ThreadData) {
   160  	s.stack = pdebug.NewStack(0)
   161  	var creator, _, _ = s.stack.Creator()
   162  	threadData.Update(
   163  		s.stack.ID(),
   164  		creator,
   165  		s.stack.GoFunction(),
   166  		threadDataLabel)
   167  }