From a3bda691d4baedac72234c40f56148e0381a5e60 Mon Sep 17 00:00:00 2001 From: Shihyu Ho Date: Sat, 20 Jan 2024 10:22:46 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=90=AC=E7=A7=BB=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/release.yaml | 2 +- .github/workflows/test.yaml | 2 +- Makefile | 2 +- calc/calculator_test.go | 112 ++++++++++++++------------------- calc/java_tool_options.go | 6 +- main_test.go | 43 +++++++++++++ 6 files changed, 96 insertions(+), 71 deletions(-) create mode 100644 main_test.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index d3be2ba..59831f2 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -24,7 +24,7 @@ jobs: goos: ${{ matrix.goos }} goarch: ${{ matrix.goarch }} goversion: https://dl.google.com/go/go1.20.linux-amd64.tar.gz - pre_command: go test ./... -v && export CGO_ENABLED=0 + pre_command: go test ./... && export CGO_ENABLED=0 ldflags: -s -w -X main.version=${{ github.event.release.tag_name }} binary_name: memory-calculator extra_files: entrypoint.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e08447e..6506a96 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -17,4 +17,4 @@ jobs: - name: Build run: go build -v ./... - name: Test with the Go CLI - run: go test ./... -v + run: go test ./... diff --git a/Makefile b/Makefile index ce6a1a7..b632256 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ gofmt: ## Run gofmt gofmt -s -w . test: ## Run tests - go test ./... -v + go test ./... build: bootstrap govet gofmt ## Build app go build -o ./build/memory-calculator diff --git a/calc/calculator_test.go b/calc/calculator_test.go index c2b3032..fd6c1b7 100644 --- a/calc/calculator_test.go +++ b/calc/calculator_test.go @@ -1,100 +1,82 @@ package calc import ( - "strings" - "testing" + "testing" ) type MockContributor struct { - Called bool + Called bool } func (m *MockContributor) Contribute() error { - m.Called = true - return nil + m.Called = true + return nil } func TestCalculator_Contribute(t *testing.T) { - mockContributor := &MockContributor{} + mockContributor := &MockContributor{} - err := contribute(mockContributor) - if err != nil { - t.Fatalf("contribute returned an error: %v", err) - } + err := contribute(mockContributor) + if err != nil { + t.Fatalf("contribute returned an error: %v", err) + } - if !mockContributor.Called { - t.Errorf("Contributor's Contribute method was not called") - } + if !mockContributor.Called { + t.Errorf("Contributor's Contribute method was not called") + } } func TestCalculator_buildHelpers_HasCaCerts(t *testing.T) { - calculator := NewCalculator() - calculator.JVMCacerts.Set("some-value") + calculator := NewCalculator() + calculator.JVMCacerts.Set("some-value") - helpers, err := calculator.buildHelpers() - if err != nil { - t.Fatalf("buildHelpers returned an error: %v", err) - } + helpers, err := calculator.buildHelpers() + if err != nil { + t.Fatalf("buildHelpers returned an error: %v", err) + } - if _, ok := helpers[helperOpensslCertificateLoader]; !ok { - t.Errorf(helperOpensslCertificateLoader + " helper not found in helpers") - } + if _, ok := helpers[helperOpensslCertificateLoader]; !ok { + t.Errorf(helperOpensslCertificateLoader + " helper not found in helpers") + } } func TestCalculator_buildHelpers_NoCaCerts(t *testing.T) { - calculator := NewCalculator() + calculator := NewCalculator() - helpers, err := calculator.buildHelpers() - if err != nil { - t.Fatalf("buildHelpers returned an error: %v", err) - } + helpers, err := calculator.buildHelpers() + if err != nil { + t.Fatalf("buildHelpers returned an error: %v", err) + } - if _, ok := helpers[helperOpensslCertificateLoader]; ok { - t.Errorf(helperOpensslCertificateLoader + " helper should not be present") - } + if _, ok := helpers[helperOpensslCertificateLoader]; ok { + t.Errorf(helperOpensslCertificateLoader + " helper should not be present") + } } func TestCalculator_buildHelpers_EnableNmt(t *testing.T) { - calculator := NewCalculator() - *calculator.EnableNmt = true + calculator := NewCalculator() + *calculator.EnableNmt = true - helpers, err := calculator.buildHelpers() - if err != nil { - t.Fatalf("buildHelpers returned an error: %v", err) - } + helpers, err := calculator.buildHelpers() + if err != nil { + t.Fatalf("buildHelpers returned an error: %v", err) + } - if _, ok := helpers[helperNmt]; !ok { - t.Errorf(helperNmt + " helper should be present") - } + if _, ok := helpers[helperNmt]; !ok { + t.Errorf(helperNmt + " helper should be present") + } } func TestCalculator_buildHelpers_DisableNmt(t *testing.T) { - calculator := NewCalculator() - *calculator.EnableNmt = false + calculator := NewCalculator() + *calculator.EnableNmt = false - helpers, err := calculator.buildHelpers() - if err != nil { - t.Fatalf("buildHelpers returned an error: %v", err) - } + helpers, err := calculator.buildHelpers() + if err != nil { + t.Fatalf("buildHelpers returned an error: %v", err) + } - if _, ok := helpers[helperNmt]; ok { - t.Errorf(helperNmt + " helper should not be present") - } -} - -func TestCalculator_Execute_Success(t *testing.T) { - calculator := NewCalculator() - *calculator.LoadedClassCount = 1000 - - jto, err := calculator.Execute() - if err != nil { - t.Fatalf("Execute returned an error: %v", err) - } - options := jto.String() - for _, o := range contributeOptions { - options = strings.ReplaceAll(options, o, "") - } - if options == "" { - t.Errorf("Execute returned an empty string") - } + if _, ok := helpers[helperNmt]; ok { + t.Errorf(helperNmt + " helper should not be present") + } } diff --git a/calc/java_tool_options.go b/calc/java_tool_options.go index 5ee421b..ce7fbbe 100644 --- a/calc/java_tool_options.go +++ b/calc/java_tool_options.go @@ -11,8 +11,8 @@ const ( separator = " " ) -// 固定要加上的參數, 這些參數可能是 libjvm 在 build image 時加的而非計算出來的, 或是我們自己想要加上去的都可以放 -var contributeOptions = []string{"-XX:+ExitOnOutOfMemoryError"} +// ContributeOptions 固定要貢獻 jvm 的參數, 這些參數可能是 libjvm 在 build image 時加的而非計算出來的, 或是我們自己想要加上去的都可以放 +var ContributeOptions = []string{"-XX:+ExitOnOutOfMemoryError"} type JavaToolOptions string @@ -21,7 +21,7 @@ func BuildJavaToolOptions() *JavaToolOptions { if val, ok := os.LookupEnv(EnvJavaToolOptions); ok { o = val } - for _, option := range contributeOptions { + for _, option := range ContributeOptions { if !strings.Contains(o, option) { if o == "" { o = option diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..ae802b5 --- /dev/null +++ b/main_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "github.com/softleader/memory-calculator/calc" + "strings" + "testing" +) + +func TestMemoryCalculation_WithLoadedClassCount(t *testing.T) { + calculator := calc.NewCalculator() + *calculator.LoadedClassCount = 1000 + + jto, err := calculator.Execute() + if err != nil { + t.Fatalf("Execute returned an error: %v", err) + } + options := jto.String() + for _, o := range calc.ContributeOptions { + options = strings.ReplaceAll(options, o, "") + } + if options == " " { + t.Errorf("Execute returned an empty string") + } +} + +func TestMemoryCalculation_WithoutLoadedClassCount(t *testing.T) { + calculator := calc.NewCalculator() + *calculator.AppPath = "." + *calculator.JVMClassCount = 1000 + *calculator.JVMClassAdj = "150%" + + jto, err := calculator.Execute() + if err != nil { + t.Fatalf("Execute returned an error: %v", err) + } + options := jto.String() + for _, o := range calc.ContributeOptions { + options = strings.ReplaceAll(options, o, "") + } + if options == " " { + t.Errorf("Execute returned an empty string") + } +}