Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 22, 2023
1 parent 0f558f5 commit bd56220
Show file tree
Hide file tree
Showing 62 changed files with 1,098 additions and 596 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- Update `cmd/hidden_lake/service`: append client.ResetPrivKey method
- Update `pkg/storage/database`: append check on correct input auth/encryption key
- Update `pkg`: update tests (coverage > 90%)
- Update `*_test`: change all tests to parallel actions
- Update `README.md`: append new badge - coverage

### CHANGES

Expand Down
43 changes: 30 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
N=1
TEST_PATH=./test/result
PPROF_PATH=./test/pprof
CHECK_ERROR=if [ $$? != 0 ]; then exit 1; fi

PPROF_NAME=_
PPROF_PORT=_

# updates in 'test-coverage-badge' block
_COVERAGE_RAW=_
_COVERAGE_VAR=_

_TEST_RESULT_PATH=./test/result
_TEST_PPROF_PATH=./test/pprof

_CHECK_ERROR=if [ $$? != 0 ]; then exit 1; fi

.PHONY: default clean \
test-run test-coverage test-coverage-view \
pprof-run \
Expand All @@ -26,20 +32,31 @@ test-run: clean
for i in {1..$(N)}; do \
echo $$i; \
go test -cover -count=1 `go list ./...`; \
$(CHECK_ERROR); \
$(_CHECK_ERROR); \
done; \
echo "Build took $$(($$(date +%s)-d)) seconds";

test-coverage: clean
go test -coverprofile=$(TEST_PATH)/coverage.out `go list ./...`
$(CHECK_ERROR);
go test -coverprofile=$(_TEST_RESULT_PATH)/coverage.out -count=1 `go list ./...`
$(_CHECK_ERROR);

test-coverage-view:
go tool cover -html=$(TEST_PATH)/coverage.out
go tool cover -html=$(_TEST_RESULT_PATH)/coverage.out

test-coverage-badge:
$(eval _COVERAGE_RAW=go tool cover -func=$(_TEST_RESULT_PATH)/coverage.out | grep total: | grep -Eo '[0-9]+\.[0-9]+')
$(eval _COVERAGE_VAR := $(shell echo "`${_COVERAGE_RAW}`/1" | bc))
if [ $(_COVERAGE_VAR) -lt 50 ]; then \
curl "https://img.shields.io/badge/coverage-$(_COVERAGE_VAR)%25-red" > $(_TEST_RESULT_PATH)/badge.svg; \
elif [ $(_COVERAGE_VAR) -gt 80 ]; then \
curl "https://img.shields.io/badge/coverage-$(_COVERAGE_VAR)%25-green" > $(_TEST_RESULT_PATH)/badge.svg; \
else \
curl "https://img.shields.io/badge/coverage-$(_COVERAGE_VAR)%25-orange" > $(_TEST_RESULT_PATH)/badge.svg; \
fi

### GIT

git-status: test-run
git-status: test-coverage test-coverage-badge
git add .
git status

Expand All @@ -53,8 +70,8 @@ git-push:
# make pprof-run PPROF_NAME=hlm PPROF_PORT=9593

pprof-run:
go tool pprof -png -output $(PPROF_PATH)/$(PPROF_NAME)/threadcreate.png http://localhost:$(PPROF_PORT)/debug/pprof/threadcreate
go tool pprof -png -output $(PPROF_PATH)/$(PPROF_NAME)/profile.png http://localhost:$(PPROF_PORT)/debug/pprof/profile?seconds=5
go tool pprof -png -output $(PPROF_PATH)/$(PPROF_NAME)/heap.png http://localhost:$(PPROF_PORT)/debug/pprof/heap
go tool pprof -png -output $(PPROF_PATH)/$(PPROF_NAME)/goroutine.png http://localhost:$(PPROF_PORT)/debug/pprof/goroutine
go tool pprof -png -output $(PPROF_PATH)/$(PPROF_NAME)/allocs.png http://localhost:$(PPROF_PORT)/debug/pprof/allocs
go tool pprof -png -output $(_TEST_PPROF_PATH)/$(PPROF_NAME)/threadcreate.png http://localhost:$(PPROF_PORT)/debug/pprof/threadcreate
go tool pprof -png -output $(_TEST_PPROF_PATH)/$(PPROF_NAME)/profile.png http://localhost:$(PPROF_PORT)/debug/pprof/profile?seconds=5
go tool pprof -png -output $(_TEST_PPROF_PATH)/$(PPROF_NAME)/heap.png http://localhost:$(PPROF_PORT)/debug/pprof/heap
go tool pprof -png -output $(_TEST_PPROF_PATH)/$(PPROF_NAME)/goroutine.png http://localhost:$(PPROF_PORT)/debug/pprof/goroutine
go tool pprof -png -output $(_TEST_PPROF_PATH)/$(PPROF_NAME)/allocs.png http://localhost:$(PPROF_PORT)/debug/pprof/allocs
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<a href="https://github.com/number571/go-peer/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/number571/go-peer.svg" alt="License" />
</a>
<a href="https://github.com/number571/go-peer/tree/master/test/result/badge.svg">
<img src="test/result/badge.svg" alt="Coverage" />
</a>
<a href="https://github.com/number571/go-peer/releases">
<img src="https://img.shields.io/github/v/release/number571/go-peer.svg" alt="Release" />
</a>
Expand Down
24 changes: 16 additions & 8 deletions cmd/hidden_lake/service/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
tcConfigFile = "config_test.txt"
tcConfigFileTemplate = "config_test_%d.txt"
)

