From 6b332f7254bc19f90ed535958016b63153292f9e Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Thu, 22 Mar 2018 22:10:05 +0100 Subject: [PATCH] some little fixes + drone ci --- .drone.yml | 19 +++++++++++++++++++ README.md | 7 ++++++- file/main.go | 13 +++++-------- file/worker.go | 2 +- http/io.go | 2 +- http/main_test.go | 1 - websocket/session.go | 2 ++ worker/main.go | 22 ++++++++++++---------- worker/main_test.go | 4 ++-- 9 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..dc498a5 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,19 @@ +pipeline: + test: + image: golang:latest + commands: + - go get -d -t ./... + - go get -u github.com/mattn/goveralls + - go get -u golang.org/x/tools/cmd/cover + - ./.test-coverage + codestyle: + image: golang:latest + commands: + - go get github.com/client9/misspell/cmd/misspell + - misspell -error . + - if [ -n "$(gofmt -s -l .)" ]; then echo "Go code is not formatted, run 'gofmt -s -w .'" >&2; exit 1; fi + test-race: + image: golang:latest + commands: + - go get -d -t ./... + - go test -v -cover --race ./... diff --git a/README.md b/README.md index af11fe1..dd138dd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ -# golang-lib [![Build Status](https://travis-ci.org/genofire/golang-lib.svg?branch=master)](https://travis-ci.org/genofire/golang-lib) [![CircleCI](https://circleci.com/gh/genofire/golang-lib/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/golang-lib/tree/master) [![Coverage Status](https://coveralls.io/repos/github/genofire/golang-lib/badge.svg?branch=master)](https://coveralls.io/github/genofire/golang-lib?branch=master) [![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/golang-lib)](https://goreportcard.com/report/dev.sum7.eu/genofire/golang-lib) [![GoDoc](https://godoc.org/dev.sum7.eu/genofire/golang-lib?status.svg)](https://godoc.org/dev.sum7.eu/genofire/golang-lib) +# golang-lib +[![DroneCI](https://ci.sum7.eu/api/badges/genofire/golang-lib/status.svg?branch=master)](https://ci.sum7.eu/genofire/golang-lib) +[![Travis](https://travis-ci.org/genofire/golang-lib.svg?branch=master)](https://travis-ci.org/genofire/golang-lib) [![CircleCI](https://circleci.com/gh/genofire/golang-lib/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/golang-lib/tree/master) +[![Coverage](https://coveralls.io/repos/github/genofire/golang-lib/badge.svg?branch=master)](https://coveralls.io/github/genofire/golang-lib?branch=master) +[![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/golang-lib)](https://goreportcard.com/report/dev.sum7.eu/genofire/golang-lib) +[![GoDoc](https://godoc.org/dev.sum7.eu/genofire/golang-lib?status.svg)](https://godoc.org/dev.sum7.eu/genofire/golang-lib) some packages collected for easy and often used functions diff --git a/file/main.go b/file/main.go index a6c9c11..00e70ac 100644 --- a/file/main.go +++ b/file/main.go @@ -15,8 +15,7 @@ func ReadTOML(path string, data interface{}) error { return err } - err = toml.Unmarshal(file, data) - if err != nil { + if err := toml.Unmarshal(file, data); err != nil { return err } @@ -30,8 +29,7 @@ func ReadJSON(path string, data interface{}) error { return err } - err = json.NewDecoder(file).Decode(data) - if err != nil { + if err := json.NewDecoder(file).Decode(data); err != nil { return err } @@ -42,17 +40,16 @@ func ReadJSON(path string, data interface{}) error { func SaveJSON(outputFile string, data interface{}) error { tmpFile := outputFile + ".tmp" - f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + file, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return err } - err = json.NewEncoder(f).Encode(data) - if err != nil { + if err := json.NewEncoder(file).Encode(data); err != nil { return err } - f.Close() + file.Close() if err := os.Rename(tmpFile, outputFile); err != nil { return err } diff --git a/file/worker.go b/file/worker.go index 6f82486..d7183e6 100644 --- a/file/worker.go +++ b/file/worker.go @@ -10,6 +10,6 @@ func NewSaveJSONWorker(repeat time.Duration, path string, data interface{}) *wor saveWorker := worker.NewWorker(repeat, func() { SaveJSON(path, data) }) - go saveWorker.Start() + saveWorker.Start() return saveWorker } diff --git a/http/io.go b/http/io.go index e6eb562..b3c12a6 100644 --- a/http/io.go +++ b/http/io.go @@ -11,7 +11,7 @@ import ( // Function to read data from a http request via json format (input) func Read(r *http.Request, to interface{}) (err error) { if !strings.Contains(r.Header.Get("Content-Type"), "application/json") { - err = errors.New("no json request recieved") + err = errors.New("no json request received") return } err = json.NewDecoder(r.Body).Decode(to) diff --git a/http/main_test.go b/http/main_test.go index 91034b7..9ac355f 100644 --- a/http/main_test.go +++ b/http/main_test.go @@ -8,7 +8,6 @@ import ( ) // Function to test the logging -// Input: pointer to teh testing object func TestGetIP(t *testing.T) { assertion := assert.New(t) diff --git a/websocket/session.go b/websocket/session.go index e855e1e..7ca05bb 100644 --- a/websocket/session.go +++ b/websocket/session.go @@ -76,6 +76,8 @@ func (s *SessionManager) Remove(c *Client) (client bool, session bool) { } func (s *SessionManager) Send(id uuid.UUID, msg *Message) { + s.Lock() + defer s.Unlock() clients := s.sessionToClient[id] for _, c := range clients { c.Write(msg) diff --git a/worker/main.go b/worker/main.go index af4de9b..8b0262a 100644 --- a/worker/main.go +++ b/worker/main.go @@ -28,17 +28,19 @@ func NewWorker(every time.Duration, f func()) (w *Worker) { // (please us it as a go routine with go w.Start()) func (w *Worker) Start() { w.wg.Add(1) - ticker := time.NewTicker(w.every) - for { - select { - case <-ticker.C: - w.run() - case <-w.quit: - ticker.Stop() - w.wg.Done() - return + go func() { + defer w.wg.Done() + ticker := time.NewTicker(w.every) + for { + select { + case <-ticker.C: + w.run() + case <-w.quit: + ticker.Stop() + return + } } - } + }() } // Function to stop the Worker diff --git a/worker/main_test.go b/worker/main_test.go index f6be1e7..51a7f87 100644 --- a/worker/main_test.go +++ b/worker/main_test.go @@ -15,9 +15,9 @@ func TestWorker(t *testing.T) { runtime := 0 w := NewWorker(time.Duration(5)*time.Millisecond, func() { - runtime = runtime + 1 + runtime++ }) - go w.Start() + w.Start() time.Sleep(time.Duration(18) * time.Millisecond) w.Close() time.Sleep(time.Duration(18) * time.Millisecond)