From d5533157a3d1c2981d96fec90ca58fb1a504541a Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Thu, 23 Aug 2018 21:02:51 +0200 Subject: [PATCH] improve docs and tests --- database/main.go | 15 +++++----- file/main.go | 22 ++++---------- file/main_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ file/testfiles/ok.json | 3 ++ file/testfiles/ok.toml | 1 + file/testfiles/trash.txt | 1 + file/worker.go | 1 + file/worker_test.go | 27 +++++++++++++++++ http/io.go | 6 ++-- http/main.go | 1 + websocket/client.go | 9 ++---- websocket/server.go | 1 - websocket/server_test.go | 25 ++++++++-------- worker/main.go | 11 ++++--- 14 files changed, 135 insertions(+), 53 deletions(-) create mode 100644 file/main_test.go create mode 100644 file/testfiles/ok.json create mode 100644 file/testfiles/ok.toml create mode 100644 file/testfiles/trash.txt create mode 100644 file/worker_test.go diff --git a/database/main.go b/database/main.go index 02f08e8..1351ba9 100644 --- a/database/main.go +++ b/database/main.go @@ -1,8 +1,9 @@ -// Package that provides the functionality to open, close and use a database +// Package database provides the functionality to open, close and use a database package database import ( "github.com/jinzhu/gorm" + // load gorm defaults dialects _ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/sqlite" @@ -10,10 +11,10 @@ import ( log "github.com/sirupsen/logrus" ) -// Database connection for writing purposes +// Write Database connection for writing purposes var Write *gorm.DB -// Database connection for reading purposes +// Read Database connection for reading purposes var Read *gorm.DB // Configuration files @@ -22,7 +23,7 @@ var ( runtime []interface{} ) -// Configuration of the database connection +// Config of the database connection type Config struct { // type of the database, currently supports sqlite and postgres Type string @@ -34,7 +35,7 @@ type Config struct { Logging bool } -// Function to open a database and set the given configuration +// Open database and set the given configuration func Open(c Config) (err error) { writeLog := log.WithField("db", "write") config = &c @@ -65,7 +66,7 @@ func Open(c Config) (err error) { return } -// Function to safely close the database +// Close connnection to database safely func Close() { Write.Close() if len(config.ReadConnection) > 0 { @@ -73,7 +74,7 @@ func Close() { } } -// Function to add a model to the runtime +// AddModel to the runtime func AddModel(m interface{}) { runtime = append(runtime, m) } diff --git a/file/main.go b/file/main.go index 00e70ac..7e75867 100644 --- a/file/main.go +++ b/file/main.go @@ -8,18 +8,14 @@ import ( "github.com/BurntSushi/toml" ) -// ReadConfigFile reads a config model from path of a yml file +// ReadTOML reads a config model from path of a yml file func ReadTOML(path string, data interface{}) error { file, err := ioutil.ReadFile(path) if err != nil { return err } - if err := toml.Unmarshal(file, data); err != nil { - return err - } - - return nil + return toml.Unmarshal(file, data) } // ReadJSON reads a config model from path of a yml file @@ -29,11 +25,7 @@ func ReadJSON(path string, data interface{}) error { return err } - if err := json.NewDecoder(file).Decode(data); err != nil { - return err - } - - return nil + return json.NewDecoder(file).Decode(data) } // SaveJSON to path @@ -45,13 +37,11 @@ func SaveJSON(outputFile string, data interface{}) error { return err } - if err := json.NewEncoder(file).Encode(data); err != nil { + err = json.NewEncoder(file).Encode(data) + if err != nil { return err } file.Close() - if err := os.Rename(tmpFile, outputFile); err != nil { - return err - } - return nil + return os.Rename(tmpFile, outputFile) } diff --git a/file/main_test.go b/file/main_test.go new file mode 100644 index 0000000..d9ed60c --- /dev/null +++ b/file/main_test.go @@ -0,0 +1,65 @@ +package file + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReadTOML(t *testing.T) { + assert := assert.New(t) + + a := struct { + Text string `toml:"text"` + }{} + + err := ReadTOML("testfiles/donoexists", &a) + assert.Error(err, "could find file ^^") + + err = ReadTOML("testfiles/trash.txt", &a) + assert.Error(err, "could marshel file ^^") + + err = ReadTOML("testfiles/ok.toml", &a) + assert.NoError(err) + assert.Equal("hallo", a.Text) +} + +func TestReadJSON(t *testing.T) { + assert := assert.New(t) + + a := struct { + Text string `toml:"text"` + }{} + + err := ReadJSON("testfiles/donoexists", &a) + assert.Error(err, "could find file ^^") + + err = ReadJSON("testfiles/trash.txt", &a) + assert.Error(err, "could marshel file ^^") + + err = ReadJSON("testfiles/ok.json", &a) + assert.NoError(err) + assert.Equal("hallo", a.Text) +} + +func TestSaveJSON(t *testing.T) { + assert := assert.New(t) + + tmpfile, _ := ioutil.TempFile("/tmp", "lib-json-testfile.json") + err := SaveJSON(tmpfile.Name(), 3) + assert.NoError(err, "could not save temp") + + err = SaveJSON(tmpfile.Name(), tmpfile.Name) + assert.Error(err, "could not save func") + + err = SaveJSON("/dev/null", 4) + assert.Error(err, "could not save to /dev/null") + + var testvalue int + err = ReadJSON(tmpfile.Name(), &testvalue) + assert.NoError(err) + assert.Equal(3, testvalue) + os.Remove(tmpfile.Name()) +} diff --git a/file/testfiles/ok.json b/file/testfiles/ok.json new file mode 100644 index 0000000..c19fa9d --- /dev/null +++ b/file/testfiles/ok.json @@ -0,0 +1,3 @@ +{ + "text": "hallo" +} diff --git a/file/testfiles/ok.toml b/file/testfiles/ok.toml new file mode 100644 index 0000000..5e180b1 --- /dev/null +++ b/file/testfiles/ok.toml @@ -0,0 +1 @@ +text = "hallo" diff --git a/file/testfiles/trash.txt b/file/testfiles/trash.txt new file mode 100644 index 0000000..4ad02cc --- /dev/null +++ b/file/testfiles/trash.txt @@ -0,0 +1 @@ +wrong format example diff --git a/file/worker.go b/file/worker.go index d7183e6..9b277f1 100644 --- a/file/worker.go +++ b/file/worker.go @@ -6,6 +6,7 @@ import ( "dev.sum7.eu/genofire/golang-lib/worker" ) +// NewSaveJSONWorker Starts a worker, which save periodly data to json file func NewSaveJSONWorker(repeat time.Duration, path string, data interface{}) *worker.Worker { saveWorker := worker.NewWorker(repeat, func() { SaveJSON(path, data) diff --git a/file/worker_test.go b/file/worker_test.go new file mode 100644 index 0000000..f35a2b6 --- /dev/null +++ b/file/worker_test.go @@ -0,0 +1,27 @@ +package file + +import ( + "io/ioutil" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestSaveJSONWorker(t *testing.T) { + assert := assert.New(t) + + tmpfile, _ := ioutil.TempFile("/tmp", "lib-json-workertest.json") + + worker := NewSaveJSONWorker(100*time.Millisecond, tmpfile.Name(), 12) + assert.NotNil(worker) + + time.Sleep(300 * time.Millisecond) + + var testvalue int + err := ReadJSON(tmpfile.Name(), &testvalue) + assert.NoError(err) + assert.Equal(12, testvalue) + os.Remove(tmpfile.Name()) +} diff --git a/http/io.go b/http/io.go index b3c12a6..bf73670 100644 --- a/http/io.go +++ b/http/io.go @@ -1,4 +1,4 @@ -// Package that provides the logic of the webserver +// Package http provides the logic of the webserver package http import ( @@ -8,7 +8,7 @@ import ( "strings" ) -// Function to read data from a http request via json format (input) +// 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 received") @@ -18,7 +18,7 @@ func Read(r *http.Request, to interface{}) (err error) { return } -// Function to write data as json to a http response (output) +// Write data as json to a http response (output) func Write(w http.ResponseWriter, data interface{}) { js, err := json.Marshal(data) if err != nil { diff --git a/http/main.go b/http/main.go index 58d5503..a6fc569 100644 --- a/http/main.go +++ b/http/main.go @@ -2,6 +2,7 @@ package http import "net/http" +// GetRemoteIP of http Request func GetRemoteIP(r *http.Request) string { ip := r.Header.Get("X-Forwarded-For") if len(ip) <= 1 { diff --git a/websocket/client.go b/websocket/client.go index 078df71..4e1782b 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -7,8 +7,9 @@ import ( "github.com/gorilla/websocket" ) -const channelBufSize = 100 +const channelBufSize = 1000 +// Client of Websocket Server Connection type Client struct { id uuid.UUID server *Server @@ -18,12 +19,6 @@ type Client struct { readQuit chan bool } -func NewTestClient(out chan *Message) *Client { - return &Client{ - out: out, - } -} - func NewClient(s *Server, ws *websocket.Conn) *Client { if ws == nil { diff --git a/websocket/server.go b/websocket/server.go index aca7e25..51136ca 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -66,7 +66,6 @@ func (s *Server) DelClient(c *Client) { } } } - func (s *Server) SendAll(msg *Message) { s.clientsMutex.Lock() defer s.clientsMutex.Unlock() diff --git a/websocket/server_test.go b/websocket/server_test.go index af0f733..a1127f8 100644 --- a/websocket/server_test.go +++ b/websocket/server_test.go @@ -3,10 +3,10 @@ package websocket import ( "net/http" "net/http/httptest" + "sync" "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" ) @@ -39,14 +39,14 @@ func TestServerSendAll(t *testing.T) { srv := NewServer(nil, nil) assert.NotNil(srv) - out1 := make(chan *Message) + out1 := make(chan *Message, 2) c1 := &Client{ id: uuid.New(), out: out1, server: srv, } - out2 := make(chan *Message) + out2 := make(chan *Message, 2) c2 := &Client{ id: uuid.New(), out: out2, @@ -55,22 +55,21 @@ func TestServerSendAll(t *testing.T) { srv.AddClient(c1) srv.AddClient(c2) - go func() { + wg := sync.WaitGroup{} - msg := <-out1 + client := func(out chan *Message) { + msg := <-out assert.Equal("hi", msg.Subject) - - }() - go func() { - - msg := <-out2 - assert.Equal("hi", msg.Subject) - - }() + wg.Done() + } + wg.Add(2) + go client(out1) + go client(out2) srv.SendAll(&Message{ Subject: "hi", }) + wg.Wait() srv.DelClient(c2) srv.DelClient(c1) diff --git a/worker/main.go b/worker/main.go index 8b0262a..df1c9d6 100644 --- a/worker/main.go +++ b/worker/main.go @@ -1,4 +1,4 @@ -// Package with a lib for cronjobs to run in background +// Package worker a lib for cronjobs to run in background package worker import ( @@ -6,7 +6,7 @@ import ( "time" ) -// Struct which handles the job +// Worker Struct which handles the job type Worker struct { every time.Duration run func() @@ -14,7 +14,7 @@ type Worker struct { wg sync.WaitGroup } -// Function to create a new Worker with a timestamp, run, every and it's function +// NewWorker create a Worker with a timestamp, run, every and it's function func NewWorker(every time.Duration, f func()) (w *Worker) { w = &Worker{ every: every, @@ -24,8 +24,7 @@ func NewWorker(every time.Duration, f func()) (w *Worker) { return } -// Function to start the Worker -// (please us it as a go routine with go w.Start()) +// Start the Worker func (w *Worker) Start() { w.wg.Add(1) go func() { @@ -43,7 +42,7 @@ func (w *Worker) Start() { }() } -// Function to stop the Worker +// Close stops the Worker func (w *Worker) Close() { close(w.quit) w.wg.Wait()