Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
concurrent test error
ignore linter
  • Loading branch information
josejuanmontiel committed Aug 3, 2022
1 parent e09ed7a commit e3b6c36
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ linters:
- gosimple
- govet
- ineffassign
- interfacer
- lll
- megacheck
- misspell
Expand Down Expand Up @@ -102,6 +101,9 @@ linters:
- wsl
- godot

- interfacer


issues:
exclude:

Expand Down
11 changes: 7 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/pprof"
"runtime"
Expand All @@ -27,6 +26,9 @@ const (

contentType = "Content-Type"
jsonContentType = "application/json;charset=UTF-8"

MAX_BODY_SIZE = 1048576
READ_HEADER_TIMEOUT = 60
)

type KalaStatsResponse struct {
Expand Down Expand Up @@ -110,7 +112,7 @@ type AddJobResponse struct {
func unmarshalNewJob(r *http.Request) (*job.Job, error) {
newJob := &job.Job{}

body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
body, err := io.ReadAll(io.LimitReader(r.Body, MAX_BODY_SIZE))
if err != nil {
log.Errorf("Error occurred when reading r.Body: %s", err)
return nil, err
Expand Down Expand Up @@ -354,7 +356,8 @@ func MakeServer(listenAddr string, cache job.JobCache, defaultOwner string, prof
n.UseHandler(r)

return &http.Server{
Addr: listenAddr,
Handler: n,
ReadHeaderTimeout: READ_HEADER_TIMEOUT,
Addr: listenAddr,
Handler: n,
}
}
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -210,7 +210,7 @@ func (a *ApiTestSuite) TestGetJobSuccess() {
a.NoError(err)

var jobResp JobResponse
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
a.NoError(err)
resp.Body.Close()
err = json.Unmarshal(body, &jobResp)
Expand All @@ -236,7 +236,7 @@ func (a *ApiTestSuite) TestHandleListJobStatsRequest() {
a.NoError(err)

var jobStatsResp ListJobStatsResponse
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
a.NoError(err)
resp.Body.Close()
err = json.Unmarshal(body, &jobStatsResp)
Expand Down Expand Up @@ -428,7 +428,7 @@ func setupTestReq(t assert.TestingT, method, path string, data []byte) (*httptes
}

func unmarshallRequestBody(t assert.TestingT, resp *http.Response, obj interface{}) {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
resp.Body.Close()
err = json.Unmarshal(body, obj)
Expand Down
4 changes: 1 addition & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type KalaClient struct {
// Example:
// c := New("http://127.0.0.1:8000")
func New(apiEndpoint string) *KalaClient {
if strings.HasSuffix(apiEndpoint, "/") {
apiEndpoint = apiEndpoint[:len(apiEndpoint)-1]
}
apiEndpoint = strings.TrimSuffix(apiEndpoint, "/")
apiUrlPrefix := api.ApiUrlPrefix[:len(api.ApiUrlPrefix)-1]

return &KalaClient{
Expand Down
4 changes: 2 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"strings"
"time"

Expand Down Expand Up @@ -84,7 +84,7 @@ var serveCmd = &cobra.Command{
if viper.GetString("jobdb-tls-capath") != "" {
// https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(viper.GetString("jobdb-tls-capath"))
pem, err := os.ReadFile(viper.GetString("jobdb-tls-capath"))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions job/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *MemoryJobCache) Start(persistWaitTime time.Duration) {
}

// Process-level defer for shutting down the db.
ch := make(chan os.Signal)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
s := <-ch
Expand Down Expand Up @@ -264,7 +264,7 @@ func (c *LockFreeJobCache) Start(persistWaitTime time.Duration, jobstatTtl time.
}

// Process-level defer for shutting down the db.
ch := make(chan os.Signal)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
s := <-ch
Expand Down
4 changes: 3 additions & 1 deletion job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
log "github.com/sirupsen/logrus"
)

const BASE_10 = 10

var (
RFC3339WithoutTimezone = "2006-01-02T15:04:05"

Expand Down Expand Up @@ -275,7 +277,7 @@ func (j *Job) InitDelayDuration(checkTime bool) error {
// Repeat forever
j.timesToRepeat = -1
} else {
j.timesToRepeat, err = strconv.ParseInt(strings.Split(splitTime[0], "R")[1], 10, 0)
j.timesToRepeat, err = strconv.ParseInt(strings.Split(splitTime[0], "R")[1], BASE_10, 0)
if err != nil {
log.Errorf("Error converting timesToRepeat to an int: %s", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ func TestDeletedFromApiJobsStillRunning(t *testing.T) {
fiveSecondsFromNow := time.Now().Add(5 * time.Second)

mockJobTBD := GetMockRecurringJobWithSchedule(fiveSecondsFromNow, "PT1S")
mockJobTBD.Name = "mock_job_to_be_deleted"
mockJobTBD.Name = "mock_job_to_be_deleted_from_api"
mockJobTBD.Id = "0"
mockJobTBD.ranChan = make(chan struct{})
mockJobTBD.Init(cache)
Expand Down
8 changes: 5 additions & 3 deletions job/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"os/exec"
"strings"
Expand All @@ -16,6 +16,8 @@ import (
log "github.com/sirupsen/logrus"
)

const HTTP_CODE_OK = 200

type JobRunner struct {
job *Job
meta Metadata
Expand Down Expand Up @@ -161,7 +163,7 @@ func (j *JobRunner) RemoteRun() (string, error) {
return "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -276,7 +278,7 @@ func (j *JobRunner) collectStats(success bool) {
func (j *JobRunner) checkExpected(statusCode int) bool {
// If no expected response codes passed, add 200 status code as expected
if len(j.job.RemoteProperties.ExpectedResponseCodes) == 0 {
j.job.RemoteProperties.ExpectedResponseCodes = append(j.job.RemoteProperties.ExpectedResponseCodes, 200)
j.job.RemoteProperties.ExpectedResponseCodes = append(j.job.RemoteProperties.ExpectedResponseCodes, HTTP_CODE_OK)
}
for _, expected := range j.job.RemoteProperties.ExpectedResponseCodes {
if expected == statusCode {
Expand Down
4 changes: 2 additions & 2 deletions job/runner_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package job

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestTemplatize(t *testing.T) {
t.Run("body", func(t *testing.T) {

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
b, _ := io.ReadAll(r.Body)
w.Write(b)
w.WriteHeader(200)
}))
Expand Down
2 changes: 1 addition & 1 deletion job/storage/boltdb/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func GetBoltDB(path string) *BoltJobDB {
path += "/"
}
path += "jobdb.db"
var perms os.FileMode = 0600
var perms os.FileMode = 0o0600
database, err := bolt.Open(path, perms, &bolt.Options{Timeout: time.Second * 10}) //nolint:gomnd
if err != nil {
log.Fatal(err)
Expand Down
14 changes: 6 additions & 8 deletions job/storage/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
log "github.com/sirupsen/logrus"
)

var (
TableName = "jobs"
)
const TABLE_NAME = "jobs"

type DB struct {
conn *sql.DB
Expand All @@ -27,15 +25,15 @@ func New(dsn string) *DB {
log.Fatal(err)
}
// passive attempt to create table
_, _ = connection.Exec(fmt.Sprintf(`create table %s (job jsonb);`, TableName))
_, _ = connection.Exec(fmt.Sprintf(`create table %s (job jsonb);`, TABLE_NAME))
return &DB{
conn: connection,
}
}

// GetAll returns all persisted Jobs.
func (d DB) GetAll() ([]*job.Job, error) {
query := fmt.Sprintf(`select coalesce(json_agg(j.job), '[]'::json) from (select * from %[1]s) as j;`, TableName)
query := fmt.Sprintf(`select coalesce(json_agg(j.job), '[]'::json) from (select * from %[1]s) as j;`, TABLE_NAME)
var r sql.NullString
err := d.conn.QueryRow(query).Scan(&r)
if err != nil && err != sql.ErrNoRows {
Expand All @@ -52,7 +50,7 @@ func (d DB) GetAll() ([]*job.Job, error) {
// Get returns a persisted Job.
func (d DB) Get(id string) (*job.Job, error) {
template := `select to_jsonb(j.job) from (select * from %[1]s where job -> 'id' = $1) as j;`
query := fmt.Sprintf(template, TableName)
query := fmt.Sprintf(template, TABLE_NAME)
var r sql.NullString
err := d.conn.QueryRow(query, id).Scan(&r)
if err != nil {
Expand All @@ -67,15 +65,15 @@ func (d DB) Get(id string) (*job.Job, error) {

// Delete deletes a persisted Job.
func (d DB) Delete(id string) error {
query := fmt.Sprintf(`delete from %v where job -> = 'id' = $1;`, TableName)
query := fmt.Sprintf(`delete from %v where job -> = 'id' = $1;`, TABLE_NAME)
_, err := d.conn.Exec(query, id)
return err
}

// Save persists a Job.
func (d DB) Save(j *job.Job) error {
template := `insert into %[1]s (job) values($1);`
query := fmt.Sprintf(template, TableName)
query := fmt.Sprintf(template, TABLE_NAME)
r, err := json.Marshal(j)
if err != nil {
return err
Expand Down

0 comments on commit e3b6c36

Please sign in to comment.