mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	Merge pull request #279 from pcasaretto/coverage-up
Test app.SetCPU, config.makeOnces, config.makeStorages
This commit is contained in:
		
						commit
						159eb68a11
					
				
							
								
								
									
										42
									
								
								app/app_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								app/app_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
			
		||||
package app_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/mholt/caddy/app"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestSetCPU(t *testing.T) {
 | 
			
		||||
	currentCPU := runtime.GOMAXPROCS(-1)
 | 
			
		||||
	maxCPU := runtime.NumCPU()
 | 
			
		||||
	for i, test := range []struct {
 | 
			
		||||
		input     string
 | 
			
		||||
		output    int
 | 
			
		||||
		shouldErr bool
 | 
			
		||||
	}{
 | 
			
		||||
		{"1", 1, false},
 | 
			
		||||
		{"-1", currentCPU, true},
 | 
			
		||||
		{"0", currentCPU, true},
 | 
			
		||||
		{"100%", maxCPU, false},
 | 
			
		||||
		{"50%", int(0.5 * float32(maxCPU)), false},
 | 
			
		||||
		{"110%", currentCPU, true},
 | 
			
		||||
		{"-10%", currentCPU, true},
 | 
			
		||||
		{"invalid input", currentCPU, true},
 | 
			
		||||
		{"invalid input%", currentCPU, true},
 | 
			
		||||
		{"9999", maxCPU, false}, // over available CPU
 | 
			
		||||
	} {
 | 
			
		||||
		err := app.SetCPU(test.input)
 | 
			
		||||
		if test.shouldErr && err == nil {
 | 
			
		||||
			t.Errorf("Test %d: Expected error, but there wasn't any", i)
 | 
			
		||||
		}
 | 
			
		||||
		if !test.shouldErr && err != nil {
 | 
			
		||||
			t.Errorf("Test %d: Expected no error, but there was one: %v", i, err)
 | 
			
		||||
		}
 | 
			
		||||
		if actual, expected := runtime.GOMAXPROCS(-1), test.output; actual != expected {
 | 
			
		||||
			t.Errorf("Test %d: GOMAXPROCS was %d but expected %d", i, actual, expected)
 | 
			
		||||
		}
 | 
			
		||||
		// teardown
 | 
			
		||||
		runtime.GOMAXPROCS(currentCPU)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -1,6 +1,8 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/mholt/caddy/server"
 | 
			
		||||
@ -62,3 +64,61 @@ func TestResolveAddr(t *testing.T) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMakeOnces(t *testing.T) {
 | 
			
		||||
	directives := []directive{
 | 
			
		||||
		{"dummy", nil},
 | 
			
		||||
		{"dummy2", nil},
 | 
			
		||||
	}
 | 
			
		||||
	directiveOrder = directives
 | 
			
		||||
	onces := makeOnces()
 | 
			
		||||
	if len(onces) != len(directives) {
 | 
			
		||||
		t.Errorf("onces had len %d , expected %d", len(onces), len(directives))
 | 
			
		||||
	}
 | 
			
		||||
	expected := map[string]*sync.Once{
 | 
			
		||||
		"dummy":  new(sync.Once),
 | 
			
		||||
		"dummy2": new(sync.Once),
 | 
			
		||||
	}
 | 
			
		||||
	if !reflect.DeepEqual(onces, expected) {
 | 
			
		||||
		t.Errorf("onces was %v, expected %v", onces, expected)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMakeStorages(t *testing.T) {
 | 
			
		||||
	directives := []directive{
 | 
			
		||||
		{"dummy", nil},
 | 
			
		||||
		{"dummy2", nil},
 | 
			
		||||
	}
 | 
			
		||||
	directiveOrder = directives
 | 
			
		||||
	storages := makeStorages()
 | 
			
		||||
	if len(storages) != len(directives) {
 | 
			
		||||
		t.Errorf("storages had len %d , expected %d", len(storages), len(directives))
 | 
			
		||||
	}
 | 
			
		||||
	expected := map[string]interface{}{
 | 
			
		||||
		"dummy":  nil,
 | 
			
		||||
		"dummy2": nil,
 | 
			
		||||
	}
 | 
			
		||||
	if !reflect.DeepEqual(storages, expected) {
 | 
			
		||||
		t.Errorf("storages was %v, expected %v", storages, expected)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestValidDirective(t *testing.T) {
 | 
			
		||||
	directives := []directive{
 | 
			
		||||
		{"dummy", nil},
 | 
			
		||||
		{"dummy2", nil},
 | 
			
		||||
	}
 | 
			
		||||
	directiveOrder = directives
 | 
			
		||||
	for i, test := range []struct {
 | 
			
		||||
		directive string
 | 
			
		||||
		valid     bool
 | 
			
		||||
	}{
 | 
			
		||||
		{"dummy", true},
 | 
			
		||||
		{"dummy2", true},
 | 
			
		||||
		{"dummy3", false},
 | 
			
		||||
	} {
 | 
			
		||||
		if actual, expected := validDirective(test.directive), test.valid; actual != expected {
 | 
			
		||||
			t.Errorf("Test %d: valid was %t, expected %t", i, actual, expected)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user