github.com/haraldrudell/parl@v0.4.176/perrors/parlerror_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 perrors
     7  
     8  import (
     9  	"errors"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestParlErrorErrCh(t *testing.T) {
    15  	chErr := make(chan error)
    16  	closePanics := func() (didPanic bool) {
    17  		defer func() {
    18  			if v := recover(); v != nil {
    19  				didPanic = true
    20  			}
    21  		}()
    22  
    23  		close(chErr)
    24  		return false
    25  	}
    26  
    27  	// does Shutdown close errCh?
    28  	pe := NewParlError(chErr)
    29  	pe.Shutdown()
    30  	if !closePanics() {
    31  		t.Error("pe.Shutdown did not close errCh")
    32  	}
    33  
    34  	expected := 1
    35  	chErr = make(chan error)
    36  	err := errors.New("message")
    37  
    38  	// what happens on shutdown with blocking thread?
    39  	pe = NewParlError(chErr)
    40  	pe.AddError(err) // a thread is now blocked sending err on errCh
    41  	pe.Shutdown()    // closes errCh
    42  	list := ErrorList(pe.GetError())
    43  	actualInt := len(list)
    44  
    45  	if actualInt != expected {
    46  		sList := make([]string, len(list))
    47  		for i, e := range list {
    48  			sList[i] = e.Error()
    49  		}
    50  		t.Errorf("Error count: %d expected: %d: [%s]", actualInt, expected, strings.Join(sList, ",\x20"))
    51  	}
    52  
    53  }
    54  
    55  func TestParlError(t *testing.T) {
    56  	e1 := errors.New("error1")
    57  	e2 := errors.New("error2")
    58  
    59  	var err error
    60  	var actualString string
    61  	var actualInt int
    62  
    63  	// use of uninitialized instance
    64  	var parlError ParlError
    65  	err = parlError.GetError()
    66  	if err != nil {
    67  		t.Error("initial err was not nil")
    68  	}
    69  
    70  	// Error() when error is nil
    71  	actualString = parlError.Error()
    72  	if actualString != peNil {
    73  		t.Errorf("bad Error() value for nil: %q expected: %q", actualString, peNil)
    74  	}
    75  
    76  	err = parlError.AddError(e1)
    77  	if err != e1 {
    78  		t.Error("First AdError bad return value")
    79  	}
    80  	parlError.AddErrorProc(e2)
    81  
    82  	actualString = parlError.Error()
    83  	if actualString != e1.Error() {
    84  		t.Errorf("bad Error() value: %q expected: %q", actualString, e1.Error())
    85  	}
    86  
    87  	err = parlError.GetError()
    88  	actualInt = len(ErrorList(err))
    89  	if actualInt != 2 {
    90  		t.Errorf("bad error encapsulation: %q expected: 2", actualInt)
    91  	}
    92  }
    93  
    94  func TestParlErrorp(t *testing.T) {
    95  
    96  	var err error
    97  
    98  	// a nil parlError pointer causes panics
    99  	// this will not usually happen, because pointers are not used
   100  	// parlError nil situations
   101  	var parlErrorp *ParlError
   102  
   103  	// panic: runtime error: invalid memory address or nil pointer dereference
   104  	//err = parlErrorp.GetError()
   105  	_ = parlErrorp
   106  	_ = err
   107  }