github.com/vmware/govmomi@v0.51.0/vim25/progress/aggregator_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package progress
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestAggregatorNoSinks(t *testing.T) {
    13  	ch := make(chan Report)
    14  	a := NewAggregator(dummySinker{ch})
    15  	a.Done()
    16  
    17  	_, ok := <-ch
    18  	if ok {
    19  		t.Errorf("Expected channel to be closed")
    20  	}
    21  }
    22  
    23  func TestAggregatorMultipleSinks(t *testing.T) {
    24  	ch := make(chan Report)
    25  	a := NewAggregator(dummySinker{ch})
    26  
    27  	for i := 0; i < 5; i++ {
    28  		go func(ch chan<- Report) {
    29  			ch <- dummyReport{}
    30  			ch <- dummyReport{}
    31  			close(ch)
    32  		}(a.Sink())
    33  
    34  		<-ch
    35  		<-ch
    36  	}
    37  
    38  	a.Done()
    39  
    40  	_, ok := <-ch
    41  	if ok {
    42  		t.Errorf("Expected channel to be closed")
    43  	}
    44  }
    45  
    46  func TestAggregatorSinkInFlightOnDone(t *testing.T) {
    47  	ch := make(chan Report)
    48  	a := NewAggregator(dummySinker{ch})
    49  
    50  	// Simulate upstream
    51  	go func(ch chan<- Report) {
    52  		time.Sleep(1 * time.Millisecond)
    53  		ch <- dummyReport{}
    54  		close(ch)
    55  	}(a.Sink())
    56  
    57  	// Drain downstream
    58  	go func(ch <-chan Report) {
    59  		<-ch
    60  	}(ch)
    61  
    62  	// This should wait for upstream to complete
    63  	a.Done()
    64  
    65  	_, ok := <-ch
    66  	if ok {
    67  		t.Errorf("Expected channel to be closed")
    68  	}
    69  }