16
16
17
17
package org .springframework .core .task ;
18
18
19
+ import java .util .concurrent .atomic .AtomicInteger ;
20
+
19
21
import org .junit .jupiter .api .Test ;
20
22
21
23
import static org .assertj .core .api .Assertions .assertThat ;
@@ -34,6 +36,7 @@ void virtualThreadsWithoutName() {
34
36
executeAndWait (executor , task , monitor );
35
37
assertThat (task .getThreadName ()).isEmpty ();
36
38
assertThat (task .isVirtual ()).isTrue ();
39
+ assertThat (task .runtCount ()).isOne ();
37
40
}
38
41
39
42
@ Test
@@ -44,6 +47,7 @@ void virtualThreadsWithNamePrefix() {
44
47
executeAndWait (executor , task , monitor );
45
48
assertThat (task .getThreadName ()).isEqualTo ("test-0" );
46
49
assertThat (task .isVirtual ()).isTrue ();
50
+ assertThat (task .runtCount ()).isOne ();
47
51
}
48
52
49
53
@ Test
@@ -54,6 +58,7 @@ void simpleWithVirtualThreadFactory() {
54
58
executeAndWait (executor , task , monitor );
55
59
assertThat (task .getThreadName ()).isEqualTo ("test" );
56
60
assertThat (task .isVirtual ()).isTrue ();
61
+ assertThat (task .runtCount ()).isOne ();
57
62
}
58
63
59
64
@ Test
@@ -66,6 +71,7 @@ void simpleWithVirtualThreadFlag() {
66
71
executeAndWait (executor , task , monitor );
67
72
assertThat (task .getThreadName ()).startsWith (customPrefix );
68
73
assertThat (task .isVirtual ()).isTrue ();
74
+ assertThat (task .runtCount ()).isOne ();
69
75
}
70
76
71
77
private void executeAndWait (TaskExecutor executor , Runnable task , Object monitor ) {
@@ -115,6 +121,8 @@ public final void run() {
115
121
116
122
private static final class ThreadNameHarvester extends AbstractNotifyingRunnable {
117
123
124
+ private final AtomicInteger runCount = new AtomicInteger ();
125
+
118
126
private String threadName ;
119
127
120
128
private boolean virtual ;
@@ -131,11 +139,16 @@ public boolean isVirtual() {
131
139
return this .virtual ;
132
140
}
133
141
142
+ public int runtCount () {
143
+ return this .runCount .get ();
144
+ }
145
+
134
146
@ Override
135
147
protected void doRun () {
136
148
Thread thread = Thread .currentThread ();
137
149
this .threadName = thread .getName ();
138
150
this .virtual = thread .isVirtual ();
151
+ runCount .incrementAndGet ();
139
152
}
140
153
}
141
154
0 commit comments