Skip to content

Commit 6765d9b

Browse files
author
xbucks
authored
Minor upates (#2)
* wip * add clarifications * Coverage changes * Coverage changes
1 parent e46dd92 commit 6765d9b

File tree

6 files changed

+70
-29
lines changed

6 files changed

+70
-29
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,5 @@ MigrationBackup/
479479
# Ionide (cross platform F# VS Code tools) working folder
480480
.ionide/
481481

482-
# End of https://www.toptal.com/developers/gitignore/api/go,intellij+iml,vs
482+
# End of https://www.toptal.com/developers/gitignore/api/go,intellij+iml,vs
483+
/.idea/sonarlint/

.idea/sonarlint/issuestore/index.pb

+1-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.18 as builder
1+
FROM golang:1.20 as builder
22
LABEL stage=builder
33
WORKDIR /usr/src/app
44

@@ -17,6 +17,9 @@ WORKDIR /app/
1717
ARG port
1818
COPY --from=builder /usr/src/app/app .
1919
COPY --from=builder /usr/src/app/config/*.yaml /app/config/
20+
## As of golang:1.20 this cerrtificate is included in the base image.
21+
## So, it is not needed to add this certificate manually or run any add ca-certificates commands.
22+
## This certificate is added to avoid any issues incase your application is making any HTTPS etc., connections
2023
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
2124
ENTRYPOINT ["./app"]
2225
EXPOSE $port

Makefile

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ docker-build:
3333
docker build -t ${DOCKER_IMAGE_NAME} . \
3434
--build-arg port=${port} \
3535

36+
docker-build-debug:
37+
$(info ---> Building Docker Image: ${DOCKER_IMAGE_NAME}, Exposed Port: ${port})
38+
docker build --no-cache --progress plain -t ${DOCKER_IMAGE_NAME} . \
39+
--build-arg port=${port} \
40+
3641
## docker-run: Run the API server as a docker container
3742
docker-run:
3843
$(info ---> Running Docker Container: ${DOCKER_CONTAINER_NAME} in Environment: ${profile})
@@ -80,8 +85,11 @@ test:
8085

8186
## coverage: Measures code coverage
8287
coverage:
83-
go test -coverprofile=coverage.out -covermode=atomic ./... && go tool cover -func=coverage.out
84-
go test -race -coverprofile=coverage.out -covermode=atomic ./...
88+
go test ./... -v -coverprofile coverage.out -covermode count
89+
go tool cover -func=coverage.out
90+
91+
coverage-html:
92+
go tool cover -html=coverage.out
8593

8694
.PHONY: help
8795
help: Makefile

internal/db/connection.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package db
22

33
import (
44
"context"
5+
"errors"
56
"sync"
67
"time"
78

@@ -12,7 +13,13 @@ import (
1213
"go.mongodb.org/mongo-driver/mongo/options"
1314
)
1415

15-
var connectOnce sync.Once
16+
var (
17+
InvalidConnUrlErr = errors.New("failed to connect to DB, as the connection string is invalid")
18+
ClientCreationErr = errors.New("failed to create new client to connect with db")
19+
ClientInitErr = errors.New("failed to initialize db client")
20+
ConnectionLeak = errors.New("unable to disconnect from db, potential connection leak")
21+
connectOnce sync.Once
22+
)
1623

1724
// ConnectionTimeOut - Max time to establish DB connection // TODO: Move to config
1825
const ConnectionTimeOut = 10 * time.Second
@@ -40,7 +47,7 @@ func NewMongoManager(dbName string, connUrl string) (MongoManager, error) {
4047
dbMgr := &connectionManager{}
4148
var connErr error
4249
connectOnce.Do(func() {
43-
if c, err := newConnection(connUrl); err != nil {
50+
if c, err := newClient(connUrl); err != nil {
4451
connErr = err
4552
} else {
4653
db := c.Database(dbName)
@@ -57,21 +64,24 @@ func NewMongoManager(dbName string, connUrl string) (MongoManager, error) {
5764
return dbMgr, connErr
5865
}
5966

60-
// newConnection - Establishes connection using given connection url and returns mongo client
61-
func newConnection(connectionUrl string) (*mongo.Client, error) {
67+
// newClient - creates a new Mongo Client to connect to the specified url and initializes the Client
68+
func newClient(connectionUrl string) (*mongo.Client, error) {
69+
if len(connectionUrl) == 0 {
70+
return nil, InvalidConnUrlErr
71+
}
6272
clientOptions := options.Client().ApplyURI(connectionUrl)
6373
client, err := mongo.NewClient(clientOptions)
6474
if err != nil {
6575
log.Error().Err(err).Msg("Connection Failed to Database")
66-
return nil, err
76+
return nil, ClientCreationErr
6777
}
6878

6979
ctx, cancel := context.WithTimeout(context.Background(), ConnectionTimeOut)
7080
defer cancel()
7181
connErr := client.Connect(ctx)
7282
if connErr != nil {
7383
log.Error().Err(connErr).Msg("Connection Failed to Database")
74-
return nil, err
84+
return nil, ClientInitErr
7585
}
7686

7787
return client, nil
@@ -96,7 +106,7 @@ func (c *connectionManager) Disconnect() error {
96106
log.Info().Msg("Disconnecting from Database")
97107
if err := c.client.Disconnect(context.Background()); err != nil {
98108
log.Error().Err(err).Msg("unable to disconnect from DB")
99-
return err
109+
return ConnectionLeak
100110
}
101111
log.Info().Msg("Successfully disconnected from DB")
102112
return nil
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package db
2+
3+
import "testing"
4+
5+
func TestNewConnection(t *testing.T) {
6+
type newConnectionTestCase struct {
7+
Description string
8+
Input string
9+
ExpectedErr error
10+
}
11+
12+
var testCases = []newConnectionTestCase{
13+
{
14+
Description: "expect error when connection url is empty",
15+
Input: "",
16+
ExpectedErr: InvalidConnUrlErr,
17+
},
18+
{
19+
Description: "expect client creation error when connection url is invalid",
20+
Input: "mongodb+srv://fuzzy-yogi:[email protected]/?retryWrites=true&w=majority",
21+
ExpectedErr: ClientCreationErr,
22+
},
23+
{
24+
Description: "expect client object",
25+
Input: "mongodb://test",
26+
ExpectedErr: nil,
27+
},
28+
}
29+
30+
for i, tc := range testCases {
31+
_, err := newClient(tc.Input)
32+
if err != tc.ExpectedErr {
33+
t.Errorf("TestNewConnection test case %d:%s failed: expected %v; got %v", i, tc.Description, tc.ExpectedErr, err)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)