github.com/haraldrudell/parl@v0.4.176/pruntime/errors_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 pruntime
     7  
     8  import (
     9  	"errors"
    10  	"testing"
    11  )
    12  
    13  func TestIsRuntimeError(t *testing.T) {
    14  
    15  	// test non-runtime.Error
    16  	err := errors.New("err")
    17  	actual := IsRuntimeError(err)
    18  	if actual != nil {
    19  		t.Error("IsRuntimeError true when expected false")
    20  	}
    21  
    22  	// test runtime.Error
    23  	getRuntimeError := func() (err error) {
    24  		defer func() {
    25  			err = recover().(error)
    26  		}()
    27  		var ch chan struct{}
    28  		close(ch)
    29  		return
    30  	}
    31  	err = getRuntimeError()
    32  	actual = IsRuntimeError(err)
    33  	if actual == nil {
    34  		t.Errorf("IsRuntimeError bad result: %v expected err: %v", actual, err)
    35  	}
    36  }
    37  
    38  func TestIsSendOnClosedChannel(t *testing.T) {
    39  	// test runtime.Error
    40  	getRuntimeError := func() (err error) {
    41  		defer func() {
    42  			err = recover().(error)
    43  		}()
    44  		var ch chan struct{}
    45  		close(ch)
    46  		return
    47  	}
    48  	err := getRuntimeError()
    49  	actualBool := IsSendOnClosedChannel(err)
    50  	if actualBool {
    51  		t.Error("IsSendOnClosedChannel true when expected false")
    52  	}
    53  
    54  	getCloseError := func() (err error) {
    55  		defer func() {
    56  			err = recover().(error)
    57  		}()
    58  		ch := make(chan struct{})
    59  		close(ch)
    60  		ch <- struct{}{}
    61  		return
    62  	}
    63  	err = getCloseError()
    64  	actualBool = IsSendOnClosedChannel(err)
    65  	if !actualBool {
    66  		t.Errorf("IsSendOnClosedChannel\n%q when expected true:\n%q", rtSendOnClosedChannel, err)
    67  	}
    68  }