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 (
|
||||
github.com/bwmarrin/discordgo v0.26.1
|
||||
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/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
|
||||
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/stdr v1.2.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/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/imdario/mergo v0.3.12 // indirect
|
||||
|
|
|
@ -183,6 +183,7 @@ func (c *monitorCommand) handleList(s *discordgo.Session, i *discordgo.Interacti
|
|||
{
|
||||
Type: discordgo.EmbedTypeRich,
|
||||
Title: "Monitor list",
|
||||
Description: "**Group**: " + group,
|
||||
Fields: fields,
|
||||
Timestamp: formatTimestamp(time.Now()),
|
||||
},
|
||||
|
|
|
@ -104,6 +104,7 @@ func (m *Monitor) Create(ctx context.Context, groupID, serverID, tribeTag string
|
|||
}
|
||||
|
||||
type getTribeResult struct {
|
||||
index int
|
||||
monitor domain.Monitor
|
||||
tribe twhelp.Tribe
|
||||
err error
|
||||
|
@ -123,10 +124,11 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
|||
var wg sync.WaitGroup
|
||||
ch := make(chan getTribeResult)
|
||||
|
||||
for _, monitor := range monitors {
|
||||
for i, monitor := range monitors {
|
||||
wg.Add(1)
|
||||
go func(monitor domain.Monitor) {
|
||||
go func(i int, monitor domain.Monitor) {
|
||||
res := getTribeResult{
|
||||
index: i,
|
||||
monitor: monitor,
|
||||
}
|
||||
res.tribe, res.err = m.client.GetTribeByID(
|
||||
|
@ -136,7 +138,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
|||
monitor.TribeID,
|
||||
)
|
||||
ch <- res
|
||||
}(monitor)
|
||||
}(i, monitor)
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
@ -145,7 +147,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
|||
}()
|
||||
|
||||
var firstErr error
|
||||
results := make([]domain.MonitorWithTribe, 0, len(monitors))
|
||||
results := make([]domain.MonitorWithTribe, len(monitors))
|
||||
for res := range ch {
|
||||
wg.Done()
|
||||
|
||||
|
@ -154,7 +156,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
|||
continue
|
||||
}
|
||||
|
||||
results = append(results, domain.MonitorWithTribe{
|
||||
results[res.index] = domain.MonitorWithTribe{
|
||||
Monitor: res.monitor,
|
||||
Tribe: domain.TribeMeta{
|
||||
ID: res.tribe.ID,
|
||||
|
@ -162,7 +164,7 @@ func (m *Monitor) List(ctx context.Context, groupID, serverID string) ([]domain.
|
|||
Tag: res.tribe.Tag,
|
||||
ProfileURL: res.tribe.ProfileURL,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
if firstErr != nil {
|
||||
return nil, firstErr
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
"gitea.dwysokinski.me/twhelp/dcbot/internal/service"
|
||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/service/internal/mock"
|
||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -248,15 +247,9 @@ func TestMonitor_List(t *testing.T) {
|
|||
List(context.Background(), groupID, uuid.NewString())
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, res, len(monitors))
|
||||
for _, m1 := range monitors {
|
||||
var exist bool
|
||||
for _, m2 := range res {
|
||||
if cmp.Equal(m1, m2.Monitor) && m1.TribeID == m2.Tribe.ID {
|
||||
exist = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, exist)
|
||||
for i, m := range monitors {
|
||||
assert.Equal(t, m, res[i].Monitor)
|
||||
assert.Equal(t, m.TribeID, res[i].Tribe.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user