Skip to content

Commit

Permalink
Enable race detector in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Aug 12, 2016
1 parent b6c97f6 commit c02425f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TEST?=./...
TESTARGS?=
TESTARGS?=-race
DEPS = $(shell go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)
CURRENT_DIR = $(shell pwd)
SOURCEDIR = $(CURRENT_DIR)
Expand Down
21 changes: 17 additions & 4 deletions consul/consul_stub.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package consul

import (
"sync"

"github.com/allegro/marathon-consul/apps"
consulapi "github.com/hashicorp/consul/api"
)

type ConsulStub struct {
sync.RWMutex
services map[apps.TaskId]*consulapi.AgentServiceRegistration
ErrorServices map[apps.TaskId]error
ErrorGetServices map[string]error
Expand All @@ -25,7 +28,9 @@ func NewConsulStubWithTag(tag string) *ConsulStub {
}
}

func (c ConsulStub) GetAllServices() ([]*consulapi.CatalogService, error) {
func (c *ConsulStub) GetAllServices() ([]*consulapi.CatalogService, error) {
c.RLock()
defer c.RUnlock()
var catalog []*consulapi.CatalogService
for _, s := range c.services {
catalog = append(catalog, &consulapi.CatalogService{
Expand All @@ -40,9 +45,11 @@ func (c ConsulStub) GetAllServices() ([]*consulapi.CatalogService, error) {
return catalog, nil
}

func (c ConsulStub) GetServices(name string) ([]*consulapi.CatalogService, error) {
if error, ok := c.ErrorGetServices[name]; ok {
return nil, error
func (c *ConsulStub) GetServices(name string) ([]*consulapi.CatalogService, error) {
c.RLock()
defer c.RUnlock()
if err, ok := c.ErrorGetServices[name]; ok {
return nil, err
}
var catalog []*consulapi.CatalogService
for _, s := range c.services {
Expand All @@ -61,6 +68,8 @@ func (c ConsulStub) GetServices(name string) ([]*consulapi.CatalogService, error
}

func (c *ConsulStub) Register(task *apps.Task, app *apps.App) error {
c.Lock()
defer c.Unlock()
if err, ok := c.ErrorServices[task.ID]; ok {
return err
} else {
Expand All @@ -78,6 +87,8 @@ func (c *ConsulStub) ServiceName(app *apps.App) string {
}

func (c *ConsulStub) Deregister(serviceId apps.TaskId, agent string) error {
c.Lock()
defer c.Unlock()
if err, ok := c.ErrorServices[serviceId]; ok {
return err
} else {
Expand All @@ -87,6 +98,8 @@ func (c *ConsulStub) Deregister(serviceId apps.TaskId, agent string) error {
}

func (c *ConsulStub) RegisteredServicesIds() []string {
c.RLock()
defer c.RUnlock()
services, _ := c.GetAllServices()
servicesIds := []string{}
for _, consulService := range services {
Expand Down
5 changes: 0 additions & 5 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func TestMark(t *testing.T) {
t.Parallel()
// given
Init(Config{Target: "stdout", Prefix: ""})

Expand All @@ -33,8 +32,6 @@ func TestMark(t *testing.T) {
}

func TestTime(t *testing.T) {
t.Parallel()

// given
Init(Config{Target: "stdout", Prefix: ""})

Expand All @@ -56,8 +53,6 @@ func TestTime(t *testing.T) {
}

func TestUpdateGauge(t *testing.T) {
t.Parallel()

// given
Init(Config{Target: "stdout", Prefix: ""})

Expand Down
29 changes: 15 additions & 14 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package sync

import (
"fmt"
"os"
"sync"
"testing"
"time"

"github.com/allegro/marathon-consul/apps"
"github.com/allegro/marathon-consul/consul"
"github.com/allegro/marathon-consul/marathon"
. "github.com/allegro/marathon-consul/utils"
consulapi "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/assert"

"fmt"
"os"
"time"

"github.com/allegro/marathon-consul/apps"
)

func TestSyncJob_ShouldSyncOnLeadership(t *testing.T) {
Expand All @@ -32,10 +31,10 @@ func TestSyncJob_ShouldSyncOnLeadership(t *testing.T) {
ticker := sync.StartSyncServicesJob()

// then
ticker.Stop()
select {
case <-time.After(15 * time.Millisecond):
ticker.Stop()
assert.Equal(t, 2, services.RegistrationsCount(app.Tasks[0].ID.String()))
assert.Equal(t, 1, services.RegistrationsCount(app.Tasks[0].ID.String()))
}
}

Expand All @@ -56,10 +55,7 @@ func TestSyncJob_ShouldNotSyncWhenDisabled(t *testing.T) {

// then
assert.Nil(t, ticker)
select {
case <-time.After(15 * time.Millisecond):
assert.Equal(t, 0, services.RegistrationsCount(app.Tasks[0].ID.String()))
}
assert.Equal(t, 0, services.RegistrationsCount(app.Tasks[0].ID.String()))
}

func TestSyncJob_ShouldDefaultLeaderConfigurationToResolvedHostname(t *testing.T) {
Expand All @@ -78,10 +74,10 @@ func TestSyncJob_ShouldDefaultLeaderConfigurationToResolvedHostname(t *testing.T
ticker := sync.StartSyncServicesJob()

// then
ticker.Stop()
select {
case <-time.After(15 * time.Millisecond):
ticker.Stop()
assert.Equal(t, 2, services.RegistrationsCount(app.Tasks[0].ID.String()))
assert.Equal(t, 1, services.RegistrationsCount(app.Tasks[0].ID.String()))
}
}

Expand Down Expand Up @@ -134,6 +130,7 @@ func TestSyncServices_ShouldSyncOnForceWithoutLeadership(t *testing.T) {
}

type ConsulServicesMock struct {
sync.RWMutex
registrations map[string]int
}

Expand All @@ -152,11 +149,15 @@ func (c *ConsulServicesMock) GetAllServices() ([]*consulapi.CatalogService, erro
}

func (c *ConsulServicesMock) Register(task *apps.Task, app *apps.App) error {
c.Lock()
defer c.Unlock()
c.registrations[task.ID.String()]++
return nil
}

func (c *ConsulServicesMock) RegistrationsCount(instanceId string) int {
c.RLock()
defer c.RUnlock()
return c.registrations[instanceId]
}

Expand Down

0 comments on commit c02425f

Please sign in to comment.