Skip to content

Commit

Permalink
Improving repo linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavia Pezoti committed Jul 10, 2020
1 parent 9a9e42d commit c8d7156
Show file tree
Hide file tree
Showing 54 changed files with 220 additions and 203 deletions.
15 changes: 15 additions & 0 deletions .golangci.yml
Expand Up @@ -47,4 +47,19 @@ linters:
- gomnd
- interfacer
- wsl
- depguard
- errcheck
- goerr113
- dupl
- gosec
- prealloc
- lll
- godot
- scopelint
- goconst
- unparam
- staticcheck
- stylecheck
- gosimple
- testpackage

9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -41,6 +41,12 @@ make build

We're using [Ginkgo](https://onsi.github.io/ginkgo) and [Gomega](https://onsi.github.io/gomega) for testing our code. Since we're making extensive use of interfaces, external dependencies are mocked for all unit tests.

### Linter

```bash
make lint
```

#### Unit Tests
We'll try to keep testing coverage as high as possible. To run unit tests simply use:

Expand Down Expand Up @@ -70,6 +76,9 @@ To run integration tests run:
make integration
```

If you are running integration tests locally with the most recent librdkafka version (such as installed by brew) some of them will fail due to incompatible librdkafka version.
Tests should work for librdkafka v0.11.5

### Benchmark

#### Create fake push data
Expand Down
2 changes: 0 additions & 2 deletions cmd/apns.go
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/topfreegames/pusher/util"
)

var app string

func startApns(
debug, json, production bool,
config *viper.Viper,
Expand Down
12 changes: 6 additions & 6 deletions cmd/apns_test.go
Expand Up @@ -38,21 +38,21 @@ var _ = Describe("APNS", func() {

var config *viper.Viper
var mockPushQueue *mocks.APNSPushQueueMock
var mockDb *mocks.PGMock
var mockDB *mocks.PGMock
var mockStatsDClient *mocks.StatsDClientMock

BeforeEach(func() {
var err error
config, err = util.NewViperWithConfigFile(cfg)
Expect(err).NotTo(HaveOccurred())
mockDb = mocks.NewPGMock(0, 1)
mockDB = mocks.NewPGMock(0, 1)
mockPushQueue = mocks.NewAPNSPushQueueMock()
mockStatsDClient = mocks.NewStatsDClientMock()
})

Describe("[Unit]", func() {
It("Should return apnsPusher without errors", func() {
apnsPusher, err := startApns(false, false, false, config, mockStatsDClient, mockDb, mockPushQueue)
apnsPusher, err := startApns(false, false, false, config, mockStatsDClient, mockDB, mockPushQueue)
Expect(err).NotTo(HaveOccurred())
Expect(apnsPusher).NotTo(BeNil())
Expect(apnsPusher.Config).NotTo(BeNil())
Expand All @@ -62,21 +62,21 @@ var _ = Describe("APNS", func() {
})

It("Should set log to json format", func() {
apnsPusher, err := startApns(false, true, false, config, mockStatsDClient, mockDb, mockPushQueue)
apnsPusher, err := startApns(false, true, false, config, mockStatsDClient, mockDB, mockPushQueue)
Expect(err).NotTo(HaveOccurred())
Expect(apnsPusher).NotTo(BeNil())
Expect(fmt.Sprintf("%T", apnsPusher.Logger.Formatter)).To(Equal(fmt.Sprintf("%T", &logrus.JSONFormatter{})))
})

It("Should set log to debug", func() {
apnsPusher, err := startApns(true, false, false, config, mockStatsDClient, mockDb, mockPushQueue)
apnsPusher, err := startApns(true, false, false, config, mockStatsDClient, mockDB, mockPushQueue)
Expect(err).NotTo(HaveOccurred())
Expect(apnsPusher).NotTo(BeNil())
Expect(apnsPusher.Logger.Level).To(Equal(logrus.DebugLevel))
})

It("Should set log to production", func() {
apnsPusher, err := startApns(false, false, true, config, mockStatsDClient, mockDb, mockPushQueue)
apnsPusher, err := startApns(false, false, true, config, mockStatsDClient, mockDB, mockPushQueue)
Expect(err).NotTo(HaveOccurred())
Expect(apnsPusher).NotTo(BeNil())
Expect(apnsPusher.IsProduction).To(BeTrue())
Expand Down
12 changes: 6 additions & 6 deletions cmd/gcm_test.go
Expand Up @@ -40,21 +40,21 @@ var _ = Describe("GCM", func() {

var config *viper.Viper
var mockClient *mocks.GCMClientMock
var mockDb *mocks.PGMock
var mockDB *mocks.PGMock
var mockStatsDClient *mocks.StatsDClientMock

BeforeEach(func() {
var err error
config, err = util.NewViperWithConfigFile(cfg)
Expect(err).NotTo(HaveOccurred())
mockDb = mocks.NewPGMock(0, 1)
mockDB = mocks.NewPGMock(0, 1)
mockClient = mocks.NewGCMClientMock()
mockStatsDClient = mocks.NewStatsDClientMock()
})

Describe("[Unit]", func() {
It("Should return gcmPusher without errors", func() {
gcmPusher, err := startGcm(false, false, false, senderID, apiKey, config, mockStatsDClient, mockDb, mockClient)
gcmPusher, err := startGcm(false, false, false, senderID, apiKey, config, mockStatsDClient, mockDB, mockClient)
Expect(err).NotTo(HaveOccurred())
Expect(gcmPusher).NotTo(BeNil())
Expect(gcmPusher.Config).NotTo(BeNil())
Expand All @@ -64,21 +64,21 @@ var _ = Describe("GCM", func() {
})

It("Should set log to json format", func() {
gcmPusher, err := startGcm(false, true, false, senderID, apiKey, config, mockStatsDClient, mockDb, mockClient)
gcmPusher, err := startGcm(false, true, false, senderID, apiKey, config, mockStatsDClient, mockDB, mockClient)
Expect(err).NotTo(HaveOccurred())
Expect(gcmPusher).NotTo(BeNil())
Expect(fmt.Sprintf("%T", gcmPusher.Logger.Formatter)).To(Equal(fmt.Sprintf("%T", &logrus.JSONFormatter{})))
})

It("Should set log to debug", func() {
gcmPusher, err := startGcm(true, false, false, senderID, apiKey, config, mockStatsDClient, mockDb, mockClient)
gcmPusher, err := startGcm(true, false, false, senderID, apiKey, config, mockStatsDClient, mockDB, mockClient)
Expect(err).NotTo(HaveOccurred())
Expect(gcmPusher).NotTo(BeNil())
Expect(gcmPusher.Logger.Level).To(Equal(logrus.DebugLevel))
})

It("Should set log to production", func() {
gcmPusher, err := startGcm(false, false, true, senderID, apiKey, config, mockStatsDClient, mockDb, mockClient)
gcmPusher, err := startGcm(false, false, true, senderID, apiKey, config, mockStatsDClient, mockDB, mockClient)
Expect(err).NotTo(HaveOccurred())
Expect(gcmPusher).NotTo(BeNil())
Expect(gcmPusher.IsProduction).To(BeTrue())
Expand Down
6 changes: 3 additions & 3 deletions errors/push.go
Expand Up @@ -24,21 +24,21 @@ package errors

import "fmt"

//PushError reports an error sending a Push Message
//PushError reports an error sending a Push Message.
type PushError struct {
Key string
Description string
}

//NewPushError creates a new instance
//NewPushError creates a new instance.
func NewPushError(key, description string) *PushError {
return &PushError{
Key: key,
Description: description,
}
}

//Error returns a string
//Error returns a string.
func (e *PushError) Error() string {
return fmt.Sprintf("Sending push notification failed with error %s (%s).", e.Key, e.Description)
}
30 changes: 13 additions & 17 deletions extensions/apns_message_handler.go
Expand Up @@ -30,7 +30,6 @@ import (

uuid "github.com/satori/go.uuid"
"github.com/sideshow/apns2"
token "github.com/sideshow/apns2/token"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/topfreegames/pusher/errors"
Expand All @@ -40,44 +39,41 @@ import (

var apnsResMutex sync.Mutex

// Notification is the notification base struct
// Notification is the notification base struct.
type Notification struct {
DeviceToken string
Payload interface{}
Metadata map[string]interface{} `json:"metadata,omitempty"`
PushExpiry int64 `json:"push_expiry,omitempty"`
}

// APNSMessageHandler implements the messagehandler interface
// APNSMessageHandler implements the messagehandler interface.
type APNSMessageHandler struct {
feedbackReporters []interfaces.FeedbackReporter
StatsReporters []interfaces.StatsReporter
authKeyPath string
keyID string
teamID string
token *token.Token
appName string
PushQueue interfaces.APNSPushQueue
Topic string
Config *viper.Viper
clients chan *apns2.Client
failuresReceived int64
feedbackReporters []interfaces.FeedbackReporter
InflightMessagesMetadata map[string]interface{}
IsProduction bool
Logger *log.Logger
LogStatsInterval time.Duration
pendingMessagesWG *sync.WaitGroup
inflightMessagesMetadataLock *sync.Mutex
PushQueue interfaces.APNSPushQueue
responsesReceived int64
run bool
sentMessages int64
ignoredMessages int64
StatsReporters []interfaces.StatsReporter
successesReceived int64
Topic string
requestsHeap *TimeoutHeap
CacheCleaningInterval int
IsProduction bool
}

// NewAPNSMessageHandler returns a new instance of a APNSMessageHandler
// NewAPNSMessageHandler returns a new instance of a APNSMessageHandler.
func NewAPNSMessageHandler(
authKeyPath, keyID, teamID, topic, appName string,
isProduction bool,
Expand Down Expand Up @@ -200,14 +196,14 @@ func (a *APNSMessageHandler) sendMessage(message interfaces.KafkaMessage) error
return nil
}

// HandleResponses from apns
// HandleResponses from apns.
func (a *APNSMessageHandler) HandleResponses() {
for response := range a.PushQueue.ResponseChannel() {
a.handleAPNSResponse(response)
}
}

// CleanMetadataCache clears expired requests from memory
// CleanMetadataCache clears expired requests from memory.
func (a *APNSMessageHandler) CleanMetadataCache() {
var deviceToken string
var hasIndeed bool
Expand All @@ -230,7 +226,7 @@ func (a *APNSMessageHandler) CleanMetadataCache() {
}
}

// HandleMessages get messages from msgChan and send to APNS
// HandleMessages get messages from msgChan and send to APNS.
func (a *APNSMessageHandler) HandleMessages(message interfaces.KafkaMessage) {
a.sendMessage(message)
}
Expand Down Expand Up @@ -327,7 +323,7 @@ func (a *APNSMessageHandler) handleAPNSResponse(responseWithMetadata *structs.Re
return nil
}

// LogStats from time to time
// LogStats from time to time.
func (a *APNSMessageHandler) LogStats() {
l := a.Logger.WithFields(log.Fields{
"method": "logStats",
Expand Down Expand Up @@ -414,7 +410,7 @@ func (a *APNSMessageHandler) mapErrorReason(reason string) string {
}
}

//Cleanup closes connections to APNS
//Cleanup closes connections to APNS.
func (a *APNSMessageHandler) Cleanup() error {
a.PushQueue.Close()
return nil
Expand Down
1 change: 0 additions & 1 deletion extensions/apns_push_queue.go
Expand Up @@ -138,7 +138,6 @@ func (p *APNSPushQueue) pushWorker() {
DeviceToken: notification.DeviceToken,
}
p.responseChannel <- newRes

}
}

Expand Down
1 change: 0 additions & 1 deletion extensions/apns_push_queue_test.go
Expand Up @@ -27,7 +27,6 @@ import (
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
// . "github.com/topfreegames/pusher/testing"
"github.com/topfreegames/pusher/util"
)

Expand Down
11 changes: 6 additions & 5 deletions extensions/common.go
Expand Up @@ -56,11 +56,11 @@ func sendToFeedbackReporters(feedbackReporters []interfaces.FeedbackReporter, re
if err != nil {
return err
}
if feedbackReporters != nil {
for _, feedbackReporter := range feedbackReporters {
feedbackReporter.SendFeedback(topic.Game, topic.Platform, jres)
}

for _, feedbackReporter := range feedbackReporters {
feedbackReporter.SendFeedback(topic.Game, topic.Platform, jres)
}

return nil
}

Expand All @@ -76,7 +76,8 @@ func statsReporterHandleNotificationSuccess(statsReporters []interfaces.StatsRep
}
}

func statsReporterHandleNotificationFailure(statsReporters []interfaces.StatsReporter, game string, platform string, err *errors.PushError) {
func statsReporterHandleNotificationFailure(
statsReporters []interfaces.StatsReporter, game string, platform string, err *errors.PushError) {
for _, statsReporter := range statsReporters {
statsReporter.HandleNotificationFailure(game, platform, err)
}
Expand Down
3 changes: 2 additions & 1 deletion extensions/datadog_statsd.go
Expand Up @@ -101,7 +101,8 @@ func (s *StatsD) HandleNotificationSuccess(game string, platform string) {

//HandleNotificationFailure stores each type of failure
func (s *StatsD) HandleNotificationFailure(game string, platform string, err *errors.PushError) {
s.Client.Incr("failed", []string{fmt.Sprintf("platform:%s", platform), fmt.Sprintf("game:%s", game), fmt.Sprintf("reason:%s", err.Key)}, 1)
s.Client.Incr("failed",
[]string{fmt.Sprintf("platform:%s", platform), fmt.Sprintf("game:%s", game), fmt.Sprintf("reason:%s", err.Key)}, 1)
}

//InitializeFailure notifu error when is impossible tho initilizer an app
Expand Down
13 changes: 6 additions & 7 deletions extensions/gcm_message_handler.go
Expand Up @@ -32,7 +32,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
gcm "github.com/topfreegames/go-gcm"
"github.com/topfreegames/go-gcm"
"github.com/topfreegames/pusher/errors"
"github.com/topfreegames/pusher/interfaces"
)
Expand All @@ -55,13 +55,14 @@ type CCSMessageWithMetadata struct {

// GCMMessageHandler implements the messagehandler interface
type GCMMessageHandler struct {
feedbackReporters []interfaces.FeedbackReporter
StatsReporters []interfaces.StatsReporter
apiKey string
GCMClient interfaces.GCMClient
senderID string
Config *viper.Viper
failuresReceived int64
feedbackReporters []interfaces.FeedbackReporter
GCMClient interfaces.GCMClient
InflightMessagesMetadata map[string]interface{}
IsProduction bool
Logger *log.Logger
LogStatsInterval time.Duration
pendingMessages chan bool
Expand All @@ -71,13 +72,11 @@ type GCMMessageHandler struct {
PingInterval int
PingTimeout int
responsesReceived int64
run bool
senderID string
sentMessages int64
StatsReporters []interfaces.StatsReporter
successesReceived int64
requestsHeap *TimeoutHeap
CacheCleaningInterval int
IsProduction bool
}

// NewGCMMessageHandler returns a new instance of a GCMMessageHandler
Expand Down

0 comments on commit c8d7156

Please sign in to comment.