Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
f46ac54c
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f46ac54c
编写于
7月 30, 2013
作者:
P
psandoz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
Reviewed-by: chegar
上级
c5c0273a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
38 addition
and
107 deletion
+38
-107
test/java/util/Random/RandomStreamTest.java
test/java/util/Random/RandomStreamTest.java
+38
-107
未找到文件。
test/java/util/Random/RandomStreamTest.java
浏览文件 @
f46ac54c
...
...
@@ -25,23 +25,22 @@ import org.testng.annotations.DataProvider;
import
org.testng.annotations.Test
;
import
java.security.SecureRandom
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.ArrayList
;
import
java.util.
Random
;
import
java.util.
List
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ExecutorService
;
import
java.util.Random
;
import
java.util.Set
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ThreadLocalRandom
;
import
java.util.concurrent.TimeUnit
;
import
java.util.stream.IntStream
;
import
java.util.stream.LongStream
;
import
java.util.stream.DoubleStream
;
import
java.util.concurrent.TimeoutException
;
import
java.util.function.Supplier
;
import
java.util.stream.Stream
;
import
static
org
.
testng
.
Assert
.
assertEquals
;
import
static
org
.
testng
.
Assert
.
assertFalse
;
import
static
org
.
testng
.
Assert
.
assertTrue
;
import
static
java
.
util
.
stream
.
Collectors
.
toList
;
import
static
java
.
util
.
stream
.
Collectors
.
toSet
;
import
static
org
.
testng
.
Assert
.
*
;
/**
* @test
...
...
@@ -147,111 +146,43 @@ public class RandomStreamTest {
}
@Test
public
void
testThreadLocalIntStream
()
throws
InterruptedException
{
final
ExecutorService
e
=
Executors
.
newFixedThreadPool
(
10
);
final
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
final
class
RandomTask
implements
Runnable
{
int
[]
randoms
;
@Override
public
void
run
()
{
randoms
=
tlr
.
ints
().
limit
(
SIZE
).
toArray
();
}
}
final
RandomTask
[]
tasks
=
new
RandomTask
[
10
];
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
tasks
[
i
]
=
new
RandomTask
();
}
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
e
.
submit
(
tasks
[
i
]);
}
e
.
shutdown
();
e
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
);
for
(
int
i
=
1
;
i
<
tasks
.
length
;
i
++)
{
assertFalse
(
Arrays
.
equals
(
tasks
[
0
].
randoms
,
tasks
[
i
].
randoms
));
}
public
void
testThreadLocalIntStream
()
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
testRandomResultSupplierConcurrently
(()
->
tlr
.
ints
().
limit
(
SIZE
).
boxed
().
collect
(
toList
()));
}
@Test
public
void
testThreadLocalLongStream
()
throws
InterruptedException
{
final
ExecutorService
e
=
Executors
.
newFixedThreadPool
(
10
);
final
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
final
class
RandomTask
implements
Runnable
{
long
[]
randoms
;
@Override
public
void
run
()
{
randoms
=
tlr
.
longs
().
limit
(
SIZE
).
toArray
();
}
}
final
RandomTask
[]
tasks
=
new
RandomTask
[
10
];
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
tasks
[
i
]
=
new
RandomTask
();
}
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
e
.
submit
(
tasks
[
i
]);
}
e
.
shutdown
();
e
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
);
for
(
int
i
=
1
;
i
<
tasks
.
length
;
i
++)
{
assertFalse
(
Arrays
.
equals
(
tasks
[
0
].
randoms
,
tasks
[
i
].
randoms
));
}
public
void
testThreadLocalLongStream
()
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
testRandomResultSupplierConcurrently
(()
->
tlr
.
longs
().
limit
(
SIZE
).
boxed
().
collect
(
toList
()));
}
@Test
public
void
testThreadLocalDoubleStream
()
throws
InterruptedException
{
final
ExecutorService
e
=
Executors
.
newFixedThreadPool
(
10
);
final
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
final
class
RandomTask
implements
Runnable
{
double
[]
randoms
;
@Override
public
void
run
()
{
randoms
=
tlr
.
doubles
().
limit
(
SIZE
).
toArray
();
}
}
final
RandomTask
[]
tasks
=
new
RandomTask
[
10
];
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
tasks
[
i
]
=
new
RandomTask
();
}
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
e
.
submit
(
tasks
[
i
]);
}
e
.
shutdown
();
e
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
);
for
(
int
i
=
1
;
i
<
tasks
.
length
;
i
++)
{
assertFalse
(
Arrays
.
equals
(
tasks
[
0
].
randoms
,
tasks
[
i
].
randoms
));
}
public
void
testThreadLocalDoubleStream
()
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
testRandomResultSupplierConcurrently
(()
->
tlr
.
doubles
().
limit
(
SIZE
).
boxed
().
collect
(
toList
()));
}
@Test
public
void
testThreadLocalGaussianStream
()
throws
InterruptedException
{
final
ExecutorService
e
=
Executors
.
newFixedThreadPool
(
10
);
final
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
final
class
RandomTask
implements
Runnable
{
double
[]
randoms
;
@Override
public
void
run
()
{
randoms
=
tlr
.
gaussians
().
limit
(
SIZE
).
toArray
();
}
}
final
RandomTask
[]
tasks
=
new
RandomTask
[
10
];
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
tasks
[
i
]
=
new
RandomTask
();
}
for
(
int
i
=
0
;
i
<
tasks
.
length
;
i
++)
{
e
.
submit
(
tasks
[
i
]);
}
e
.
shutdown
();
e
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
);
for
(
int
i
=
1
;
i
<
tasks
.
length
;
i
++)
{
assertFalse
(
Arrays
.
equals
(
tasks
[
0
].
randoms
,
tasks
[
i
].
randoms
));
}
public
void
testThreadLocalGaussianStream
()
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
ThreadLocalRandom
tlr
=
ThreadLocalRandom
.
current
();
testRandomResultSupplierConcurrently
(()
->
tlr
.
gaussians
().
limit
(
SIZE
).
boxed
().
collect
(
toList
()));
}
<
T
>
void
testRandomResultSupplierConcurrently
(
Supplier
<
T
>
s
)
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
// Produce 10 completable future tasks
final
int
tasks
=
10
;
List
<
CompletableFuture
<
T
>>
cfs
=
Stream
.
generate
(()
->
CompletableFuture
.
supplyAsync
(
s
)).
limit
(
tasks
).
collect
(
toList
());
// Wait for all tasks to complete
// Timeout is beyond reasonable doubt that completion should
// have occurred unless there is an issue
CompletableFuture
<
Void
>
all
=
CompletableFuture
.
allOf
(
cfs
.
stream
().
toArray
(
CompletableFuture
[]::
new
));
all
.
get
(
1
,
TimeUnit
.
MINUTES
);
// Count the distinct results, which should equal the number of tasks
long
rc
=
cfs
.
stream
().
map
(
CompletableFuture:
:
join
).
distinct
().
count
();
assertEquals
(
rc
,
tasks
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录