/
CoroutTest.cs
361 lines (296 loc) · 11.8 KB
/
CoroutTest.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections;
using BLK10.Iterator;
using BLK10.Report;
using UnityEditor;
using UnityEngine;
using NUnit.Framework;
namespace BLK10.Iterator.Test
{
[TestFixture]
[Category("Corout Test")]
public class CoroutTest
{
[Test]
public void Simple()
{
var x = 7.525f;
var y = x * x;
Corout.Create<float>(co => Routines.GetFloatSquareRoot(x, co))
.OnSucceed(co =>
{
Assert.AreEqual(co.Result, y);
Assert.IsTrue(co.Succeeded);
})
.Start();
}
[Test]
public void StepAndSecond()
{
int startStep = Scheduler.Instance.Step;
int step = 6;
Corout.Create(Routines.NStep(step))
.OnSucceed(co => Assert.AreEqual(Scheduler.Instance.Step, startStep + step))
.Start();
float startSecond = Scheduler.Instance.Second;
float second = 0.5f;
Corout.Create(Routines.NSecond(second))
.OnSucceed(co => Assert.GreaterOrEqual(Scheduler.Instance.Second, startSecond + second))
.Start();
}
[Test]
public void CatchThrowFinally()
{
var x = 42;
var y = x * x;
Corout.Create<int>(co => Routines.Failing(x, co))
.OnCatch<NotSupportedException>((ex, co) =>
{
Assert.IsInstanceOf<NotSupportedException>(ex);
Debug.Log("Catch: 'NotSupportedException'.");
})
.OnFinally(co =>
{
Assert.IsTrue(co.Faulted);
Debug.Log("Finally do some clean-up...");
})
.Start();
Corout.Create<int>(co => Routines.GetIntSquareRoot(x, co))
.OnFinally(co =>
{
Debug.Log("'OnFinally' doesn't need 'OnCatch' and are always executed.");
Assert.AreEqual(co.Result, y);
Assert.IsTrue(co.Succeeded);
})
.Start();
Corout.Create<int>(co => Routines.Failing(x, co))
.OnCatch<NotSupportedException>((ex, co) =>
{
Assert.IsInstanceOf<NotSupportedException>(ex);
Assert.IsTrue(co.Faulted);
throw ex;
})
.OnFinally(co =>
{
Assert.IsTrue(co.Faulted);
Debug.Log("Even if you throw exception 'OnFinally' is executed.");
})
.Start();
}
[Test]
public void Cancel()
{
var x = 7.525f;
Corout.Create<float>((co, tk) => Routines.Cancelable(x, co, tk))
.OnSucceed(co => Debug.Log("OnSucceed."))
.OnCancel(co => Debug.Log("Corout canceled."))
.Start();
var second = 1.15f;
Corout.Create<float>((co, tk) => Routines.CancelableAfter(second, x, co, tk))
.OnCancel(co => Debug.Log(string.Format("Corout canceled after {0} second", second)))
.OnSucceed(co => Debug.Log("OnSucceed."))
.Start();
var step = 15;
Corout.Create<float>((co, tk) => Routines.CancelableAfter(step, x, co, tk))
.OnSucceed(co => Debug.Log("'OnSucceed' never call."))
.OnCancel(co => Debug.Log(string.Format("Corout canceled after {0} step", step)))
.Start();
}
[Test]
public void Abort()
{
var step = 100;
var corout = Corout.Create(Routines.NStepVerbose(step))
.OnSucceed(co => Debug.Log(string.Format("corout complete in {0} steps", step)))
.OnCancel( co => Debug.Log("'OnCancel' never called"))
.OnFinally(co => Debug.Log("'OnFinally'."));
corout.Start();
Corout.WaitThen(10, () =>
{
corout.Abort();
Debug.Log("Aborted");
});
// OR
/*
Corout.WaitThen(0.5f, () =>
{
corout.Abort();
Debug.Log("Aborted");
});
*/
}
[Test]
public void Fence()
{
var first = 10;
var second = 05;
var third = 03;
int startStep = Scheduler.Instance.Step;
using (Corout.OpenFence(() => Debug.Log("fenced corout ends...")))
{
Corout.Create(() => Routines.NStepVerbose(first))
.OnSucceed(co =>
{
Debug.Log(string.Format("first corout complete in {0} steps", first));
Assert.AreEqual(Scheduler.Instance.Step, startStep + first);
})
.Start();
Corout.Create(() => Routines.NStepVerbose(second))
.OnSucceed(co =>
{
Debug.Log(string.Format("second corout Complete in {0} steps", second));
Assert.AreEqual(Scheduler.Instance.Step, startStep + second);
})
.Start();
}
Corout.Create(() => Routines.NStepVerbose(third))
.OnSucceed(co =>
{
Debug.Log(string.Format("third corout complete in {0} steps", third));
Assert.AreEqual(Scheduler.Instance.Step, startStep + Math.Max(first, second) + third + 1); // 1 step for fence callback
})
.Start();
// OR
/*
var fence01 = Corout.OpenFence(() => Debug.Log("fenced corout ends..."));
Corout.Create(() => Routines.NStepVerbose(first))
.OnComplete(co =>
{
Debug.Log(string.Format("first corout complete in {0} steps", first));
Assert.AreEqual(Corout.CurrentStep, startStep + first);
})
.Start();
Corout.Create(() => Routines.NStepVerbose(second))
.OnComplete(co =>
{
Debug.Log(string.Format("second corout Complete in {0} steps", second));
Assert.AreEqual(Corout.CurrentStep, startStep + second);
})
.Start();
fence01.Close();
Corout.Create(() => Routines.NStepVerbose(third))
.OnComplete(co =>
{
Debug.Log(string.Format("third corout complete in {0} steps", third));
Assert.AreEqual(Corout.CurrentStep, startStep + Math.Max(first, second) + third + 1); // 1 step for fence callback
})
.Start();
*/
}
[Test]
public void ContinueSimple()
{
var x = 7.525f;
var y = x * x;
var s = y.ToString();
int l = s.Length;
Corout.Create<float>(co => Routines.GetFloatSquareRoot(x, co))
.OnSucceed(co => Assert.AreEqual(co.Result, y))
.ContinueWith<string>(Routines.GetFloatString)
.OnSucceed(co => Assert.AreEqual(co.Result, s))
.ContinueWith<int>(Routines.GetLength)
.OnSucceed(co => Assert.AreEqual(co.Result, l))
.Start();
}
[Test]
public void Interlace()
{
var x = 3;
var y = 4.57f;
var z = 1425.67f;
var xx = x * x;
var yy = y * y;
var zz = z.ToString();
var cw = string.Join(" ", new string[] { xx.ToString(), yy.ToString(), zz });
Corout.WhenAll(() => Routines.NStepVerbose(5),
() => Routines.NStepVerbose(10),
() => Routines.NStepVerbose(15))
.OnSucceed(co =>
{
Debug.Log("OnSucceed");
})
.Start();
Corout.WhenAll<int, float, string>(co => Routines.GetIntSquareRoot(x, co),
co => Routines.GetFloatSquareRoot(y, co),
co => Routines.GetFloatString(z, co))
.OnSucceed(co =>
{
Debug.Log(co.Result.Item1);
Debug.Log(co.Result.Item2);
Debug.Log(co.Result.Item3);
})
.ContinueWith<string>(Routines.JoinString)
.OnSucceed(co =>
{
Debug.Log(co.Result);
Assert.AreEqual(cw, co.Result);
})
.Start();
}
[Test]
public void Error02()
{
int x = 15;
var y = x * x;
var s = y.ToString();
int l = s.Length;
Corout.Create<int>(co => Routines.Failing(x, co))
.OnCatch<NotSupportedException>((ex, co) =>
{
Debug.Log("Catch error.");
co.Abort();
})
.OnSucceed(co =>
{
Debug.Log("first on succeed.");
Assert.AreEqual(co.Result, y);
})
.ContinueWith<string>(Routines.GetIntString)
.OnSucceed(co =>
{
Debug.Log("second on succeed should not happen.");
Assert.AreEqual(co.Result, s);
})
.ContinueWith<int>(Routines.GetLength)
.OnSucceed(co =>
{
Debug.Log("third on succeed should not happen.");
Assert.AreEqual(co.Result, l);
})
.Start();
}
[Test]
public void InterlaceError()
{
var x = 3;
var y = 4.57f;
var z = 1425.67f;
var xx = x * x;
var yy = y * y;
var zz = z.ToString();
var cw = string.Join(" ", new string[] { xx.ToString(), yy.ToString(), zz });
Corout.WhenAll<int, float, string>(ac => Routines.Failing(x, ac),
ac => Routines.GetFloatSquareRoot(y, ac),
ac => Routines.GetFloatString(z, ac))
.OnCatch<Exception>((ex, co) =>
{
Debug.Log("catch error");
//throw ex;
co.Abort();
})
.ContinueWith(() => Routines.NStepVerbose(10))
//.ContinueWith<string>(Routines.JoinString)
//.OnComplete(co =>
//{
//Debug.Log(co.Result);
//Assert.AreEqual(cw, co.Result);
//})
.Start();
}
}
[TestFixture]
[Category("Command Test")]
public class CommandTest
{
}
}