mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	add work queue; test coverage 100%
This commit is contained in:
		
							
								
								
									
										115
									
								
								pkg/util/workqueue/queue_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								pkg/util/workqueue/queue_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,115 @@
 | 
			
		||||
/*
 | 
			
		||||
Copyright 2015 Google Inc. All rights reserved.
 | 
			
		||||
 | 
			
		||||
Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
you may not use this file except in compliance with the License.
 | 
			
		||||
You may obtain a copy of the License at
 | 
			
		||||
 | 
			
		||||
    http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 | 
			
		||||
Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
See the License for the specific language governing permissions and
 | 
			
		||||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
package workqueue_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util/workqueue"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestBasic(t *testing.T) {
 | 
			
		||||
	// If something is seriously wrong this test will never complete.
 | 
			
		||||
	q := workqueue.New()
 | 
			
		||||
 | 
			
		||||
	// Start producers
 | 
			
		||||
	const producers = 50
 | 
			
		||||
	producerWG := sync.WaitGroup{}
 | 
			
		||||
	producerWG.Add(producers)
 | 
			
		||||
	for i := 0; i < producers; i++ {
 | 
			
		||||
		go func(i int) {
 | 
			
		||||
			defer producerWG.Done()
 | 
			
		||||
			for j := 0; j < 50; j++ {
 | 
			
		||||
				q.Add(i)
 | 
			
		||||
				time.Sleep(time.Millisecond)
 | 
			
		||||
			}
 | 
			
		||||
		}(i)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Start consumers
 | 
			
		||||
	const consumers = 10
 | 
			
		||||
	consumerWG := sync.WaitGroup{}
 | 
			
		||||
	consumerWG.Add(consumers)
 | 
			
		||||
	for i := 0; i < consumers; i++ {
 | 
			
		||||
		go func(i int) {
 | 
			
		||||
			defer consumerWG.Done()
 | 
			
		||||
			for {
 | 
			
		||||
				item, quit := q.Get()
 | 
			
		||||
				if item == "added after shutdown!" {
 | 
			
		||||
					t.Errorf("Got an item added after shutdown.")
 | 
			
		||||
				}
 | 
			
		||||
				if quit {
 | 
			
		||||
					return
 | 
			
		||||
				}
 | 
			
		||||
				t.Logf("Worker %v: begin processing %v", i, item)
 | 
			
		||||
				time.Sleep(3 * time.Millisecond)
 | 
			
		||||
				t.Logf("Worker %v: done processing %v", i, item)
 | 
			
		||||
				q.Done(item)
 | 
			
		||||
			}
 | 
			
		||||
		}(i)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	producerWG.Wait()
 | 
			
		||||
	q.ShutDown()
 | 
			
		||||
	q.Add("added after shutdown!")
 | 
			
		||||
	consumerWG.Wait()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAddWhileProcessing(t *testing.T) {
 | 
			
		||||
	q := workqueue.New()
 | 
			
		||||
 | 
			
		||||
	// Start producers
 | 
			
		||||
	const producers = 50
 | 
			
		||||
	producerWG := sync.WaitGroup{}
 | 
			
		||||
	producerWG.Add(producers)
 | 
			
		||||
	for i := 0; i < producers; i++ {
 | 
			
		||||
		go func(i int) {
 | 
			
		||||
			defer producerWG.Done()
 | 
			
		||||
			q.Add(i)
 | 
			
		||||
		}(i)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Start consumers
 | 
			
		||||
	const consumers = 10
 | 
			
		||||
	consumerWG := sync.WaitGroup{}
 | 
			
		||||
	consumerWG.Add(consumers)
 | 
			
		||||
	for i := 0; i < consumers; i++ {
 | 
			
		||||
		go func(i int) {
 | 
			
		||||
			defer consumerWG.Done()
 | 
			
		||||
			// Every worker will re-add every item up to two times.
 | 
			
		||||
			// This tests the dirty-while-processing case.
 | 
			
		||||
			counters := map[interface{}]int{}
 | 
			
		||||
			for {
 | 
			
		||||
				item, quit := q.Get()
 | 
			
		||||
				if quit {
 | 
			
		||||
					return
 | 
			
		||||
				}
 | 
			
		||||
				counters[item]++
 | 
			
		||||
				if counters[item] < 2 {
 | 
			
		||||
					q.Add(item)
 | 
			
		||||
				}
 | 
			
		||||
				q.Done(item)
 | 
			
		||||
			}
 | 
			
		||||
		}(i)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	producerWG.Wait()
 | 
			
		||||
	q.ShutDown()
 | 
			
		||||
	consumerWG.Wait()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user