github.com/haraldrudell/parl@v0.4.176/perrors/errorglue/send-nb_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 errorglue
     7  
     8  import (
     9  	"errors"
    10  	"testing"
    11  )
    12  
    13  func TestSendNB(t *testing.T) {
    14  	var err1 = errors.New("err1")
    15  	var err2 = errors.New("err2")
    16  	var errCh = make(chan error)
    17  
    18  	var err error
    19  	var hasThread bool
    20  	var queueLen int
    21  
    22  	sendNb := NewSendNb(errCh)
    23  
    24  	sendNb.Send(err1)
    25  	sendNb.Send(err2)
    26  	err = <-errCh
    27  	if err != err1 {
    28  		t.Errorf("Got wrong error: %v expected: %v", err, err1)
    29  	}
    30  	sendNb.sqLock.Lock()
    31  	hasThread = sendNb.hasThread
    32  	sendNb.sqLock.Unlock()
    33  	if !hasThread {
    34  		t.Error("sendNB no thread")
    35  	}
    36  
    37  	if sendNb.IsShutdown() {
    38  		t.Error("sendNb.IsShutdown")
    39  	}
    40  	sendNb.Shutdown()
    41  	if !sendNb.IsShutdown() {
    42  		t.Error("sendNb.IsShutdown false")
    43  	}
    44  	sendNb.sqLock.Lock()
    45  	hasThread = sendNb.hasThread
    46  	queueLen = len(sendNb.sendQueue)
    47  	sendNb.sqLock.Unlock()
    48  	if hasThread {
    49  		t.Error("sendNB has thread")
    50  	}
    51  	if queueLen != 0 {
    52  		t.Errorf("sendNB queue length not 0: %d", queueLen)
    53  	}
    54  }