Skip to content

Commit

Permalink
chore: 更新ci相关版本,并重构memroy相关测试(#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
longyue0521 authored Oct 17, 2023
2 parents c49cfb9 + 0634a5b commit cf53b00
Show file tree
Hide file tree
Showing 22 changed files with 472 additions and 59 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ">=1.18.0"
go-version: ">=1.21.0"

- name: Install goimports
run: go install golang.org/x/tools/cmd/goimports@latest
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: '>=1.21.0'

- name: Build
run: go build -v ./...
Expand All @@ -37,4 +37,4 @@ jobs:
run: go test -race -coverprofile=cover.out -v ./...

- name: Post Coverage
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
9 changes: 5 additions & 4 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- uses: actions/checkout@v3
go-version: '>=1.21.0'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.52.2
version: v1.54

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: '>=1.21.0'

- name: Test
run: sudo sh ./script/integrate_test.sh
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/stale@v4
- uses: actions/stale@v8
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue is inactive for a long time.'
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
# vendor/

# Go workspace file
go.work
.idea
.vscode/
**/.DS_Store
test.db
65 changes: 65 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
APP_PATH:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
SCRIPTS_PATH:=$(APP_PATH)/scripts

.PHONY: setup
setup:
@echo "初始化开发环境......"
@find "$(SCRIPTS_PATH)" -type f -name '*.sh' -exec chmod +x {} \;
@bash $(SCRIPTS_PATH)/setup/setup.sh
@$(MAKE) tidy

# 依赖清理
.PHONY: tidy
tidy:
@go mod tidy

# 代码风格
.PHONY: fmt
fmt:
@goimports -l -w $$(find . -type f -name '*.go' -not -path "./.idea/*" -not -path "./cmd/monolithic/ioc/wire_gen.go")
@gofumpt -l -w $$(find . -type f -name '*.go' -not -path "./.idea/*" -not -path "./cmd/monolithic/ioc/wire_gen.go")

# 静态扫描
.PHONY: lint
lint:
@golangci-lint run -c $(SCRIPTS_PATH)/lint/.golangci.yaml ./...

# 单元测试
.PHONY: ut
ut:
@go test -race -cover -coverprofile=unit.out -failfast -shuffle=on ./...

# 集成测试
.PHONY: it
it:
@make dev_3rd_down
@make dev_3rd_up
@go test -tags=integration -race -cover -coverprofile=integration.out -failfast -shuffle=on ./...
@make dev_3rd_down

# 端到端测试
.PHONY: e2e
e2e:
@make dev_3rd_down
@make dev_3rd_up
@go test -tags=e2e -race -cover -coverprofile=e2e.out -failfast -shuffle=on ./...
@make dev_3rd_down

# 启动本地研发 docker 依赖
.PHONY: dev_3rd_up
dev_3rd_up:
@docker compose -f ./scripts/deploy/dev-compose.yaml up -d

.PHONY: dev_3rd_down
dev_3rd_down:
@docker compose -f ./scripts/deploy/dev-compose.yaml down -v

.PHONY: check
check:
@echo "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 检查阶段 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
@echo "整理项目依赖中......"
@$(MAKE) tidy
@echo "代码风格检查中......"
@$(MAKE) fmt
@echo "代码静态扫描中......"
@$(MAKE) lint
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ecodeclub/mq-api
go 1.21.0

require (
github.com/ecodeclub/ekit v0.0.7
github.com/ecodeclub/ekit v0.0.8
github.com/stretchr/testify v1.8.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ecodeclub/ekit v0.0.7 h1:6e3p4FQToZPvnsHSKRCTcDo+vYcr8yChV78NeCOcEp0=
github.com/ecodeclub/ekit v0.0.7/go.mod h1:q/cMifDy7CygsCz9NZNgFS6lksEo5tWxsb7RjMoZv00=
github.com/ecodeclub/ekit v0.0.8 h1:861Aot0GvD5ueREEYDVYc1oIhDuFyg6MTxIyiOa4Pvw=
github.com/ecodeclub/ekit v0.0.8/go.mod h1:OqTojKeKFTxeeAAUwNIPKu339SRkX6KAuoK/8A5BCEs=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
Expand Down
15 changes: 6 additions & 9 deletions memory/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package memory

import (
"context"
"sync"

"github.com/ecodeclub/ekit/syncx"
"github.com/ecodeclub/mq-api"
"sync"
)

type Topic struct {
Expand All @@ -30,12 +31,6 @@ type Topic struct {

type topicOption func(topic *Topic)

func WithProducerChannelSize(size int) topicOption {
return func(topic *Topic) {
topic.produceChan = make(chan *mq.Message, size)
}
}

func (t *Topic) NewConsumer(size int) mq.Consumer {
consumerCh := make(chan *mq.Message, size)
t.lock.Lock()
Expand Down Expand Up @@ -100,7 +95,8 @@ func NewMq() mq.MQ {

func (m *Mq) Consumer(topic string) mq.Consumer {
tp, _ := m.topics.LoadOrStore(topic, NewTopic(topic))
return tp.NewConsumer(10)
const size = 10
return tp.NewConsumer(size)
}

func (m *Mq) Producer(topic string) mq.Producer {
Expand All @@ -109,9 +105,10 @@ func (m *Mq) Producer(topic string) mq.Producer {
}

func NewTopic(name string, opts ...topicOption) *Topic {
const i = 1000
t := &Topic{
Name: name,
produceChan: make(chan *mq.Message, 1000),
produceChan: make(chan *mq.Message, i),
}
for _, opt := range opts {
opt(t)
Expand Down
11 changes: 6 additions & 5 deletions memory/mq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package memory

import (
"context"
"github.com/ecodeclub/mq-api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"sync"
"testing"
"time"

"github.com/ecodeclub/mq-api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type MemoryMqTestSuite struct {
Expand All @@ -34,7 +35,7 @@ func (m *MemoryMqTestSuite) SetupSuite() {
}

func (m *MemoryMqTestSuite) TestMq() {
testcases := []struct {
testcases := []*struct {
name string
consumers []mq.Consumer
producers []mq.Producer
Expand Down Expand Up @@ -101,11 +102,11 @@ func (m *MemoryMqTestSuite) TestMq() {
for _, a := range ansList {
assert.Equal(t, tc.wantValue, a)
}

})
}
}

func TestMq(t *testing.T) {
t.Parallel()
suite.Run(t, &MemoryMqTestSuite{})
}
7 changes: 7 additions & 0 deletions scripts/cicd/3rd-dependency-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

output=$(make tidy)
if [ -n "$output" ]; then
echo "错误: 请在本地运行'make tidy'命令,确认无误后再提交." >&2
exit 1
fi
7 changes: 7 additions & 0 deletions scripts/cicd/code-static-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

output=$(make lint)
if [ -n "$output" ]; then
echo "错误: 请在本地运行'make lint'命令,确认无误后再提交." >&2
exit 1
fi
7 changes: 7 additions & 0 deletions scripts/cicd/code-style-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

output=$(make fmt)
if [ -n "$output" ]; then
echo >&2 "错误: 请在本地运行'make fmt'命令,确认无误后再提交."
exit 1
fi
8 changes: 8 additions & 0 deletions scripts/cicd/end-to-end-testing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

echo "运行端到端测试中......"
make e2e
if [ $? -ne 0 ]; then
echo "错误: 请在本地运行'make e2e'命令,确认测试全部通过后再提交." >&2
exit 1
fi
8 changes: 8 additions & 0 deletions scripts/cicd/integration-testing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

echo "运行集成测试中......"
make it
if [ $? -ne 0 ]; then
echo "错误: 请在本地运行'make it'命令,确认测试全部通过后再提交." >&2
exit 1
fi
8 changes: 8 additions & 0 deletions scripts/cicd/unit-testing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

echo "运行单元测试中......"
make ut
if [ $? -ne 0 ]; then
echo "错误: 请在本地运行'make ut'命令,确认测试全部通过后再提交." >&2
exit 1
fi
21 changes: 21 additions & 0 deletions scripts/deploy/dev-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'
services:
zookeeper:
image: 'bitnami/zookeeper:latest'
ports:
- '2181:2181'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: 'bitnami/kafka:3.5.1'
ports:
- '9092:9092'
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true
depends_on:
- zookeeper
Loading

0 comments on commit cf53b00

Please sign in to comment.