From 790051da32c66de1a2c2b093d7f78de32ba7e03f Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Wed, 14 Feb 2024 01:52:25 +0800 Subject: [PATCH] ci: test on push and pr --- .github/workflows/test.yml | 45 ++++++++++++++++++++++++++++++++++++++ scripts/seed.go | 2 +- scripts/stress.js | 2 +- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..bfb925b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,45 @@ +name: Test +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.21.7' + - run: go test -v + - run: go build . + + stress-test: + runs-on: ubuntu-latest + container: ghcr.io/catthehacker/ubuntu:act-latest + services: + mongodb: + image: mongo + ports: + - 27017:27017 + env: + MONGO_INITDB_ROOT_USERNAME: user + MONGO_INITDB_ROOT_PASSWORD: password + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.21.7' + - name: Install k6 + run: curl https://github.com/grafana/k6/releases/download/v0.49.0/k6-v0.49.0-linux-amd64.tar.gz -L | tar xvz --strip-components 1 + - run: go build . + - run: ./ad-service & sleep 5 && go run scripts/seed.go 2500 && ./k6 run -u 100 scripts/stress.js + env: + GIN_MODE: release + MONGODB_URI: mongodb://user:password@mongo:27017 diff --git a/scripts/seed.go b/scripts/seed.go index 5dfa315..34d1d4e 100644 --- a/scripts/seed.go +++ b/scripts/seed.go @@ -117,7 +117,7 @@ func sendAd(ad *AdPayload) { return } - resp, err := http.Post("http://localhost:8080/api/v1/ad", "application/json", bytes.NewBuffer(jsonData)) + resp, err := http.Post("http://127.0.0.1:8080/api/v1/ad", "application/json", bytes.NewBuffer(jsonData)) if err != nil { fmt.Println("Error sending ad to API", err) return diff --git a/scripts/stress.js b/scripts/stress.js index 8381cff..681edde 100644 --- a/scripts/stress.js +++ b/scripts/stress.js @@ -14,7 +14,7 @@ const randomPlatform = () => ["android", "ios", "web", ""][Math.floor(Math.rando const randomCountry = () => ["US", "CA", "MX", "BR", "JP", "KR", "CN", "RU", "AU", "NZ", "TW", ""][Math.floor(Math.random() * 12)]; export default function () { - let url = `http://localhost:8080/api/v1/ad?offset=${randomOffset()}&limit=${randomLimit()}`; + let url = `http://127.0.0.1:8080/api/v1/ad?offset=${randomOffset()}&limit=${randomLimit()}`; const age = randomAge(); if (age) url += `&age=${age}`;