mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	
		
			
	
	
		
			42 lines
		
	
	
		
			763 B
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			42 lines
		
	
	
		
			763 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								package health
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"context"
							 | 
						||
| 
								 | 
							
									"net/http"
							 | 
						||
| 
								 | 
							
									"net/http/httptest"
							 | 
						||
| 
								 | 
							
									"testing"
							 | 
						||
| 
								 | 
							
									"time"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func Test_health_overloaded_cancellation(t *testing.T) {
							 | 
						||
| 
								 | 
							
									ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
							 | 
						||
| 
								 | 
							
										time.Sleep(1 * time.Second)
							 | 
						||
| 
								 | 
							
										w.WriteHeader(http.StatusOK)
							 | 
						||
| 
								 | 
							
									}))
							 | 
						||
| 
								 | 
							
									defer ts.Close()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									ctx := context.Background()
							 | 
						||
| 
								 | 
							
									ctx, cancel := context.WithCancel(ctx)
							 | 
						||
| 
								 | 
							
									h := &health{
							 | 
						||
| 
								 | 
							
										Addr: ts.URL,
							 | 
						||
| 
								 | 
							
										stop: cancel,
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									stopped := make(chan struct{})
							 | 
						||
| 
								 | 
							
									go func() {
							 | 
						||
| 
								 | 
							
										h.overloaded(ctx)
							 | 
						||
| 
								 | 
							
										stopped <- struct{}{}
							 | 
						||
| 
								 | 
							
									}()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// wait for overloaded function to start atleast once
							 | 
						||
| 
								 | 
							
									time.Sleep(1 * time.Second)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									cancel()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									select {
							 | 
						||
| 
								 | 
							
									case <-stopped:
							 | 
						||
| 
								 | 
							
									case <-time.After(5 * time.Second):
							 | 
						||
| 
								 | 
							
										t.Fatal("overloaded function should have been cancelled")
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |