fix: monitor list - random order (#35)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #35
This commit is contained in:
parent
d43ff21893
commit
d68bcb02ca
2
go.mod
2
go.mod
|
@ -5,7 +5,6 @@ go 1.19
|
||||||
require (
|
require (
|
||||||
github.com/bwmarrin/discordgo v0.26.1
|
github.com/bwmarrin/discordgo v0.26.1
|
||||||
github.com/cenkalti/backoff/v4 v4.1.3
|
github.com/cenkalti/backoff/v4 v4.1.3
|
||||||
github.com/google/go-cmp v0.5.9
|
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
|
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
|
||||||
github.com/kelseyhightower/envconfig v1.4.0
|
github.com/kelseyhightower/envconfig v1.4.0
|
||||||
|
@ -35,6 +34,7 @@ require (
|
||||||
github.com/go-logr/logr v1.2.3 // indirect
|
github.com/go-logr/logr v1.2.3 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
github.com/gogo/protobuf v1.3.2 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
github.com/google/go-cmp v0.5.9 // indirect
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||||
github.com/gorilla/websocket v1.4.2 // indirect
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
github.com/imdario/mergo v0.3.12 // indirect
|
github.com/imdario/mergo v0.3.12 // indirect
|
||||||
|
|
|
@ -181,10 +181,11 @@ func (c *monitorCommand) handleList(s *discordgo.Session, i *discordgo.Interacti
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Embeds: []*discordgo.MessageEmbed{
|
Embeds: []*discordgo.MessageEmbed{
|
||||||
{
|
{
|
||||||
Type: discordgo.EmbedTypeRich,
|
Type: discordgo.EmbedTypeRich,
|
||||||
Title: "Monitor list",
|
Title: "Monitor list",
|
||||||
Fields: fields,
|
Description: "**Group**: " + group,
|
||||||
Timestamp: formatTimestamp(time.Now()),
|
Fields: fields,
|
||||||
|
Timestamp: formatTimestamp(time.Now()),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -104,6 +104,7 @@ func (m *Monitor) Create(ctx context.Context, groupID, serverID, tribeTag string
|
||||||
}
|
}
|
||||||
|
|
||||||
type getTribeResult struct {
|
type getTribeResult struct {
|
||||||
|
index int
|
||||||
monitor domain.Monitor
|
monitor domain.Monitor
|
||||||
tribe twhelp.Tribe
|
tribe twhelp.Tribe
|
||||||
err error
|
err error
|
||||||
|
@ -123,10 +124,11 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
ch := make(chan getTribeResult)
|
ch := make(chan getTribeResult)
|
||||||
|
|
||||||
for _, monitor := range monitors {
|
for i, monitor := range monitors {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(monitor domain.Monitor) {
|
go func(i int, monitor domain.Monitor) {
|
||||||
res := getTribeResult{
|
res := getTribeResult{
|
||||||
|
index: i,
|
||||||
monitor: monitor,
|
monitor: monitor,
|
||||||
}
|
}
|
||||||
res.tribe, res.err = m.client.GetTribeByID(
|
res.tribe, res.err = m.client.GetTribeByID(
|
||||||
|
@ -136,7 +138,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
||||||
monitor.TribeID,
|
monitor.TribeID,
|
||||||
)
|
)
|
||||||
ch <- res
|
ch <- res
|
||||||
}(monitor)
|
}(i, monitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -145,7 +147,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var firstErr error
|
var firstErr error
|
||||||
results := make([]domain.MonitorWithTribe, 0, len(monitors))
|
results := make([]domain.MonitorWithTribe, len(monitors))
|
||||||
for res := range ch {
|
for res := range ch {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
||||||
|
@ -154,7 +156,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
results = append(results, domain.MonitorWithTribe{
|
results[res.index] = domain.MonitorWithTribe{
|
||||||
Monitor: res.monitor,
|
Monitor: res.monitor,
|
||||||
Tribe: domain.TribeMeta{
|
Tribe: domain.TribeMeta{
|
||||||
ID: res.tribe.ID,
|
ID: res.tribe.ID,
|
||||||
|
@ -162,7 +164,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
||||||
Tag: res.tribe.Tag,
|
Tag: res.tribe.Tag,
|
||||||
ProfileURL: res.tribe.ProfileURL,
|
ProfileURL: res.tribe.ProfileURL,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
if firstErr != nil {
|
if firstErr != nil {
|
||||||
return nil, firstErr
|
return nil, firstErr
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/service"
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/service"
|
||||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/service/internal/mock"
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/service/internal/mock"
|
||||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -248,15 +247,9 @@ func TestMonitor_List(t *testing.T) {
|
||||||
List(context.Background(), groupID, uuid.NewString())
|
List(context.Background(), groupID, uuid.NewString())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, res, len(monitors))
|
assert.Len(t, res, len(monitors))
|
||||||
for _, m1 := range monitors {
|
for i, m := range monitors {
|
||||||
var exist bool
|
assert.Equal(t, m, res[i].Monitor)
|
||||||
for _, m2 := range res {
|
assert.Equal(t, m.TribeID, res[i].Tribe.ID)
|
||||||
if cmp.Equal(m1, m2.Monitor) && m1.TribeID == m2.Tribe.ID {
|
|
||||||
exist = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert.True(t, exist)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user