const (
Expand Down Expand Up @@ -111,10 +111,14 @@ func testConfigDefaultInit(configPath string) {
}

func TestConfig(t *testing.T) {
testConfigDefaultInit(tcConfigFile)
defer os.Remove(tcConfigFile)
t.Parallel()

cfg, err := LoadConfig(tcConfigFile)
configFile := fmt.Sprintf(tcConfigFileTemplate, 0)

testConfigDefaultInit(configFile)
defer os.Remove(configFile)

cfg, err := LoadConfig(configFile)
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -219,10 +223,14 @@ func TestConfig(t *testing.T) {
}

func TestWrapper(t *testing.T) {
testConfigDefaultInit(tcConfigFile)
defer os.Remove(tcConfigFile)
t.Parallel()

configFile := fmt.Sprintf(tcConfigFileTemplate, 1)

testConfigDefaultInit(configFile)
defer os.Remove(configFile)

cfg, err := LoadConfig(tcConfigFile)
cfg, err := LoadConfig(configFile)
if err != nil {
t.Error(err)
return
Expand All @@ -241,7 +249,7 @@ func TestWrapper(t *testing.T) {
return
}

cfg, err = LoadConfig(tcConfigFile)
cfg, err = LoadConfig(configFile)
if err != nil {
t.Error(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import (
)

func TestHandleConnectsAPI(t *testing.T) {
wcfg, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[6])
defer testAllFree(node, srv)
t.Parallel()

pathCfg := fmt.Sprintf(tcPathConfigTemplate, 0)
pathDB := fmt.Sprintf(tcPathDBTemplate, 0)

wcfg, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[6])
defer testAllFree(node, srv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import (
)

func TestHandleFriendsAPI(t *testing.T) {
wcfg, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[7])
defer testAllFree(node, srv)
t.Parallel()

pathCfg := fmt.Sprintf(tcPathConfigTemplate, 1)
pathDB := fmt.Sprintf(tcPathDBTemplate, 1)

wcfg, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[7])
defer testAllFree(node, srv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import (
)

func TestHandleConfigSettingsAPI(t *testing.T) {
t.Parallel()

addr := testutils.TgAddrs[26]
pathCfg := fmt.Sprintf(tcPathConfigTemplate, 2)
pathDB := fmt.Sprintf(tcPathDBTemplate, 2)

_, node, srv := testAllCreate(tcPathConfig, tcPathDB, addr)
defer testAllFree(node, srv)
_, node, srv := testAllCreate(pathCfg, pathDB, addr)
defer testAllFree(node, srv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
10 changes: 5 additions & 5 deletions cmd/hidden_lake/service/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

const (
tcServiceAddressInHLS = "hidden-echo-service"
tcPathDB = "database_test.db"
tcPathConfig = "config_test.cfg"
tcPathDBTemplate = "database_test_%d.db"
tcPathConfigTemplate = "config_test_%d.cfg"
)

var (
Expand Down Expand Up @@ -115,10 +115,10 @@ func testAllCreate(cfgPath, dbPath, srvAddr string) (config.IWrapper, anonymity.
return wcfg, node, srvc
}

func testAllFree(node anonymity.INode, srv *http.Server) {
func testAllFree(node anonymity.INode, srv *http.Server, pathCfg, pathDB string) {
defer func() {
os.RemoveAll(tcPathDB)
os.RemoveAll(tcPathConfig)
os.RemoveAll(pathDB)
os.RemoveAll(pathCfg)
}()
types.StopAll([]types.ICommand{
node,
Expand Down
8 changes: 6 additions & 2 deletions cmd/hidden_lake/service/internal/handler/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import (
)

func TestHandleIndexAPI(t *testing.T) {
t.Parallel()

addr := testutils.TgAddrs[22]
pathCfg := fmt.Sprintf(tcPathConfigTemplate, 3)
pathDB := fmt.Sprintf(tcPathDBTemplate, 3)

_, node, srv := testAllCreate(tcPathConfig, tcPathDB, addr)
defer testAllFree(node, srv)
_, node, srv := testAllCreate(pathCfg, pathDB, addr)
defer testAllFree(node, srv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
9 changes: 7 additions & 2 deletions cmd/hidden_lake/service/internal/handler/network_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import (
)

func TestHandleNetworkKeyAPI(t *testing.T) {
wcfg, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[25])
defer testAllFree(node, srv)
t.Parallel()

pathCfg := fmt.Sprintf(tcPathConfigTemplate, 4)
pathDB := fmt.Sprintf(tcPathDBTemplate, 4)

wcfg, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[25])
defer testAllFree(node, srv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ import (
)

func TestHandleMessageAPI(t *testing.T) {
_, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[24])
defer testAllFree(node, srv)
t.Parallel()

pathCfg := fmt.Sprintf(tcPathConfigTemplate, 5)
pathDB := fmt.Sprintf(tcPathDBTemplate, 5)

_, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[24])
defer testAllFree(node, srv, pathCfg, pathDB)

hlsClient := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
27 changes: 16 additions & 11 deletions cmd/hidden_lake/service/internal/handler/network_online_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ import (
)

func TestHandleOnlineAPI(t *testing.T) {
_, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[12])
defer testAllFree(node, srv)
t.Parallel()

pushNode := testAllOnlineCreate()
defer testAllOnlineFree(pushNode)
pathCfg := fmt.Sprintf(tcPathConfigTemplate, 6)
pathDB := fmt.Sprintf(tcPathDBTemplate, 6)

_, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[12])
defer testAllFree(node, srv, pathCfg, pathDB)

pushNode := testAllOnlineCreate(pathCfg, pathDB)
defer testAllOnlineFree(pushNode, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down Expand Up @@ -76,11 +81,11 @@ func testDelOnline(t *testing.T, client hls_client.IClient, addr string) {
}
}

func testAllOnlineCreate() anonymity.INode {
os.RemoveAll(tcPathConfig + "_push2")
os.RemoveAll(tcPathDB + "_push2")
func testAllOnlineCreate(pathCfg, pathDB string) anonymity.INode {
os.RemoveAll(pathCfg + "_push2")
os.RemoveAll(pathDB + "_push2")

pushNode := testOnlinePushNode(tcPathConfig+"_push2", tcPathDB+"_push2")
pushNode := testOnlinePushNode(pathCfg+"_push2", pathDB+"_push2")
if pushNode == nil {
return nil
}
Expand All @@ -89,10 +94,10 @@ func testAllOnlineCreate() anonymity.INode {
return pushNode
}

func testAllOnlineFree(node anonymity.INode) {
func testAllOnlineFree(node anonymity.INode, pathCfg, pathDB string) {
defer func() {
os.RemoveAll(tcPathConfig + "_push2")
os.RemoveAll(tcPathDB + "_push2")
os.RemoveAll(pathCfg + "_push2")
os.RemoveAll(pathDB + "_push2")
}()
types.StopAll([]types.ICommand{
node,
Expand Down
27 changes: 16 additions & 11 deletions cmd/hidden_lake/service/internal/handler/network_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ import (
)

func TestHandleRequestAPI(t *testing.T) {
_, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[9])
defer testAllFree(node, srv)
t.Parallel()

pushNode, pushSrv := testAllPushCreate()
defer testAllPushFree(pushNode, pushSrv)
pathCfg := fmt.Sprintf(tcPathConfigTemplate, 7)
pathDB := fmt.Sprintf(tcPathDBTemplate, 7)

_, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[9])
defer testAllFree(node, srv, pathCfg, pathDB)

pushNode, pushSrv := testAllPushCreate(pathCfg, pathDB)
defer testAllPushFree(pushNode, pushSrv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down Expand Up @@ -76,11 +81,11 @@ func testFetch(t *testing.T, client hls_client.IClient) {
}
}

func testAllPushCreate() (anonymity.INode, *http.Server) {
os.RemoveAll(tcPathConfig + "_push1")
os.RemoveAll(tcPathDB + "_push1")
func testAllPushCreate(pathCfg, pathDB string) (anonymity.INode, *http.Server) {
os.RemoveAll(pathCfg + "_push1")
os.RemoveAll(pathDB + "_push1")

pushNode := testNewPushNode(tcPathConfig+"_push1", tcPathDB+"_push1")
pushNode := testNewPushNode(pathCfg+"_push1", pathDB+"_push1")
if pushNode == nil {
return nil, nil
}
Expand All @@ -90,10 +95,10 @@ func testAllPushCreate() (anonymity.INode, *http.Server) {
return pushNode, pushSrv
}

func testAllPushFree(node anonymity.INode, srv *http.Server) {
func testAllPushFree(node anonymity.INode, srv *http.Server, pathCfg, pathDB string) {
defer func() {
os.RemoveAll(tcPathConfig + "_push1")
os.RemoveAll(tcPathDB + "_push1")
os.RemoveAll(pathCfg + "_push1")
os.RemoveAll(pathDB + "_push1")
}()
types.StopAll([]types.ICommand{
node,
Expand Down
9 changes: 7 additions & 2 deletions cmd/hidden_lake/service/internal/handler/node_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import (
)

func TestHandlePubKeyAPI(t *testing.T) {
_, node, srv := testAllCreate(tcPathConfig, tcPathDB, testutils.TgAddrs[8])
defer testAllFree(node, srv)
t.Parallel()

pathCfg := fmt.Sprintf(tcPathConfigTemplate, 8)
pathDB := fmt.Sprintf(tcPathDBTemplate, 8)

_, node, srv := testAllCreate(pathCfg, pathDB, testutils.TgAddrs[8])
defer testAllFree(node, srv, pathCfg, pathDB)

client := hls_client.NewClient(
hls_client.NewBuilder(),
Expand Down
Loading

0 comments on commit bd56220

Please sign in to comment.