Skip to content

Commit

Permalink
Merge pull request #34 from topfreegames/fix-ci
Browse files Browse the repository at this point in the history
Move all integ tests into a same Spec and mark the ones failing as Pending
  • Loading branch information
leohahn committed Jul 17, 2020
2 parents a90bd59 + db9e5c2 commit a78ce26
Show file tree
Hide file tree
Showing 57 changed files with 233 additions and 209 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

3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -10,14 +10,13 @@ sudo: true
env:
matrix:
- LIBRDKAFKA_VERSION=v0.11.5
- LIBRDKAFKA_VERSION=master
global:
secure: dm9+XwwQk759cpVpleBjHhtbbe5s3LNjz9wwMGa3DEENFAMiX4HjJSmfyvV8D3t0NdicDJrYW4HdTIqacneIOFbkMEYA4RmwdxBe4NcJQC6es/8I+EL52/EKmwKwtnkvsQFrQkJmzS1JYKNQb73bWY4huTGNxYiUiX9xfK3sO+X9xi62eN/xpOVaSbIwB44h5oNEj7/aaeQEvsqg69eO5hRsCdIRWXxTyP37GdMupC4vtE6A5uU8LdGzE4f0dP5JKxmL53s0fCM0kqjP1ycIOwjhlqWdo+BjRBAKpE8cM4p/BnzM+rCWNyjt333Kxjwp42FBnfaYiEET1f0xN43h2rfZRHvCoL3ATGev2aDzGMhsOuqHLfVRFt7u/9QynZltApctsmEMMYXDY6a9TsbcE6UjC7GG4HTIOO/PmWyburL18aTicCWLUvbKhljG7873sAwZchrtCxXxyBkd6zjHznciwljbVvexMlWhaO269C++u95fdEUO2pkidDNkrnf5TjYJnGJGcyBvXAvkGdZrxiOwV3pxH1mfb7D87tvzo60XRf3/7++Un8iggFb9+Ppd35eYCnpNmEP95IhKigkJ6hMY5Al8ARpQ+JA6EwTqMPZf+r5RiAq4uxOeYK0rEiWDY7RxqlPPW7AGdoyy/OJ1LgbCdZssZs+Ig4JWViwdTWg=
cache:
directories:
- "$HOME/.cache/librdkafka"
before_install:
- sudo /bin/bash ./travis_install_kafka.sh
- sudo /bin/bash ./travis-install-librkafka.sh
install:
- make setup-ci
script:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -133,7 +133,7 @@ test-integration integration func: deps test-db-drop test-db-create
@echo "= Running integration tests... ="
@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
@echo
@env MY_IP=${MY_IP} ginkgo -r -tags=integration --randomizeAllSpecs --randomizeSuites --skip="\[Integration\].*" .
@env MY_IP=${MY_IP} ginkgo -r -tags=integration --randomizeAllSpecs --randomizeSuites --focus="\[Integration\].*" .
@echo
@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
@echo "= Integration tests finished. ="
Expand Down
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
15 changes: 10 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,12 @@ 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
6 changes: 5 additions & 1 deletion extensions/datadog_statsd.go
Expand Up @@ -101,7 +101,11 @@ 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

0 comments on commit a78ce26

Please sign in to comment.