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
853da7d0
Unverified
Commit
853da7d0
authored
Aug 25, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-challenger: Define worker for parallel scheduling
parent
16b00d32
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
0 deletions
+126
-0
types.go
op-challenger/fault/scheduler/types.go
+12
-0
worker.go
op-challenger/fault/scheduler/worker.go
+38
-0
worker_test.go
op-challenger/fault/scheduler/worker_test.go
+76
-0
No files found.
op-challenger/fault/scheduler/types.go
0 → 100644
View file @
853da7d0
package
scheduler
import
"context"
type
GamePlayer
interface
{
ProgressGame
(
ctx
context
.
Context
)
bool
}
type
job
struct
{
player
GamePlayer
resolved
bool
}
op-challenger/fault/scheduler/worker.go
0 → 100644
View file @
853da7d0
package
scheduler
import
(
"context"
"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
()
for
{
select
{
case
<-
ctx
.
Done
()
:
return
case
j
:=
<-
w
.
in
:
j
.
resolved
=
j
.
player
.
ProgressGame
(
ctx
)
w
.
out
<-
j
}
}
}
op-challenger/fault/scheduler/worker_test.go
0 → 100644
View file @
853da7d0
package
scheduler
import
(
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func
TestShouldProcessJobs
(
t
*
testing
.
T
)
{
in
:=
make
(
chan
job
,
2
)
out
:=
make
(
chan
job
,
2
)
w
:=
&
worker
{
in
:
in
,
out
:
out
,
}
w
.
Start
(
context
.
Background
())
in
<-
job
{
player
:
&
stubPlayer
{
done
:
false
},
}
in
<-
job
{
player
:
&
stubPlayer
{
done
:
true
},
}
result1
:=
readWithTimeout
(
t
,
out
)
result2
:=
readWithTimeout
(
t
,
out
)
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
.
wg
.
Wait
()
}
type
stubPlayer
struct
{
done
bool
}
func
(
s
*
stubPlayer
)
ProgressGame
(
ctx
context
.
Context
)
bool
{
return
s
.
done
}
func
readWithTimeout
[
T
any
](
t
*
testing
.
T
,
ch
<-
chan
T
)
T
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
defer
cancel
()
select
{
case
<-
ctx
.
Done
()
:
var
val
T
t
.
Fatal
(
"Did not receive event from channel"
)
return
val
// Won't be reached but makes the compiler happy
case
val
:=
<-
ch
:
return
val
}
}
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