github.com/haraldrudell/parl@v0.4.176/channel-send_test.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parl
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/haraldrudell/parl/perrors"
    12  )
    13  
    14  func TestChannelSend(t *testing.T) {
    15  	var errCh = make(chan error, 1)
    16  	var err0 error
    17  
    18  	var didSend, isNilChannel, isClosedChannel bool
    19  	var err error
    20  
    21  	// successful send
    22  	didSend, isNilChannel, isClosedChannel, err = ChannelSend(errCh, err0)
    23  	if !didSend {
    24  		t.Error("didSend false")
    25  	}
    26  	if isNilChannel {
    27  		t.Error("isNilChannel true")
    28  	}
    29  	if isClosedChannel {
    30  		t.Error("isClosedChannel true")
    31  	}
    32  	if err != nil {
    33  		t.Errorf("ChannelSend err: %s", perrors.Short(err))
    34  	}
    35  }
    36  
    37  func TestChannelSendNil(t *testing.T) {
    38  	var errCh chan error
    39  	var err0 error
    40  
    41  	var didSend, isNilChannel, isClosedChannel bool
    42  	var err error
    43  
    44  	// nil send
    45  	didSend, isNilChannel, isClosedChannel, err = ChannelSend(errCh, err0)
    46  	if didSend {
    47  		t.Error("didSend true")
    48  	}
    49  	if !isNilChannel {
    50  		t.Error("isNilChannel false")
    51  	}
    52  	if isClosedChannel {
    53  		t.Error("isClosedChannel true")
    54  	}
    55  	if err == nil {
    56  		t.Error("ChannelSend missing err")
    57  	}
    58  }
    59  
    60  func TestChannelSendClosed(t *testing.T) {
    61  	var errCh = make(chan error, 1)
    62  	var err0 error
    63  
    64  	var didSend, isNilChannel, isClosedChannel bool
    65  	var err error
    66  
    67  	// closed send
    68  	close(errCh)
    69  	didSend, isNilChannel, isClosedChannel, err = ChannelSend(errCh, err0)
    70  	if didSend {
    71  		t.Error("didSend true")
    72  	}
    73  	if isNilChannel {
    74  		t.Error("isNilChannel true")
    75  	}
    76  	if !isClosedChannel {
    77  		t.Error("isClosedChannel false")
    78  	}
    79  	if err == nil {
    80  		t.Error("ChannelSend missing err")
    81  	}
    82  }
    83  
    84  func TestChannelSendNonBlocking(t *testing.T) {
    85  	var errCh = make(chan error)
    86  	var err0 error
    87  
    88  	var didSend, isNilChannel, isClosedChannel bool
    89  	var err error
    90  
    91  	// non-blocking send
    92  	didSend, isNilChannel, isClosedChannel, err = ChannelSend(errCh, err0, SendNonBlocking)
    93  	if didSend {
    94  		t.Error("didSend true")
    95  	}
    96  	if isNilChannel {
    97  		t.Error("isNilChannel true")
    98  	}
    99  	if isClosedChannel {
   100  		t.Error("isClosedChannel true")
   101  	}
   102  	if err != nil {
   103  		t.Errorf("ChannelSend err: %s", perrors.Short(err))
   104  	}
   105  }