Skip to content

Commit

Permalink
Merge pull request #259 from thingspect/bconway_test_style_fixes_inte…
Browse files Browse the repository at this point in the history
…rnal

Test updates and style fixes for Go 1.22 for internal/
  • Loading branch information
bconway authored Feb 26, 2024
2 parents c4aba6e + 5b29913 commit 229a923
Show file tree
Hide file tree
Showing 43 changed files with 327 additions and 428 deletions.
22 changes: 9 additions & 13 deletions internal/atlas-accumulator/accumulator/accumulate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ func TestAccumulateMessages(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can accumulate %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can accumulate %+v", test), func(t *testing.T) {
t.Parallel()

vOutQueue := queue.NewFake()
Expand All @@ -83,7 +81,7 @@ func TestAccumulateMessages(t *testing.T) {

datapointer := NewMockdatapointer(gomock.NewController(t))
datapointer.EXPECT().Create(gomock.Any(), matcher.NewProtoMatcher(
lTest.inp.GetPoint()), lTest.inp.GetDevice().GetOrgId()).
test.inp.GetPoint()), test.inp.GetDevice().GetOrgId()).
DoAndReturn(func(
_ interface{}, _ interface{}, _ interface{},
) error {
Expand All @@ -102,7 +100,7 @@ func TestAccumulateMessages(t *testing.T) {
acc.accumulateMessages()
}()

bVOut, err := proto.Marshal(lTest.inp)
bVOut, err := proto.Marshal(test.inp)
require.NoError(t, err)
t.Logf("bVOut: %s", bVOut)

Expand Down Expand Up @@ -141,9 +139,7 @@ func TestAccumulateMessagesError(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Cannot accumulate %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Cannot accumulate %+v", test), func(t *testing.T) {
t.Parallel()

vOutQueue := queue.NewFake()
Expand All @@ -160,8 +156,8 @@ func TestAccumulateMessagesError(t *testing.T) {
) error {
defer wg.Done()

return lTest.inpErr
}).Times(lTest.inpTimes)
return test.inpErr
}).Times(test.inpTimes)

acc := Accumulator{
dpDAO: datapointer,
Expand All @@ -174,15 +170,15 @@ func TestAccumulateMessagesError(t *testing.T) {
}()

bVOut := []byte("acc-aaa")
if lTest.inpVOut != nil {
if test.inpVOut != nil {
var err error
bVOut, err = proto.Marshal(lTest.inpVOut)
bVOut, err = proto.Marshal(test.inpVOut)
require.NoError(t, err)
t.Logf("bVOut: %s", bVOut)
}

require.NoError(t, vOutQueue.Publish("", bVOut))
if lTest.inpTimes > 0 {
if test.inpTimes > 0 {
wg.Wait()
} else {
// If the failure mode isn't supported by WaitGroup operation,
Expand Down
2 changes: 1 addition & 1 deletion internal/atlas-accumulator/accumulator/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func New(cfg *config.Config) (*Accumulator, error) {

// Serve starts the message accumulators.
func (acc *Accumulator) Serve(concurrency int) {
for i := 0; i < concurrency; i++ {
for range concurrency {
go acc.accumulateMessages()
}

Expand Down
22 changes: 10 additions & 12 deletions internal/atlas-accumulator/test/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ func TestAccumulateMessages(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can accumulate %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can accumulate %+v", test), func(t *testing.T) {
t.Parallel()

bVOut, err := proto.Marshal(lTest.inp)
bVOut, err := proto.Marshal(test.inp)
require.NoError(t, err)
t.Logf("bVOut: %s", bVOut)

Expand All @@ -86,23 +84,23 @@ func TestAccumulateMessages(t *testing.T) {
testTimeout)
defer cancel()

listPoints, err := globalDPDAO.List(ctx, lTest.inp.GetDevice().GetOrgId(),
lTest.inp.GetPoint().GetUniqId(), "", "", lTest.inp.GetPoint().GetTs().AsTime(),
lTest.inp.GetPoint().GetTs().AsTime().Add(-time.Millisecond))
listPoints, err := globalDPDAO.List(ctx, test.inp.GetDevice().GetOrgId(),
test.inp.GetPoint().GetUniqId(), "", "", test.inp.GetPoint().GetTs().AsTime(),
test.inp.GetPoint().GetTs().AsTime().Add(-time.Millisecond))
t.Logf("listPoints, err: %+v, %v", listPoints, err)
require.NoError(t, err)
require.Len(t, listPoints, 1)

// Normalize token.
listPoints[0].Token = lTest.inp.GetPoint().GetToken()
listPoints[0].Token = test.inp.GetPoint().GetToken()
// Normalize timestamp.
lTest.inp.Point.Ts = timestamppb.New(
lTest.inp.GetPoint().GetTs().AsTime().Truncate(time.Millisecond))
test.inp.Point.Ts = timestamppb.New(
test.inp.GetPoint().GetTs().AsTime().Truncate(time.Millisecond))

// Testify does not currently support protobuf equality:
// https://github.com/stretchr/testify/issues/758
if !proto.Equal(lTest.inp.GetPoint(), listPoints[0]) {
t.Fatalf("\nExpect: %+v\nActual: %+v", lTest.inp.GetPoint(),
if !proto.Equal(test.inp.GetPoint(), listPoints[0]) {
t.Fatalf("\nExpect: %+v\nActual: %+v", test.inp.GetPoint(),
listPoints[0])
}
})
Expand Down
82 changes: 39 additions & 43 deletions internal/atlas-alerter/alerter/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ func TestAlertMessages(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can alert %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can alert %+v", test), func(t *testing.T) {
t.Parallel()

eOutQueue := queue.NewFake()
Expand All @@ -130,39 +128,39 @@ func TestAlertMessages(t *testing.T) {

ctrl := gomock.NewController(t)
orger := NewMockorger(ctrl)
orger.EXPECT().Read(gomock.Any(), lTest.inpEOut.GetDevice().
orger.EXPECT().Read(gomock.Any(), test.inpEOut.GetDevice().
GetOrgId()).Return(org, nil).Times(1)

alarmer := NewMockalarmer(ctrl)
alarmer.EXPECT().List(gomock.Any(), lTest.inpEOut.GetDevice().
GetOrgId(), time.Time{}, "", int32(0), lTest.inpEOut.GetRule().
GetId()).Return(lTest.inpAlarms, int32(0), nil).Times(1)
alarmer.EXPECT().List(gomock.Any(), test.inpEOut.GetDevice().
GetOrgId(), time.Time{}, "", int32(0), test.inpEOut.GetRule().
GetId()).Return(test.inpAlarms, int32(0), nil).Times(1)

userer := NewMockuserer(ctrl)
userer.EXPECT().ListByTags(gomock.Any(), lTest.inpEOut.GetDevice().
GetOrgId(), lTest.inpAlarms[0].GetUserTags()).Return(lTest.
inpUsers, nil).Times(lTest.inpUserTimes)
userer.EXPECT().ListByTags(gomock.Any(), test.inpEOut.GetDevice().
GetOrgId(), test.inpAlarms[0].GetUserTags()).Return(test.
inpUsers, nil).Times(test.inpUserTimes)

var alert *api.Alert
if lTest.inpAlertTimes > 0 {
if test.inpAlertTimes > 0 {
alert = &api.Alert{
OrgId: lTest.inpEOut.GetDevice().GetOrgId(),
UniqId: lTest.inpEOut.GetDevice().GetUniqId(),
AlarmId: lTest.inpAlarms[0].GetId(),
UserId: lTest.inpUsers[0].GetId(),
OrgId: test.inpEOut.GetDevice().GetOrgId(),
UniqId: test.inpEOut.GetDevice().GetUniqId(),
AlarmId: test.inpAlarms[0].GetId(),
UserId: test.inpUsers[0].GetId(),
Status: api.AlertStatus_SENT,
TraceId: lTest.inpEOut.GetPoint().GetTraceId(),
TraceId: test.inpEOut.GetPoint().GetTraceId(),
}
}

notifier := notify.NewMockNotifier(ctrl)
notifier.EXPECT().App(gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any()).Return(nil).Times(lTest.inpAppTimes)
gomock.Any()).Return(nil).Times(test.inpAppTimes)
notifier.EXPECT().SMS(gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any()).Return(nil).Times(lTest.inpSMSTimes)
gomock.Any()).Return(nil).Times(test.inpSMSTimes)
notifier.EXPECT().Email(gomock.Any(), org.GetDisplayName(),
org.GetEmail(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil).Times(lTest.inpEmailTimes)
Return(nil).Times(test.inpEmailTimes)

alerter := NewMockalerter(ctrl)
alerter.EXPECT().Create(gomock.Any(),
Expand All @@ -171,18 +169,18 @@ func TestAlertMessages(t *testing.T) {
defer wg.Done()

return nil
}).Times(lTest.inpAlertTimes)
}).Times(test.inpAlertTimes)

cache := cache.NewMemory()

if lTest.inpSeedCache {
if test.inpSeedCache {
ctx, cancel := context.WithTimeout(context.Background(),
5*time.Second)
defer cancel()

key := repeatKey(lTest.inpEOut.GetDevice().GetOrgId(),
lTest.inpEOut.GetDevice().GetId(),
lTest.inpAlarms[0].GetId(), lTest.inpUsers[0].GetId())
key := repeatKey(test.inpEOut.GetDevice().GetOrgId(),
test.inpEOut.GetDevice().GetId(),
test.inpAlarms[0].GetId(), test.inpUsers[0].GetId())
ok, err := cache.SetIfNotExistTTL(ctx, key, 1, time.Minute)
require.True(t, ok)
require.NoError(t, err)
Expand All @@ -204,12 +202,12 @@ func TestAlertMessages(t *testing.T) {
ale.alertMessages()
}()

bEOut, err := proto.Marshal(lTest.inpEOut)
bEOut, err := proto.Marshal(test.inpEOut)
require.NoError(t, err)
t.Logf("bEOut: %s", bEOut)

require.NoError(t, eOutQueue.Publish("", bEOut))
if lTest.inpAlertTimes > 0 {
if test.inpAlertTimes > 0 {
wg.Wait()
} else {
// If the success mode isn't supported by WaitGroup operation,
Expand Down Expand Up @@ -378,9 +376,7 @@ func TestAlertMessagesError(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can alert %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can alert %+v", test), func(t *testing.T) {
t.Parallel()

eOutQueue := queue.NewFake()
Expand All @@ -392,36 +388,36 @@ func TestAlertMessagesError(t *testing.T) {

ctrl := gomock.NewController(t)
orger := NewMockorger(ctrl)
orger.EXPECT().Read(gomock.Any(), gomock.Any()).Return(lTest.inpOrg,
lTest.inpOrgErr).Times(lTest.inpOrgTimes)
orger.EXPECT().Read(gomock.Any(), gomock.Any()).Return(test.inpOrg,
test.inpOrgErr).Times(test.inpOrgTimes)

alarmer := NewMockalarmer(ctrl)
alarmer.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any(), gomock.Any(), gomock.Any()).
Return(lTest.inpAlarms, int32(0), lTest.inpAlarmsErr).
Times(lTest.inpAlarmTimes)
Return(test.inpAlarms, int32(0), test.inpAlarmsErr).
Times(test.inpAlarmTimes)

userer := NewMockuserer(ctrl)
userer.EXPECT().ListByTags(gomock.Any(), gomock.Any(),
gomock.Any()).Return(lTest.inpUsers, lTest.inpUsersErr).
Times(lTest.inpUserTimes)
gomock.Any()).Return(test.inpUsers, test.inpUsersErr).
Times(test.inpUserTimes)

cacher := cache.NewMockCacher(ctrl)
cacher.EXPECT().SetIfNotExistTTL(gomock.Any(), gomock.Any(),
gomock.Any(), gomock.Any()).Return(lTest.inpCache,
lTest.inpCacheErr).Times(lTest.inpCacheTimes)
gomock.Any(), gomock.Any()).Return(test.inpCache,
test.inpCacheErr).Times(test.inpCacheTimes)

notifier := notify.NewMockNotifier(ctrl)
notifier.EXPECT().App(gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any()).Return(lTest.inpAppErr).Times(lTest.inpAppTimes)
gomock.Any()).Return(test.inpAppErr).Times(test.inpAppTimes)

alerter := NewMockalerter(ctrl)
alerter.EXPECT().Create(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ interface{}, _ interface{}) error {
defer wg.Done()

return lTest.inpAlertErr
}).Times(lTest.inpAlertTimes)
return test.inpAlertErr
}).Times(test.inpAlertTimes)

ale := Alerter{
orgDAO: orger,
Expand All @@ -440,14 +436,14 @@ func TestAlertMessagesError(t *testing.T) {
}()

bEOut := []byte("ale-aaa")
if lTest.inpEOut != nil {
bEOut, err = proto.Marshal(lTest.inpEOut)
if test.inpEOut != nil {
bEOut, err = proto.Marshal(test.inpEOut)
require.NoError(t, err)
t.Logf("bEOut: %s", bEOut)
}

require.NoError(t, eOutQueue.Publish("", bEOut))
if lTest.inpAlertTimes > 0 {
if test.inpAlertTimes > 0 {
wg.Wait()
} else {
// If the success mode isn't supported by WaitGroup operation,
Expand Down
2 changes: 1 addition & 1 deletion internal/atlas-alerter/alerter/alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func New(cfg *config.Config) (*Alerter, error) {

// Serve starts the message alerters.
func (ale *Alerter) Serve(concurrency int) {
for i := 0; i < concurrency; i++ {
for range concurrency {
go ale.alertMessages()
}

Expand Down
6 changes: 2 additions & 4 deletions internal/atlas-alerter/alerter/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
func TestRepeatKey(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can key %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can key %v", i), func(t *testing.T) {
t.Parallel()

orgID := uuid.NewString()
Expand Down
18 changes: 8 additions & 10 deletions internal/atlas-alerter/test/alerter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,13 @@ func TestAlertMessagesError(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Cannot alert %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Cannot alert %+v", test), func(t *testing.T) {
t.Parallel()

bEOut := []byte("ale-aaa")
if lTest.inpEOut != nil {
if test.inpEOut != nil {
var err error
bEOut, err = proto.Marshal(lTest.inpEOut)
bEOut, err = proto.Marshal(test.inpEOut)
require.NoError(t, err)
t.Logf("bEOut: %s", bEOut)
}
Expand All @@ -319,22 +317,22 @@ func TestAlertMessagesError(t *testing.T) {
defer cancel()

listAlerts, err := globalAleDAO.List(ctx, createOrg.GetId(),
dev.GetUniqId(), "", lTest.inpAlarmID, createUser.GetId(), time.Now(),
dev.GetUniqId(), "", test.inpAlarmID, createUser.GetId(), time.Now(),
time.Now().Add(-4*time.Second))
t.Logf("listAlerts, err: %+v, %v", listAlerts, err)
require.NoError(t, err)

if lTest.inpNotifyErr != nil {
if test.inpNotifyErr != nil {
require.Len(t, listAlerts, 1)

alert := &api.Alert{
OrgId: createOrg.GetId(),
UniqId: dev.GetUniqId(),
AlarmId: lTest.inpAlarmID,
AlarmId: test.inpAlarmID,
UserId: createUser.GetId(),
Status: api.AlertStatus_ERROR,
Error: lTest.inpNotifyErr.Error(),
TraceId: lTest.inpEOut.GetPoint().GetTraceId(),
Error: test.inpNotifyErr.Error(),
TraceId: test.inpEOut.GetPoint().GetTraceId(),
}

// Normalize timestamp.
Expand Down
Loading

0 comments on commit 229a923

Please sign in to comment.