...

Source file src/k8s.io/kubernetes/pkg/util/async/runner_test.go

Documentation: k8s.io/kubernetes/pkg/util/async

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     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 async
    18  
    19  import (
    20  	"fmt"
    21  	"sync"
    22  	"testing"
    23  )
    24  
    25  func TestRunner(t *testing.T) {
    26  	var (
    27  		lock   sync.Mutex
    28  		events []string
    29  		funcs  []func(chan struct{})
    30  	)
    31  	done := make(chan struct{}, 20)
    32  	for i := 0; i < 10; i++ {
    33  		iCopy := i
    34  		funcs = append(funcs, func(c chan struct{}) {
    35  			lock.Lock()
    36  			events = append(events, fmt.Sprintf("%v starting\n", iCopy))
    37  			lock.Unlock()
    38  			<-c
    39  			lock.Lock()
    40  			events = append(events, fmt.Sprintf("%v stopping\n", iCopy))
    41  			lock.Unlock()
    42  			done <- struct{}{}
    43  		})
    44  	}
    45  
    46  	r := NewRunner(funcs...)
    47  	r.Start()
    48  	r.Stop()
    49  	for i := 0; i < 10; i++ {
    50  		<-done
    51  	}
    52  	if len(events) != 20 {
    53  		t.Errorf("expected 20 events, but got:\n%v\n", events)
    54  	}
    55  }
    56  

View as plain text