Skip to content

Commit 838e64f

Browse files
committed
Add callbackHandle.cancel unittest
1 parent 479f2cc commit 838e64f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/asynchronous/events.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ if (isDelegate!Dg)
8383
}
8484
}
8585

86+
/**
87+
* Cancel the call. If the callback is already canceled or executed, this
88+
* method has no effect.
89+
*/
8690
override void cancel()
8791
{
8892
this.cancelled = true;
93+
this.dg = null;
94+
this.args = Args.init;
8995
}
9096

9197
protected override void opCallImpl()

test/events.d

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ unittest
1717
assert(fooCalled);
1818
}
1919

20+
unittest
21+
{
22+
auto loop = getEventLoop;
23+
int bar = 0;
24+
25+
void foo(int i)
26+
{
27+
bar = i;
28+
loop.stop;
29+
}
30+
31+
loop.callSoon(&foo, 10);
32+
assert(bar == 0);
33+
loop.runForever;
34+
assert(bar == 10);
35+
}
36+
37+
unittest
38+
{
39+
auto loop = getEventLoop;
40+
int bar = 0;
41+
42+
void foo(int i)
43+
{
44+
bar += i;
45+
loop.stop;
46+
}
47+
48+
auto h1 = loop.callSoon(&foo, 10);
49+
auto h2 = loop.callSoon(&foo, 15);
50+
assert(bar == 0);
51+
h1.cancel;
52+
loop.runForever;
53+
assert(bar == 15);
54+
}
55+
2056
unittest
2157
{
2258
import std.datetime : msecs;

0 commit comments

Comments
 (0)