Skip to content

Commit ea978ab

Browse files
authored
✨ Extended job manager to log job messages (#89)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Proprietary --> ### Description Add an API to log job's messages ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 5f5cde8 commit ea978ab

File tree

8 files changed

+199
-43
lines changed

8 files changed

+199
-43
lines changed

changes/20241125162709.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Extended job manager to log job messages

utils/go.mod

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ module github.com/ARM-software/embedded-development-services-client-utils/utils
33
go 1.23
44

55
require (
6-
github.com/ARM-software/embedded-development-services-client/client v1.36.1
7-
github.com/ARM-software/golang-utils/utils v1.74.1
6+
github.com/ARM-software/embedded-development-services-client/client v1.37.2
7+
github.com/ARM-software/golang-utils/utils v1.75.1
88
github.com/go-faker/faker/v4 v4.5.0
99
github.com/go-logr/logr v1.4.2
1010
github.com/golang/mock v1.6.0
11-
github.com/stretchr/testify v1.9.0
11+
github.com/stretchr/testify v1.10.0
1212
go.uber.org/atomic v1.11.0
1313
go.uber.org/goleak v1.3.0
1414
golang.org/x/sync v0.9.0
1515
)
1616

1717
require (
1818
github.com/OneOfOne/xxhash v1.2.8 // indirect
19-
github.com/avast/retry-go v3.0.0+incompatible // indirect
19+
github.com/avast/retry-go/v4 v4.6.0 // indirect
2020
github.com/bmatcuk/doublestar/v3 v3.0.0 // indirect
2121
github.com/bombsimon/logrusr/v4 v4.1.0 // indirect
2222
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
23+
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
2324
github.com/djherbis/times v1.6.0 // indirect
2425
github.com/dolmen-go/contextio v1.0.0 // indirect
26+
github.com/ebitengine/purego v0.8.1 // indirect
2527
github.com/evanphx/hclogr v0.2.0 // indirect
2628
github.com/fatih/color v1.16.0 // indirect
2729
github.com/fsnotify/fsnotify v1.7.0 // indirect
@@ -52,8 +54,7 @@ require (
5254
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
5355
github.com/sasha-s/go-deadlock v0.3.5 // indirect
5456
github.com/scjalliance/comshim v0.0.0-20190308082608-cf06d2532c4e // indirect
55-
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
56-
github.com/shoenig/go-m1cpu v0.1.6 // indirect
57+
github.com/shirou/gopsutil/v4 v4.24.10 // indirect
5758
github.com/sirupsen/logrus v1.9.3 // indirect
5859
github.com/sourcegraph/conc v0.3.0 // indirect
5960
github.com/spaolacci/murmur3 v1.1.0 // indirect

utils/go.sum

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
bitbucket.org/creachadair/stringset v0.0.9/go.mod h1:t+4WcQ4+PXTa8aQdNKe40ZP6iwesoMFWAxPGd3UGjyY=
2-
github.com/ARM-software/embedded-development-services-client/client v1.36.1 h1:DsW0oaSbI+xkGDYBfbz5SN5q5nu1EgzCXHhOlFYigEE=
3-
github.com/ARM-software/embedded-development-services-client/client v1.36.1/go.mod h1:FOYpc1CM02XN6Psj53+P/yN6DBSPY4HEC1eKZYY2RO8=
4-
github.com/ARM-software/golang-utils/utils v1.74.1 h1:OHJjn+Ti8ty3MRKGpjPtgJvngPwLxRy0zewZ4GvoHX0=
5-
github.com/ARM-software/golang-utils/utils v1.74.1/go.mod h1:sXDZAe2s4PKz4eG2H0qbvsR2z45Wacs58p2/DftdN5g=
2+
github.com/ARM-software/embedded-development-services-client/client v1.37.2 h1:LAdBYXVUBk4PviKGPocsiECbmd/XlbcBs0a6fRnyW10=
3+
github.com/ARM-software/embedded-development-services-client/client v1.37.2/go.mod h1:73EyYj6gez9vYGyF/nf3gXEjX0phwhrKm//UIomGfsE=
4+
github.com/ARM-software/golang-utils/utils v1.75.1 h1:qOetGmy+GtTA9lxDVaRfkG83HUi0fKYLvb28RwdRLa4=
5+
github.com/ARM-software/golang-utils/utils v1.75.1/go.mod h1:I+vVGT+a9oYuVA/AQBj9T++yingwWqNrrxAp+SQq+TI=
66
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
77
github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
88
github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
99
github.com/StackExchange/wmi v1.2.0/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
1010
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
1111
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
12-
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
13-
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
12+
github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA=
13+
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
1414
github.com/bmatcuk/doublestar/v3 v3.0.0 h1:TQtVPlDnAYwcrVNB2JiGuMc++H5qzWZd9PhkNo5WyHI=
1515
github.com/bmatcuk/doublestar/v3 v3.0.0/go.mod h1:6PcTVMw80pCY1RVuoqu3V++99uQB3vsSYKPTd8AWA0k=
1616
github.com/bombsimon/logrusr/v4 v4.1.0 h1:uZNPbwusB0eUXlO8hIUwStE6Lr5bLN6IgYgG+75kuh4=
@@ -22,10 +22,14 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
2222
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2323
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
2424
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
25+
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
26+
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
2527
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
2628
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
2729
github.com/dolmen-go/contextio v1.0.0 h1:bNfCo4gsRIhMeo6Z1ImXzkxZG81B6I5t2fUFJjphdAU=
2830
github.com/dolmen-go/contextio v1.0.0/go.mod h1:cxc20xI7fOgsFHWgt+PenlDDnMcrvh7Ocuj5hEFIdEk=
31+
github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE=
32+
github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
2933
github.com/evanphx/hclogr v0.2.0 h1:YOWab0yQmEx25AnNRhy/8wIY9VG4urejJtQqSo5zPu0=
3034
github.com/evanphx/hclogr v0.2.0/go.mod h1:1jTbx9bMs6/CBoiPwzNobWb7BFkK/KF5XljvPuQQB6Q=
3135
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
@@ -54,8 +58,8 @@ github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead5
5458
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
5559
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
5660
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
57-
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
58-
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
61+
github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
62+
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
5963
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs=
6064
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
6165
github.com/golang/glog v0.0.0-20210429001901-424d2337a529/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -152,12 +156,8 @@ github.com/sasha-s/go-deadlock v0.3.5 h1:tNCOEEDG6tBqrNDOX35j/7hL5FcFViG6awUGROb
152156
github.com/sasha-s/go-deadlock v0.3.5/go.mod h1:bugP6EGbdGYObIlx7pUZtWqlvo8k9H6vCBBsiChJQ5U=
153157
github.com/scjalliance/comshim v0.0.0-20190308082608-cf06d2532c4e h1:+/AzLkOdIXEPrAQtwAeWOBnPQ0BnYlBW0aCZmSb47u4=
154158
github.com/scjalliance/comshim v0.0.0-20190308082608-cf06d2532c4e/go.mod h1:9Tc1SKnfACJb9N7cw2eyuI6xzy845G7uZONBsi5uPEA=
155-
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
156-
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
157-
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
158-
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
159-
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
160-
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
159+
github.com/shirou/gopsutil/v4 v4.24.10 h1:7VOzPtfw/5YDU+jLEoBwXwxJbQetULywoSV4RYY7HkM=
160+
github.com/shirou/gopsutil/v4 v4.24.10/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0vM7DHbZRxnIT8=
161161
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
162162
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
163163
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
@@ -182,8 +182,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
182182
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
183183
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
184184
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
185-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
186185
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
186+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
187+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
187188
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
188189
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
189190
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=

utils/job/interfaces.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"time"
1212

1313
"github.com/ARM-software/embedded-development-services-client-utils/utils/resource"
14+
"github.com/ARM-software/golang-utils/utils/collection/pagination"
15+
"github.com/ARM-software/golang-utils/utils/logs"
1416
)
1517

1618
// Mocks are generated using `go generate ./...`
@@ -48,4 +50,8 @@ type IJobManager interface {
4850
WaitForJobCompletion(ctx context.Context, job IAsynchronousJob) (err error)
4951
// WaitForJobCompletionWithTimeout waits for a job to complete but with timeout protection.
5052
WaitForJobCompletionWithTimeout(ctx context.Context, job IAsynchronousJob, timeout time.Duration) (err error)
53+
// GetMessagePaginator returns a paginator over job messages.
54+
GetMessagePaginator(ctx context.Context, logger logs.Loggers, job IAsynchronousJob, timeout time.Duration) (pagination.IStreamPaginatorAndPageFetcher, error)
55+
// LogJobMessagesUntilNow logs all the job messages until now. This is doing the same as WaitForJobCompletionWithTimeout apart from waiting for job completion.
56+
LogJobMessagesUntilNow(ctx context.Context, job IAsynchronousJob, timeout time.Duration) (err error)
5157
}

utils/job/manager.go

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (m *Manager) FetchJobMessagesFirstPage(ctx context.Context, job IAsynchrono
5656
return
5757
}
5858

59-
func waitForJobState(ctx context.Context, logger messages.IMessageLogger, job IAsynchronousJob, jobState string, checkStateFunc func(context.Context, IAsynchronousJob) (bool, error), timeout time.Duration) (err error) {
59+
func waitForJobState(ctx context.Context, logger logs.Loggers, job IAsynchronousJob, jobState string, checkStateFunc func(context.Context, IAsynchronousJob) (bool, error), timeout time.Duration) (err error) {
6060
err = parallelisation.DetermineContextError(ctx)
6161
if err != nil {
6262
return
@@ -85,11 +85,11 @@ func waitForJobState(ctx context.Context, logger messages.IMessageLogger, job IA
8585
return
8686
}
8787

88-
func (m *Manager) waitForJobToStart(ctx context.Context, logger messages.IMessageLogger, job IAsynchronousJob, timeout time.Duration) error {
88+
func (m *Manager) waitForJobToStart(ctx context.Context, logger logs.Loggers, job IAsynchronousJob, timeout time.Duration) error {
8989
return waitForJobState(ctx, logger, job, "start", m.HasJobStarted, timeout)
9090
}
9191

92-
func (m *Manager) waitForJobToHaveMessagesAvailable(ctx context.Context, logger messages.IMessageLogger, job IAsynchronousJob, timeout time.Duration) error {
92+
func (m *Manager) waitForJobToHaveMessagesAvailable(ctx context.Context, logger logs.Loggers, job IAsynchronousJob, timeout time.Duration) error {
9393
return waitForJobState(ctx, logger, job, "have messages", m.areThereMessages, timeout)
9494
}
9595

@@ -100,6 +100,25 @@ func (m *Manager) createMessagePaginator(ctx context.Context, job IAsynchronousJ
100100
return
101101
}
102102

103+
func (m *Manager) GetMessagePaginator(ctx context.Context, logger logs.Loggers, job IAsynchronousJob, timeout time.Duration) (messagePaginator pagination.IStreamPaginatorAndPageFetcher, err error) {
104+
err = parallelisation.DetermineContextError(ctx)
105+
if err != nil {
106+
return
107+
}
108+
subCtx, cancel := context.WithTimeout(ctx, timeout)
109+
defer cancel()
110+
err = m.waitForJobToStart(subCtx, logger, job, timeout)
111+
if err != nil {
112+
return
113+
}
114+
err = m.waitForJobToHaveMessagesAvailable(subCtx, logger, job, timeout)
115+
if err != nil {
116+
return
117+
}
118+
messagePaginator, err = m.createMessagePaginator(subCtx, job)
119+
return
120+
}
121+
103122
func (m *Manager) WaitForJobCompletion(ctx context.Context, job IAsynchronousJob) error {
104123
return m.WaitForJobCompletionWithTimeout(ctx, job, 5*time.Minute)
105124
}
@@ -120,15 +139,8 @@ func (m *Manager) WaitForJobCompletionWithTimeout(ctx context.Context, job IAsyn
120139
}()
121140
subCtx, cancel := context.WithTimeout(ctx, timeout)
122141
defer cancel()
123-
err = m.waitForJobToStart(subCtx, messageLogger, job, timeout)
124-
if err != nil {
125-
return
126-
}
127-
err = m.waitForJobToHaveMessagesAvailable(subCtx, messageLogger, job, timeout)
128-
if err != nil {
129-
return
130-
}
131-
messagePaginator, err := m.createMessagePaginator(subCtx, job)
142+
143+
messagePaginator, err := m.GetMessagePaginator(subCtx, messageLogger, job, timeout)
132144
if err != nil {
133145
return
134146
}
@@ -154,6 +166,45 @@ func (m *Manager) WaitForJobCompletionWithTimeout(ctx context.Context, job IAsyn
154166
return
155167
}
156168

169+
func (m *Manager) LogJobMessagesUntilNow(ctx context.Context, job IAsynchronousJob, timeout time.Duration) (err error) {
170+
err = parallelisation.DetermineContextError(ctx)
171+
if err != nil {
172+
return
173+
}
174+
messageLogger, err := m.messageLoggerFactory.Create(ctx)
175+
if err != nil {
176+
return
177+
}
178+
defer func() {
179+
if messageLogger != nil {
180+
_ = messageLogger.Close()
181+
}
182+
}()
183+
subCtx, cancel := context.WithTimeout(ctx, timeout)
184+
defer cancel()
185+
186+
messagePaginator, err := m.GetMessagePaginator(subCtx, messageLogger, job, timeout)
187+
if err != nil {
188+
return
189+
}
190+
defer func() {
191+
if messagePaginator != nil {
192+
_ = messagePaginator.Close()
193+
}
194+
}()
195+
196+
err = messagePaginator.DryUp()
197+
if err != nil {
198+
messageLogger.LogError(err)
199+
return
200+
}
201+
err = messageLogger.LogMessagesCollection(subCtx, messagePaginator)
202+
if err != nil {
203+
messageLogger.LogError(err)
204+
}
205+
return
206+
}
207+
157208
func (m *Manager) checkForMessageStreamExhaustion(ctx context.Context, paginator pagination.IGenericStreamPaginator, job IAsynchronousJob) error {
158209

159210
for {

utils/job/manager_test.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ package job
88
import (
99
"context"
1010
"fmt"
11-
"math/rand"
1211
"net/http"
1312
"net/http/httptest"
1413
"testing"
1514
"time"
1615

16+
"github.com/go-faker/faker/v4"
1717
"github.com/stretchr/testify/assert"
1818
"github.com/stretchr/testify/require"
1919
"go.uber.org/goleak"
@@ -147,6 +147,56 @@ func mapFunc(f func() (*jobtest.MockAsynchronousJob, error)) func() (IAsynchrono
147147
}
148148
}
149149

150+
func TestManager_logMessages(t *testing.T) {
151+
defer goleak.VerifyNone(t)
152+
tests := []struct {
153+
jobFunc func() (IAsynchronousJob, error)
154+
expectedError []error
155+
timeout *time.Duration
156+
}{
157+
{
158+
jobFunc: mapFunc(jobtest.NewMockFailedAsynchronousJob),
159+
expectedError: nil,
160+
},
161+
{
162+
jobFunc: mapFunc(jobtest.NewMockQueuedAsynchronousJob),
163+
expectedError: []error{commonerrors.ErrCondition, commonerrors.ErrTimeout, commonerrors.ErrCancelled},
164+
timeout: field.ToOptionalDuration(500 * time.Millisecond),
165+
},
166+
{
167+
jobFunc: mapFunc(jobtest.NewMockSuccessfulAsynchronousJob),
168+
expectedError: nil,
169+
},
170+
}
171+
for i := range tests {
172+
test := tests[i]
173+
174+
t.Run(fmt.Sprintf("#%v", i), func(t *testing.T) {
175+
// t.Parallel()
176+
logger, err := logging.NewStandardClientLogger(fmt.Sprintf("test #%v", i), nil)
177+
require.NoError(t, err)
178+
loggerF := messages.NewMessageLoggerFactory(logger, false, time.Nanosecond)
179+
job, err := test.jobFunc()
180+
runOut := time.Nanosecond
181+
factory, err := newMockJobManager(loggerF, time.Nanosecond, &runOut, job, err)
182+
183+
require.NoError(t, err)
184+
require.NotNil(t, factory)
185+
if test.timeout == nil {
186+
err = factory.LogJobMessagesUntilNow(context.TODO(), job, 5*time.Minute)
187+
} else {
188+
err = factory.LogJobMessagesUntilNow(context.TODO(), job, *test.timeout)
189+
}
190+
if test.expectedError == nil {
191+
assert.NoError(t, err)
192+
} else {
193+
assert.Error(t, err)
194+
errortest.AssertError(t, err, test.expectedError...)
195+
}
196+
})
197+
}
198+
}
199+
150200
func TestManager_WaitForJobCompletion(t *testing.T) {
151201
defer goleak.VerifyNone(t)
152202
tests := []struct {
@@ -229,13 +279,17 @@ func TestManager_WaitForJobCompletionTimeout(t *testing.T) {
229279

230280
err = factory.WaitForJobCompletionWithTimeout(context.TODO(), job, time.Nanosecond)
231281
assert.Error(t, err)
232-
errortest.AssertError(t, err, commonerrors.ErrTimeout, commonerrors.ErrCancelled, commonerrors.ErrCondition)
282+
errortest.AssertError(t, err, commonerrors.ErrInvalid, commonerrors.ErrTimeout, commonerrors.ErrCancelled, commonerrors.ErrCondition)
233283
})
234284
}
235285
}
236286

237287
func newMockJobManager(logger *messages.MessageLoggerFactory, backOffPeriod time.Duration, messagePaginatorRunOutTimeout *time.Duration, job IAsynchronousJob, errToReturn error) (*Manager, error) {
238-
pageNumber := rand.Intn(50) //nolint:gosec //causes G404: Use of weak random number generator
288+
n, err := faker.RandomInt(1, 50)
289+
if err != nil {
290+
return nil, err
291+
}
292+
pageNumber := n[0]
239293
messageStream := messages.NewMockMessagePaginatorFactory(pageNumber)
240294
if messagePaginatorRunOutTimeout != nil {
241295
messageStream = messageStream.UpdateRunOutTimeout(*messagePaginatorRunOutTimeout)

0 commit comments

Comments
 (0)