Skip to content

Fix Flaky Tests #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 25 additions & 58 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
@@ -1,85 +1,52 @@
# Use the latest stable version of Semaphore 2.0 YML syntax:
version: v1.0

# Name your pipeline. In case you connect multiple pipelines with promotions,
# the name will help you differentiate between, for example, a CI build phase
# and delivery phases.
name: Semaphore Go CI example

# An agent defines the environment in which your code runs.
# It is a combination of one of available machine types and operating
# system images.
# See https://docs.semaphoreci.com/article/20-machine-types
# and https://docs.semaphoreci.com/article/32-ubuntu-1804-image
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004

# Blocks are the heart of a pipeline and are executed sequentially.
# Each block has a task that defines one or more jobs. Jobs define the
# commands to execute.
# See https://docs.semaphoreci.com/article/62-concepts
blocks:
- name: Build project
task:
jobs:
- name: go get & build
commands:
# Download code from Git repository. This step is mandatory if the
# job is to work with your code.
- checkout

# Set version of Go:
- sem-version go 1.19
- go get
- go build -o ./bin/main

# Store the binary in cache to reuse in further blocks.
# More about caching: https://docs.semaphoreci.com/article/54-toolbox-reference#cache
- cache store $(checksum main.go) bin

- name: go get & build
commands:
- checkout
- sem-version go 1.19
- go get
- go build -o ./bin/main
- cache store $(checksum main.go) bin
secrets:
- name: test-home
- name: Check code style
task:
jobs:
- name: gofmt
commands:
# Each job on Semaphore starts from a clean environment, so we
# repeat the required project setup.
- checkout
- sem-version go 1.19
- gofmt main.go | diff --ignore-tab-expansion main.go -

- name: gofmt
commands:
- checkout
- sem-version go 1.19
- gofmt main.go | diff --ignore-tab-expansion main.go -
- name: Run tests
task:
# This block runs two jobs in parallel and they both share common
# setup steps. We can group them in a prologue.
# See https://docs.semaphoreci.com/article/50-pipeline-yaml#prologue
prologue:
commands:
- checkout
- sem-version go 1.19
jobs:
- name: go test
commands:
# Start Postgres database service.
# See https://docs.semaphoreci.com/ci-cd-environment/sem-service-managing-databases-and-services-on-linux/
- sem-service start postgres
- psql -p 5432 -h localhost -U postgres -c "CREATE DATABASE s2"
- go install gotest.tools/gotestsum@latest
- gotestsum --junitfile junit.xml ./...

- name: Test web server
commands:
# Restore compiled binary which we created in the first block:
- cache restore $(checksum main.go)
- ./bin/main 8001 &
- curl --silent localhost:8001/time | grep "The current time is"
- name: go test
commands:
- sem-service start postgres
- psql -p 5432 -h localhost -U postgres -c "CREATE DATABASE s2"
- go install gotest.tools/gotestsum@latest
- gotestsum --junitfile junit.xml ./...
- name: Test web server
commands:
- cache restore $(checksum main.go)
- ./bin/main 8001 &
- 'curl --silent localhost:8001/time | grep "The current time is"'
epilogue:
always:
commands:
- test-results publish junit.xml

after_pipeline:
task:
jobs:
Expand Down
26 changes: 26 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"net/http/httptest"
"testing"
"math/rand"
"time"
)

func createTable() {
Expand Down Expand Up @@ -145,3 +147,27 @@ func Test_record(t *testing.T) {
}
dropTable()
}

func TestAPI(t *testing.T) {
rand.Seed(time.Now().UnixNano())
IsFlaky := rand.Intn(2) == 0
if !IsFlaky {
t.Error("API Test failed. Reason: timeout")
}
}

func TestFeatureA(t *testing.T) {
rand.Seed(time.Now().UnixNano())
IsFlaky := rand.Intn(2) == 0
if !IsFlaky {
t.Error("FeatureA failed. Reason: resource not ready")
}
}

func TestFeatureB(t *testing.T) {
rand.Seed(time.Now().UnixNano())
IsFlaky := rand.Intn(2) == 0
if !IsFlaky {
t.Error("FeatureB failed. Reason: connection failed")
}
}