Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
63beee72
Unverified
Commit
63beee72
authored
Aug 25, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-challenger: Simplify worker to a single function.
parent
853da7d0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
49 deletions
+12
-49
worker.go
op-challenger/fault/scheduler/worker.go
+4
-23
worker_test.go
op-challenger/fault/scheduler/worker_test.go
+8
-26
No files found.
op-challenger/fault/scheduler/worker.go
View file @
63beee72
...
...
@@ -5,34 +5,15 @@ import (
"sync"
)
type
worker
struct
{
in
<-
chan
job
out
chan
<-
job
cancel
func
()
wg
sync
.
WaitGroup
}
func
(
w
*
worker
)
Start
(
ctx
context
.
Context
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
w
.
cancel
=
cancel
w
.
wg
.
Add
(
1
)
go
w
.
loop
(
ctx
)
}
func
(
w
*
worker
)
Stop
()
{
w
.
cancel
()
w
.
wg
.
Wait
()
}
func
(
w
*
worker
)
loop
(
ctx
context
.
Context
)
{
defer
w
.
wg
.
Done
()
func
runWorker
(
ctx
context
.
Context
,
in
<-
chan
job
,
out
chan
<-
job
,
wg
*
sync
.
WaitGroup
)
{
defer
wg
.
Done
()
for
{
select
{
case
<-
ctx
.
Done
()
:
return
case
j
:=
<-
w
.
in
:
case
j
:=
<-
in
:
j
.
resolved
=
j
.
player
.
ProgressGame
(
ctx
)
w
.
out
<-
j
out
<-
j
}
}
}
op-challenger/fault/scheduler/worker_test.go
View file @
63beee72
...
...
@@ -2,20 +2,21 @@ package scheduler
import
(
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func
Test
ShouldProcessJobs
(
t
*
testing
.
T
)
{
func
Test
WorkerShouldProcessJobsUntilContextDone
(
t
*
testing
.
T
)
{
in
:=
make
(
chan
job
,
2
)
out
:=
make
(
chan
job
,
2
)
w
:=
&
worker
{
in
:
in
,
out
:
out
,
}
w
.
Start
(
context
.
Background
()
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
var
wg
sync
.
WaitGroup
go
runWorker
(
ctx
,
in
,
out
,
&
wg
)
in
<-
job
{
player
:
&
stubPlayer
{
done
:
false
},
...
...
@@ -29,29 +30,10 @@ func TestShouldProcessJobs(t *testing.T) {
require
.
Equal
(
t
,
result1
.
resolved
,
false
)
require
.
Equal
(
t
,
result2
.
resolved
,
true
)
defer
w
.
Stop
()
}
func
TestWorkerStopsWhenContextDone
(
t
*
testing
.
T
)
{
in
:=
make
(
chan
job
,
2
)
out
:=
make
(
chan
job
,
2
)
w
:=
&
worker
{
in
:
in
,
out
:
out
,
}
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
w
.
Start
(
ctx
)
// Make sure the worker is up and running
in
<-
job
{
player
:
&
stubPlayer
{
done
:
false
},
}
readWithTimeout
(
t
,
out
)
// Cancel the context which should exit the worker
cancel
()
w
.
w
g
.
Wait
()
wg
.
Wait
()
}
type
stubPlayer
struct
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment