Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Improving code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rehabaam committed Nov 12, 2023
1 parent 2ba8632 commit 2a47e16
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 84 deletions.
2 changes: 1 addition & 1 deletion TDWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
// startServer func starts an HTTP server that exposes RESTful APIs
func startServer() error {
logger.AppLogger(labels.Info, labels.ServerStarting, 0)
err := commands.StartServer()
err := commands.NewServer().Start()
if err != nil {
// Print the error occured while starting the server.
fmt.Printf("%+v\n", err.Error())
Expand Down
13 changes: 0 additions & 13 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ var (
// ZLevel is global logger
ZLevel *zap.AtomicLevel

// timeFormat is custom Time format
customTimeFormat string

// onceInit guarantee initialize logger only once
onceInit sync.Once

Expand Down Expand Up @@ -86,12 +83,9 @@ func Init(lvl string, timeFormat string, msName string) error {
consoleErrors := zapcore.Lock(os.Stderr)

// Configure console output.
var useCustomTimeFormat bool
ecfg := zap.NewProductionEncoderConfig()
if len(timeFormat) > 0 {
customTimeFormat = timeFormat
ecfg.EncodeTime = customTimeEncoder
useCustomTimeFormat = true
}
consoleEncoder := zapcore.NewJSONEncoder(ecfg)

Expand All @@ -105,11 +99,6 @@ func Init(lvl string, timeFormat string, msName string) error {
// From a zapcore.Core, it's easy to construct a Logger.
Log = zap.New(core)
zap.RedirectStdLog(Log)

// If timeformat is not provided, log a warning on console to use a zap default or custom one
if !useCustomTimeFormat {
Log.Warn("time format for logger is not provided - use zap default")
}
})

return err
Expand Down Expand Up @@ -141,8 +130,6 @@ func AppLogger(level string, description string, time int64, items ...string) {
Log.Debug(description, fields...)
case "error":
Log.Error(description, fields...)
case "fatal":
Log.Fatal(description, fields...)
case "warn":
Log.Warn(description, fields...)
}
Expand Down
18 changes: 18 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ func TestAppLogger(t *testing.T) {
items: []string{"description | error"},
},
},
{
name: "givenWarnArguments_returnNothing_success",
args: args{
level: "warn",
description: "description",
time: 1593332975979,
items: []string{"description | warn"},
},
},
{
name: "givenPipeArguments_returnNothing_Error",
args: args{
level: "debug",
description: "description",
time: 1593332975979,
items: []string{"description fatal"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
50 changes: 16 additions & 34 deletions services/commands.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package commands

import (
"context"
"net/http"
"os"
"os/signal"
"time"

"github.com/gorilla/mux"
)

var (
server *http.Server
)
type Server struct {
*http.Server
}

// getPartners func for getting TriDubai annually partners
func getPartners(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -44,8 +41,8 @@ func serveImage(w http.ResponseWriter, r *http.Request) {
_, _ = getImage(w, r)
}

// RunServer func for running HTTP server
func StartServer() error {
// NewServer func for HTTP server
func NewServer() Server {

r := mux.NewRouter()
api := r.PathPrefix("/api/v1").Subrouter()
Expand All @@ -58,33 +55,18 @@ func StartServer() error {

loadFileToMemory()

server = &http.Server{
Addr: ":8080",
ReadHeaderTimeout: 5 * time.Second,
Handler: r,
return Server{
&http.Server{
Addr: ":8080",
ReadHeaderTimeout: 5 * time.Second,
Handler: r,
},
}

ctx := context.Background()
StopServer(ctx, server)

return server.ListenAndServe()
}

// Graceful shutdown for HTTP server if Interrupt signal captured from OS
func StopServer(ctx context.Context, srv *http.Server) {

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
select {
case <-c:
break
case <-ctx.Done():
break
}

_, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
}()
func (s Server) Start() error {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
}
44 changes: 8 additions & 36 deletions services/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,42 +229,14 @@ func Test_serveImage(t *testing.T) {
}
}

func TestStopServer(t *testing.T) {
type args struct {
ctx context.Context
srv *http.Server
}
tests := []struct {
name string
args args
}{
{
name: "StopServer_Success",
args: args{
ctx: context.Background(),
srv: &http.Server{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
StopServer(tt.args.ctx, tt.args.srv)
})
}
}

func TestStartServer(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := StartServer(); (err != nil) != tt.wantErr {
t.Errorf("StartServer() error = %v, wantErr %v", err, tt.wantErr)
}
})
srv := NewServer()
go func() {
time.Sleep(1 * time.Second)
srv.Shutdown(context.Background())
}()
err := srv.Start()
if err != nil {
t.Error("unexpected error:", err)
}
}
5 changes: 5 additions & 0 deletions services/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func Test_loadFileToMemory(t *testing.T) {
loadFileToMemory()
})
}

fNames = []string{"Sessions2"}

loadFileToMemory()

}

func jsonMarshaller(str string) string {
Expand Down

0 comments on commit 2a47e16

Please sign in to comment.