github.com/haraldrudell/parl@v0.4.176/awaitable_test.go (about)

     1  /*
     2  © 2023-present Harald Rudell <haraldrudell@proton.me> (https://haraldrudell.github.io/haraldrudell/)
     3  All rights reserved
     4  */
     5  
     6  package parl
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  func TestAwaitable(t *testing.T) {
    13  
    14  	var ch AwaitableCh
    15  	var isClosed, didClose bool
    16  
    17  	// Ch() Close() IsClosed()
    18  	var awaitable *Awaitable
    19  	var reset = func() {
    20  		awaitable = &Awaitable{}
    21  	}
    22  
    23  	// Ch is non-nil
    24  	reset()
    25  	ch = awaitable.Ch()
    26  	if ch == nil {
    27  		t.Error("Ch nil")
    28  	}
    29  	// Ch is open
    30  	var isOpen bool
    31  	select {
    32  	case <-ch:
    33  		isOpen = false
    34  	default:
    35  		isOpen = true
    36  	}
    37  	if !isOpen {
    38  		t.Error("Ch closed")
    39  	}
    40  	// IsClosed is false
    41  	isClosed = awaitable.IsClosed()
    42  	if isClosed {
    43  		t.Error("IsClosed true")
    44  	}
    45  
    46  	// Close works
    47  	didClose = awaitable.Close()
    48  	if !didClose {
    49  		t.Errorf("didClose false")
    50  	}
    51  	// Ch is closed
    52  	select {
    53  	case <-ch:
    54  		isOpen = false
    55  	default:
    56  		isOpen = true
    57  	}
    58  	if isOpen {
    59  		t.Error("Ch open")
    60  	}
    61  	// IsClosed is true
    62  	isClosed = awaitable.IsClosed()
    63  	if !isClosed {
    64  		t.Error("IsClosed false")
    65  	}
    66  }
    67  
    68  func TestZeroValue(t *testing.T) {
    69  	var awaitable Awaitable
    70  	awaitable.Close()
    71  	t.Log("OK")
    72  }