Skip to content

Commit

Permalink
Merge pull request #18 from dongxuny/master
Browse files Browse the repository at this point in the history
Enrich boot_test.go
  • Loading branch information
dongxuny committed Nov 1, 2021
2 parents 13d49f3 + 8cae3fd commit 73a6a38
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- name: Run linter
run: make lint
- name: Run test coverage
run: go test $(go list ./... | grep -v example) -race -coverprofile=coverage.txt -covermode=atomic
run: go test $(go list ./... | grep -v example) -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)
103 changes: 95 additions & 8 deletions boot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package rkboot

import (
"context"
"fmt"
"github.com/rookie-ninja/rk-entry/entry"
"github.com/stretchr/testify/assert"
"io/ioutil"
Expand All @@ -17,7 +18,7 @@ import (
"time"
)

func TestNewBoot_HappyCase(t *testing.T) {
func TestNewBoot_HappyCase_Gin(t *testing.T) {
config := `
---
gin:
Expand All @@ -26,7 +27,7 @@ gin:
enabled: true
`

filePath := createFileAtTestTempDir(t, config)
filePath := createFileAtTestTempDir(t, "ut-boot-gin.yaml", config)

boot := NewBoot(WithBootConfigPath(filePath))
boot.AddShutdownHookFunc("ut-shutdown", func() {
Expand All @@ -43,19 +44,105 @@ gin:
assert.Nil(t, boot.GetConfigEntry(""))
assert.Nil(t, boot.GetCertEntry(""))
assert.NotNil(t, boot.GetGinEntry("ut-gin"))
assert.Nil(t, boot.GetGrpcEntry(""))
assert.Nil(t, boot.GetEchoEntry("ut-gin"))
assert.Nil(t, boot.GetGrpcEntry("ut-gin"))
assert.Nil(t, boot.GetPromEntry(""))

go func() {
time.Sleep(3 * time.Second)
rkentry.GlobalAppCtx.GetShutdownSig() <- syscall.SIGTERM
boot.WaitForShutdownSig(context.TODO())
}()

boot.WaitForShutdownSig(context.TODO())
time.Sleep(1 * time.Second)
rkentry.GlobalAppCtx.GetShutdownSig() <- syscall.SIGTERM
time.Sleep(1 * time.Second)
rkentry.GlobalAppCtx.RemoveEntry("ut-gin")
}

func createFileAtTestTempDir(t *testing.T, content string) string {
tempDir := path.Join(t.TempDir(), "ut-boot.yaml")
func TestNewBoot_HappyCase_Echo(t *testing.T) {
config := `
---
echo:
- name: ut-echo
port: 8080
enabled: true
`

filePath := createFileAtTestTempDir(t, "ut-boot-echo.yaml", config)

boot := NewBoot(WithBootConfigPath(filePath))
boot.AddShutdownHookFunc("ut-shutdown", func() {
// noop
})

boot.Bootstrap(context.TODO())

assert.NotNil(t, boot.GetAppInfoEntry())
assert.NotNil(t, boot.GetZapLoggerEntry(rkentry.DefaultZapLoggerEntryName))
assert.NotNil(t, boot.GetZapLoggerEntryDefault())
assert.NotNil(t, boot.GetEventLoggerEntry(rkentry.DefaultEventLoggerEntryName))
assert.NotNil(t, boot.GetEventLoggerEntryDefault())
assert.Nil(t, boot.GetConfigEntry(""))
assert.Nil(t, boot.GetCertEntry(""))
assert.Nil(t, boot.GetGinEntry("ut-echo"))
assert.NotNil(t, boot.GetEchoEntry("ut-echo"))
assert.Nil(t, boot.GetGrpcEntry("ut-echo"))
assert.Nil(t, boot.GetPromEntry(""))

go func() {
boot.WaitForShutdownSig(context.TODO())
}()

time.Sleep(1 * time.Second)
rkentry.GlobalAppCtx.GetShutdownSig() <- syscall.SIGTERM
time.Sleep(1 * time.Second)
rkentry.GlobalAppCtx.RemoveEntry("ut-echo")
}

func TestNewBoot_HappyCase_Grpc(t *testing.T) {
config := `
---
grpc:
- name: ut-grpc
port: 8080
enabled: true
`

filePath := createFileAtTestTempDir(t, "ut-boot-grpc.yaml", config)

boot := NewBoot(WithBootConfigPath(filePath))
fmt.Println(filePath)
boot.AddShutdownHookFunc("ut-shutdown", func() {
// noop
})

boot.Bootstrap(context.TODO())

assert.NotNil(t, boot.GetAppInfoEntry())
assert.NotNil(t, boot.GetZapLoggerEntry(rkentry.DefaultZapLoggerEntryName))
assert.NotNil(t, boot.GetZapLoggerEntryDefault())
assert.NotNil(t, boot.GetEventLoggerEntry(rkentry.DefaultEventLoggerEntryName))
assert.NotNil(t, boot.GetEventLoggerEntryDefault())
assert.Nil(t, boot.GetConfigEntry(""))
assert.Nil(t, boot.GetCertEntry(""))
assert.Nil(t, boot.GetGinEntry("ut-grpc"))
assert.Nil(t, boot.GetEchoEntry("ut-grpc"))
assert.NotNil(t, boot.GetGrpcEntry("ut-grpc"))
assert.Nil(t, boot.GetPromEntry(""))

go func() {
boot.WaitForShutdownSig(context.TODO())
}()

time.Sleep(1 * time.Second)
rkentry.GlobalAppCtx.GetShutdownSig() <- syscall.SIGTERM
time.Sleep(1 * time.Second)
rkentry.GlobalAppCtx.RemoveEntry("ut-grpc")

}

func createFileAtTestTempDir(t *testing.T, filename, content string) string {
tempDir := path.Join(t.TempDir(), filename)

assert.Nil(t, ioutil.WriteFile(tempDir, []byte(content), os.ModePerm))
return tempDir
}

0 comments on commit 73a6a38

Please sign in to comment.