Skip to content

Commit

Permalink
test: 搬移 test
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Jan 20, 2024
1 parent 64ae5a7 commit a3bda69
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: Build
run: go build -v ./...
- name: Test with the Go CLI
run: go test ./... -v
run: go test ./...
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 47 additions & 65 deletions calc/calculator_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
6 changes: 3 additions & 3 deletions calc/java_tool_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit a3bda69

Please sign in to comment.