Skip to content

Commit

Permalink
feat: add github action test
Browse files Browse the repository at this point in the history
fix: linter fix

fix: fix vet command and shadowing
  • Loading branch information
nick-bisonai committed Feb 21, 2024
1 parent fc32737 commit c1fd31a
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 22 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/node.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: "orakl node test"

on:
push:
branches-ignore:
- "master"
paths:
- "node/**"
jobs:
core-build:
runs-on: ubuntu-latest
timeout-minutes: 3

services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: orakl-test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379

steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: "1.21.5"
check-latest: true
cache-dependency-path: |
./node/go.sum
- name: Run lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
working-directory: node
skip-pkg-cache: true
skip-build-cache: true
args: --timeout=10m

- name: Run Vet
run: |
cd ./node
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
go vet ./...
go vet -vettool=$(which shadow) ./...
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz
sudo mv ./migrate /usr/bin
- name: Migrate up
run: |
cd ./node
migrate -database "postgresql://postgres:postgres@localhost:5432/orakl-test?search_path=public&sslmode=disable" -verbose -path ./migrations up
- name: Install dependencies
run: |
cd ./node
go mod tidy
- name: Install task
run: |
sudo snap install task --classic
- name: Run test
run: |
cd ./node
task local:test
env:
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/orakl-test?search_path=public"
REDIS_HOST: "localhost"
REDIS_PORT: "6379"
1 change: 1 addition & 0 deletions node/pkg/admin/tests/adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:all
package tests

import (
Expand Down
1 change: 1 addition & 0 deletions node/pkg/admin/tests/feed_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:all
package tests

import (
Expand Down
4 changes: 2 additions & 2 deletions node/pkg/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ func (a *Aggregator) LeaderJob() error {
func (a *Aggregator) HandleCustomMessage(message raft.Message) error {
switch message.Type {
case RoundSync:
a.HandleRoundSyncMessage(message)
return a.HandleRoundSyncMessage(message)
// every node runs its job when leader sends roundSync message
case PriceData:
a.HandlePriceDataMessage(message)
return a.HandlePriceDataMessage(message)
}
return nil
}
Expand Down
9 changes: 6 additions & 3 deletions node/pkg/db/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,30 @@ func loadPgsqlConnectionString() string {
}

func Query(ctx context.Context, queryString string, args map[string]any) (pgx.Rows, error) {
pool, err := GetPool(ctx)
_pool, err := GetPool(ctx)
if err != nil {
return nil, err
}
pool = _pool
return query(pool, queryString, args)
}

func QueryRow[T any](ctx context.Context, queryString string, args map[string]any) (T, error) {
var t T
pool, err := GetPool(ctx)
_pool, err := GetPool(ctx)
if err != nil {
return t, err
}
pool = _pool
return queryRow[T](pool, queryString, args)
}

func QueryRows[T any](ctx context.Context, queryString string, args map[string]any) ([]T, error) {
pool, err := GetPool(ctx)
_pool, err := GetPool(ctx)
if err != nil {
return nil, err
}
pool = _pool
return queryRows[T](pool, queryString, args)
}

Expand Down
15 changes: 9 additions & 6 deletions node/pkg/db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,40 @@ func connectRdb(ctx context.Context) (*redis.Conn, error) {
}

func Set(ctx context.Context, key string, value string, exp time.Duration) error {
rdb, err := GetRedisConn(ctx)
_rdb, err := GetRedisConn(ctx)
if err != nil {
return err
}
rdb = _rdb
return setRedis(ctx, rdb, key, value, exp)
}

func Get(ctx context.Context, key string) (string, error) {
rdb, err := GetRedisConn(ctx)
_rdb, err := GetRedisConn(ctx)
if err != nil {
return "", err
}
rdb = _rdb
return getRedis(ctx, rdb, key)
}

func Del(ctx context.Context, key string) error {
rdb, err := GetRedisConn(ctx)
_rdb, err := GetRedisConn(ctx)
if err != nil {
return err
}
rdb = _rdb
return rdb.Del(ctx, key).Err()
}

func connectToRedis(ctx context.Context, connectionInfo RedisConnectionInfo) (*redis.Conn, error) {
rdb := redis.NewClient(&redis.Options{
_rdb := redis.NewClient(&redis.Options{
Addr: connectionInfo.Host + ":" + connectionInfo.Port}).Conn()
_, rdbErr := rdb.Ping(ctx).Result()
_, rdbErr := _rdb.Ping(ctx).Result()
if rdbErr != nil {
return nil, rdbErr
}
return rdb, nil
return _rdb, nil
}

func loadRedisConnectionString() (RedisConnectionInfo, error) {
Expand Down
42 changes: 31 additions & 11 deletions node/pkg/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (r *Raft) Run(ctx context.Context, node Node) {
r.startElectionTimer()
var jobTicker <-chan time.Time
if node.GetJobTimeout() != nil {
node.SetJobTicker(node.GetJobTimeout())
err := node.SetJobTicker(node.GetJobTimeout())
if err != nil {
log.Println("failed to set job ticker")
}
jobTicker = node.GetJobTicker().C
}

Expand All @@ -54,7 +57,10 @@ func (r *Raft) Run(ctx context.Context, node Node) {
case <-r.ElectionTimer.C:
r.startElection()
case <-jobTicker:
node.Job()
err := node.Job()
if err != nil {
log.Println("failed to execute job:", err)
}
}
}
}
Expand Down Expand Up @@ -151,7 +157,10 @@ func (r *Raft) handleRequestVote(msg Message) error {
}

if RequestVoteMessage.Term < r.GetCurrentTerm() {
r.sendReplyVote(msg.SentFrom, false)
err := r.sendReplyVote(msg.SentFrom, false)
if err != nil {
log.Println("failed to send reply vote:", err)
}
return nil
}

Expand All @@ -165,8 +174,7 @@ func (r *Raft) handleRequestVote(msg Message) error {
r.UpdateVotedFor(msg.SentFrom)
}

r.sendReplyVote(msg.SentFrom, voteGranted)
return nil
return r.sendReplyVote(msg.SentFrom, voteGranted)
}

func (r *Raft) handleReplyVote(node Node, msg Message) error {
Expand Down Expand Up @@ -248,10 +256,10 @@ func (r *Raft) sendReplyVote(to string, voteGranted bool) error {
}

func (r *Raft) sendRequestVote() error {
RequestVoteMessage := RequestVoteMessage{
requestVoteMessage := RequestVoteMessage{
Term: r.GetCurrentTerm(),
}
marshalledRequestVoteMsg, err := json.Marshal(RequestVoteMessage)
marshalledRequestVoteMsg, err := json.Marshal(requestVoteMessage)
if err != nil {
return err
}
Expand Down Expand Up @@ -279,7 +287,10 @@ func (r *Raft) StopHeartbeatTicker(node Node) {

if node.GetLeaderJobTicker() != nil {
node.GetLeaderJobTicker().Stop()
node.SetLeaderJobTicker(nil)
err := node.SetLeaderJobTicker(nil)
if err != nil {
log.Println("failed to stop leader job ticker")
}
}
if r.Resign != nil {
close(r.Resign)
Expand All @@ -296,17 +307,26 @@ func (r *Raft) becomeLeader(node Node) {

var leaderJobTicker <-chan time.Time
if node.GetLeaderJobTimeout() != nil {
node.SetLeaderJobTicker(node.GetLeaderJobTimeout())
err := node.SetLeaderJobTicker(node.GetLeaderJobTimeout())
if err != nil {
log.Println("failed to set leader job ticker")
}
leaderJobTicker = node.GetLeaderJobTicker().C
}

go func() {
for {
select {
case <-r.HeartbeatTicker.C:
r.sendHeartbeat()
err := r.sendHeartbeat()
if err != nil {
log.Println("failed to send heartbeat:", err)
}
case <-leaderJobTicker:
node.LeaderJob()
err := node.LeaderJob()
if err != nil {
log.Println("failed to execute leader job:", err)
}
case <-r.Resign:
log.Println("resigning as leader")
return
Expand Down
4 changes: 4 additions & 0 deletions node/taskfiles/taskfile.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ tasks:
dotenv: [".env"]
cmds:
- go test ./pkg/admin/tests -v
test:
cmds:
- task: test-db
- task: test-admin

0 comments on commit c1fd31a

Please sign in to comment.