github.com/vmware/govmomi@v0.37.1/vim25/progress/aggregator_test.go (about) 1 /* 2 Copyright (c) 2014 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package progress 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 func TestAggregatorNoSinks(t *testing.T) { 25 ch := make(chan Report) 26 a := NewAggregator(dummySinker{ch}) 27 a.Done() 28 29 _, ok := <-ch 30 if ok { 31 t.Errorf("Expected channel to be closed") 32 } 33 } 34 35 func TestAggregatorMultipleSinks(t *testing.T) { 36 ch := make(chan Report) 37 a := NewAggregator(dummySinker{ch}) 38 39 for i := 0; i < 5; i++ { 40 go func(ch chan<- Report) { 41 ch <- dummyReport{} 42 ch <- dummyReport{} 43 close(ch) 44 }(a.Sink()) 45 46 <-ch 47 <-ch 48 } 49 50 a.Done() 51 52 _, ok := <-ch 53 if ok { 54 t.Errorf("Expected channel to be closed") 55 } 56 } 57 58 func TestAggregatorSinkInFlightOnDone(t *testing.T) { 59 ch := make(chan Report) 60 a := NewAggregator(dummySinker{ch}) 61 62 // Simulate upstream 63 go func(ch chan<- Report) { 64 time.Sleep(1 * time.Millisecond) 65 ch <- dummyReport{} 66 close(ch) 67 }(a.Sink()) 68 69 // Drain downstream 70 go func(ch <-chan Report) { 71 <-ch 72 }(ch) 73 74 // This should wait for upstream to complete 75 a.Done() 76 77 _, ok := <-ch 78 if ok { 79 t.Errorf("Expected channel to be closed") 80 } 81 }