github.com/haraldrudell/parl@v0.4.176/close-channel_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 parl
     7  
     8  import "testing"
     9  
    10  func TestCloseChannel(t *testing.T) {
    11  	var value = 3
    12  	var doDrain = true
    13  
    14  	var ch chan int
    15  	var err, errp error
    16  	var n int
    17  	var isNilChannel, isCloseOfClosedChannel bool
    18  
    19  	// close of nil channel should return isNilChannel true
    20  	ch = nil
    21  	isNilChannel, isCloseOfClosedChannel, n, err = CloseChannel(ch, &errp)
    22  	if !isNilChannel {
    23  		t.Error("isNilChannel false")
    24  	}
    25  	_ = err
    26  	_ = n
    27  	_ = isCloseOfClosedChannel
    28  
    29  	// n should return number of items when draining
    30  	ch = make(chan int, 1)
    31  	ch <- value
    32  	isNilChannel, isCloseOfClosedChannel, n, err = CloseChannel(ch, &errp, doDrain)
    33  	if n != 1 {
    34  		t.Errorf("n bad %d exp %d", n, 1)
    35  	}
    36  	_ = isNilChannel
    37  	_ = err
    38  	_ = isCloseOfClosedChannel
    39  
    40  	// close of closed channel should set isCloseOfClosedChannel, err, errp
    41  	ch = make(chan int)
    42  	close(ch)
    43  	isNilChannel, isCloseOfClosedChannel, n, err = CloseChannel(ch, &errp)
    44  	if !isCloseOfClosedChannel {
    45  		t.Error("isCloseOfClosedChannel false")
    46  	}
    47  	if err == nil {
    48  		t.Error("isCloseOfClosedChannel err nil")
    49  	}
    50  	if errp == nil {
    51  		t.Error("isCloseOfClosedChannel errp nil")
    52  	}
    53  	_ = isNilChannel
    54  	_ = n
    55  }