github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/engine/processbar/processbar_test.go (about)

     1  // Copyright 2017 The GoReporter Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //    http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package processbar
    15  
    16  import (
    17  	"fmt"
    18  	"sync"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestLinterProcessBar(t *testing.T) {
    24  	lintersProcessChans := make(chan int64, 20)
    25  	lintersFinishedSignal := make(chan string, 10)
    26  	go LinterProcessBar(lintersProcessChans, lintersFinishedSignal)
    27  	wg := sync.WaitGroup{}
    28  	for i := 1; i <= 100; i++ {
    29  		wg.Add(1)
    30  		time.Sleep(100 * time.Microsecond)
    31  		go func(i int) {
    32  			lintersProcessChans <- 1
    33  			if i%10 == 0 {
    34  				lintersFinishedSignal <- fmt.Sprintf("go-routine %d done\n", i)
    35  			}
    36  			wg.Done()
    37  		}(i)
    38  	}
    39  
    40  	wg.Wait()
    41  	close(lintersFinishedSignal)
    42  	close(lintersProcessChans)
    43  	return
    44  }