Skip to content

Commit

Permalink
improve docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Aug 23, 2018
1 parent 5fd5733 commit d553315
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 53 deletions.
15 changes: 8 additions & 7 deletions database/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// 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"

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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -65,15 +66,15 @@ 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 {
Read.Close()
}
}

// Function to add a model to the runtime
// AddModel to the runtime
func AddModel(m interface{}) {
runtime = append(runtime, m)
}
22 changes: 6 additions & 16 deletions file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
65 changes: 65 additions & 0 deletions file/main_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
3 changes: 3 additions & 0 deletions file/testfiles/ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"text": "hallo"
}
1 change: 1 addition & 0 deletions file/testfiles/ok.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text = "hallo"
1 change: 1 addition & 0 deletions file/testfiles/trash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wrong format example
1 change: 1 addition & 0 deletions file/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions file/worker_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
6 changes: 3 additions & 3 deletions http/io.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package that provides the logic of the webserver
// Package http provides the logic of the webserver
package http

import (
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (s *Server) DelClient(c *Client) {
}
}
}

func (s *Server) SendAll(msg *Message) {
s.clientsMutex.Lock()
defer s.clientsMutex.Unlock()
Expand Down
25 changes: 12 additions & 13 deletions websocket/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package websocket
import (
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/google/uuid"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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,
Expand All @@ -55,22 +55,21 @@ func TestServerSendAll(t *testing.T) {
srv.AddClient(c1)
srv.AddClient(c2)

go func() {

msg := <-out1
assert.Equal("hi", msg.Subject)

}()
go func() {
wg := sync.WaitGroup{}

msg := <-out2
client := func(out chan *Message) {
msg := <-out
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)
Expand Down
11 changes: 5 additions & 6 deletions worker/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Package with a lib for cronjobs to run in background
// Package worker a lib for cronjobs to run in background
package worker

import (
"sync"
"time"
)

// Struct which handles the job
// Worker Struct which handles the job
type Worker struct {
every time.Duration
run func()
quit chan 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,
Expand All @@ -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() {
Expand All @@ -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()
Expand Down

0 comments on commit d553315

Please sign in to comment.