Skip to content

Commit

Permalink
ci: add Go-related workflows and steps
Browse files Browse the repository at this point in the history
  • Loading branch information
torives committed Oct 2, 2023
1 parent 7bd9d75 commit 979c8ea
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ jobs:
path: offchain/schema.graphql
if-no-files-found: error

- name: Install Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'

- name: Run Go tests
working-directory: ${{ github.workspace }}
run: go test ./internal/pkg/services

build_docker:
runs-on: ubuntu-22.04
needs:
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/go-code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Assess Go code quality

on:
push:
paths:
- ".github/workflows/go-code-quality.yml"
- "cmd/**"
- "internal/**"

jobs:
assess-go-code-quality:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-go@v4
with:
go-version-file: "go.mod"

- name: Check code format
run: |
output=$(gofmt -s -l .)
if [ -n "$output" ]; then
echo "$output"
exit 1;
fi
- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
2 changes: 1 addition & 1 deletion .github/workflows/rust-code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Assess Rust code quality
on:
push:
paths:
- '.github/workflows/code-quality.yml'
- '.github/workflows/rust-code-quality.yml'
- 'offchain/**'

jobs:
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/services/graphql-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (g GraphQLService) Start(ctx context.Context) error {
go func() {
<-ctx.Done()
fmt.Printf("%v: %v\n", g.String(), ctx.Err())
cmd.Process.Signal(syscall.SIGTERM)
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
msg := "%v: failed to send SIGTERM to %v\n"
fmt.Printf(msg, g.String(), binaryName)
}
}()

err := cmd.Wait()
Expand Down
49 changes: 49 additions & 0 deletions internal/pkg/services/graphql-service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package services

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"time"
)

func TestGraphQLService(t *testing.T) {
t.Run("it stops when the context is cancelled", func(t *testing.T) {
service := GraphQLService{}
setupEnvVars()
ctx, cancel := context.WithCancel(context.Background())
exit := make(chan error)

go func() {
<-time.After(100 * time.Millisecond)
if err := service.Start(ctx); err != nil {
exit <- err
}
}()

<-time.After(200 * time.Millisecond)
cancel()

err := <-exit
exitError, ok := err.(*exec.ExitError)
if !ok || !assertExitErrorWasCausedBy(exitError, syscall.SIGTERM) {
fmt.Printf("service exited for the wrong reason: %v", err)
t.FailNow()
}
})
}

func setupEnvVars() {
abs, _ := filepath.Abs("../../../offchain/target/debug")
os.Setenv("PATH", abs)
os.Setenv("POSTGRES_PASSWORD", "pw")
}

func assertExitErrorWasCausedBy(err *exec.ExitError, signal syscall.Signal) bool {
status := err.Sys().(syscall.WaitStatus)
return status.Signal() == signal
}

0 comments on commit 979c8ea

Please sign in to comment.