-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchTest.cs
137 lines (115 loc) · 3.55 KB
/
BatchTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using revecs.Core;
using revecs.Core.Boards;
using revtask.Core;
using revtask.Helpers;
using revtask.OpportunistJobRunner;
using Xunit;
using Xunit.Abstractions;
namespace revecs.Tests;
public partial class BatchTest : TestBase
{
struct EmptyJob : IJob
{
public int SetupJob(JobSetupInfo info)
{
return 1;
}
public void Execute(IJobRunner runner, JobExecuteInfo info)
{
// fake work
Thread.Sleep(5);
}
}
record struct WaitJob(JobRequest ParentJob) : IJob
{
public int SetupJob(JobSetupInfo info)
{
return 1;
}
public void Execute(IJobRunner runner, JobExecuteInfo info)
{
runner.CompleteBatch(ParentJob);
}
}
[Fact]
public void TestNonDeadlock()
{
using var runner = new OpportunistJobRunner(1f);
var list = new List<JobRequest>();
for (var i = 0; i < 100; i++)
{
var empty = new EmptyJob();
var wait = new WaitJob(runner.Queue(new WaitJob(runner.Queue(empty))));
list.Add(runner.Queue(wait));
}
var final = runner.WaitBatches(list);
runner.CompleteBatch(final);
}
struct MultipleIndex : IJob
{
public int SetupJob(JobSetupInfo info)
{
return 2;
}
public void Execute(IJobRunner runner, JobExecuteInfo info)
{
Console.WriteLine($"{info.Index}; {info.MaxUseIndex}");
}
}
[Fact]
public void TestMultipleIndex()
{
using var runner = new OpportunistJobRunner(0.5f);
foreach (var _ in Enumerable.Range(0, 10))
{
var batches = new List<JobRequest>();
for (var i = 0; i < 100; i++)
batches.Add(runner.Queue(new MultipleIndex()));
runner.CompleteBatch(runner.WaitBatches(batches));
}
}
public BatchTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public void TestQuery()
{
using var runner = new OpportunistJobRunner(0.5f);
using var world = new RevolutionWorld();
world.AddBoard(nameof(BatchRunnerBoard), new BatchRunnerBoard(runner, world));
const int count = 100;
for (var i = 0; i < count; i++)
{
var ent = world.CreateEntity();
world.AddComponentA(ent, new ComponentA {Value = i});
world.AddComponentB(ent, new ComponentB {Value = i + 100});
}
world.ArchetypeUpdateBoard.Update();
var query = new MyQuery(world);
var hashsetA = new HashSet<int>();
var hashsetB = new HashSet<int>();
query.QueueAndComplete(runner, (_, entities) =>
{
foreach (var ent in entities)
{
output.WriteLine($"{ent.Handle} {ent.a.Value} {ent.b.Value}");
lock (this)
{
Assert.False(hashsetA.Contains(ent.a.Value), "hashsetA.Contains(ent.a.Value)");
Assert.False(hashsetB.Contains(ent.b.Value), "hashsetB.Contains(ent.b.Value)");
hashsetA.Add(ent.a.Value);
hashsetB.Add(ent.b.Value);
}
}
});
for (var i = 0; i < count; i++)
{
Assert.Contains(i, hashsetA);
Assert.Contains(i + 100, hashsetB);
}
output.WriteLine("ok?");
}
public partial struct MyQuery : IQuery<(Read<ComponentA> a, Read<ComponentB> b)>
{
}
}