Skip to content

Commit

Permalink
feat: add github workflow
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <[email protected]>
  • Loading branch information
rfyiamcool committed Sep 25, 2023
1 parent 254865a commit a4feb3e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 16 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: go-co-op
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

# Maintain Go dependencies
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
67 changes: 67 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '34 7 * * 1'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
33 changes: 33 additions & 0 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
on: [push]
name: golangci-lint
jobs:
golangci:
strategy:
matrix:
go-version:
- "1.20"
name: lint and test
runs-on: ubuntu-latest
services:
etcd:
image: bitnami/etcd:3.5.5
env:
ALLOW_NONE_AUTHENTICATION: yes
ETCD_ADVERTISE_CLIENT_URLS: http://127.0.0.1:2379
ETCDCTL_API: 3
ports:
- 2379:2379
- 2380:2380
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/[email protected]
with:
version: v1.51.2
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: test
run: make test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ lint:
@golangci-lint run

test:
@go test -race -v $(GO_FLAGS) -count=1 $(GO_PKGS)
@go test -v $(GO_FLAGS) -count=1 $(GO_PKGS)
8 changes: 6 additions & 2 deletions elector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"errors"
"fmt"
mrand "math/rand"
"os"
"sync"
"time"
Expand Down Expand Up @@ -185,7 +186,7 @@ func (e *Elector) Start(electionPath string) error {
continue
}

if !e.isLeader {
if e.IsLeader(e.ctx) != nil { // is non-leader
e.setLeader(val)
e.logger("switch to leader, the current instance is leader")
}
Expand All @@ -202,6 +203,9 @@ func (e *Elector) Start(electionPath string) error {
func getID() string {
hostname, _ := os.Hostname()
bs := make([]byte, 10)
rand.Read(bs)
_, err := rand.Read(bs)
if err != nil {
return fmt.Sprintf("%s-%d", hostname, mrand.Int63())
}
return fmt.Sprintf("%s-%x", hostname, bs)
}
31 changes: 20 additions & 11 deletions elector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ var (
func TestGocronWithElector(t *testing.T) {
el, err := NewElector(context.Background(), testConfig, WithTTL(1))
assert.Equal(t, nil, err)
go el.Start(testElectionPath + "gocron_one")
go func() {
err := el.Start(testElectionPath + "gocron_one")
if err != nil {
t.Error(err)
}
}()

defer el.Stop()
defer func() {
_ = el.Stop()
}()

done := make(chan struct{}, 1)
counter := 0
Expand Down Expand Up @@ -62,7 +69,9 @@ func TestGocronWithMultipleElectors(t *testing.T) {
el, err := NewElector(context.Background(), testConfig, WithTTL(1))
assert.Equal(t, nil, err)

go el.Start(testElectionPath + "gocron_multi")
go func() {
_ = el.Start(testElectionPath + "gocron_multi")
}()

s := gocron.NewScheduler(time.UTC)
s.WithDistributedElector(el)
Expand Down Expand Up @@ -92,7 +101,7 @@ func TestGocronWithMultipleElectors(t *testing.T) {
}

for i := 0; i < workers; i++ {
elections[i].Stop()
_ = elections[i].Stop()
schedulers[i].Stop()
}
}
Expand All @@ -108,9 +117,9 @@ func TestElectorSingleAcquire(t *testing.T) {
}()

time.Sleep(2 * time.Second)
assert.Equal(t, nil, el.IsLeader(nil))
assert.Equal(t, nil, el.IsLeader(context.Background()))
assert.Equal(t, el.GetLeaderID(), el.GetID())
el.Stop()
_ = el.Stop()

select {
case <-sig:
Expand All @@ -119,7 +128,7 @@ func TestElectorSingleAcquire(t *testing.T) {
}

// after elector.stop, current instance is not leader
assert.Equal(t, ErrNonLeader, el.IsLeader(nil))
assert.Equal(t, ErrNonLeader, el.IsLeader(context.Background()))
}

func TestElectorMultipleAcquire(t *testing.T) {
Expand All @@ -142,7 +151,7 @@ func TestElectorMultipleAcquire(t *testing.T) {

var leaderCounter int
for _, el := range elections {
if el.IsLeader(nil) == nil {
if el.IsLeader(context.Background()) == nil {
leaderCounter++
}
}
Expand All @@ -152,7 +161,7 @@ func TestElectorMultipleAcquire(t *testing.T) {

// stop all electors
for _, el := range elections {
el.Stop()
_ = el.Stop()
}
}

Expand Down Expand Up @@ -192,7 +201,7 @@ func TestElectorAcquireRace(t *testing.T) {
for idx, el := range elections {
last := len(elections) - 1

el.Stop()
_ = el.Stop()

time.Sleep(3 * time.Second)
if idx == last {
Expand All @@ -206,7 +215,7 @@ func TestElectorAcquireRace(t *testing.T) {
func TestElectorStop(t *testing.T) {
el, err := NewElector(context.Background(), testConfig)
assert.Equal(t, nil, err)
el.Stop()
_ = el.Stop()
err = el.Start(testElectionPath)
assert.Equal(t, err, ErrClosed)
}
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func main() {
s := gocron.NewScheduler(time.UTC)
s.WithDistributedElector(el)

s.Every("1s").Do(func() {
if el.IsLeader(context.TODO()) == nil {
_, _ = s.Every("1s").Do(func() {
if el.IsLeader(context.Background()) == nil {
fmt.Println("the current instance is leader")
} else {
fmt.Println("the current leader is", el.GetLeaderID())
Expand Down

0 comments on commit a4feb3e

Please sign in to comment.