Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
ee0c8803
W
wechaty
项目概览
.www.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ee0c8803
编写于
8月 16, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
try to fix get port race condition unit test fail (no idea why yet)
上级
cbbaccd6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
10 addition
and
10 deletion
+10
-10
src/helper-functions/impure/get-port.spec.ts
src/helper-functions/impure/get-port.spec.ts
+3
-5
src/helper-functions/impure/get-port.ts
src/helper-functions/impure/get-port.ts
+7
-5
未找到文件。
src/helper-functions/impure/get-port.spec.ts
浏览文件 @
ee0c8803
...
...
@@ -20,16 +20,14 @@
// tslint:disable:no-shadowed-variable
import
test
from
'
blue-tape
'
// import http from 'http'
// import express from 'express'
import
net
from
'
net
'
import
{
getPort
}
from
'
./get-port
'
test
(
'
getPort() for an available socket port
'
,
async
t
=>
{
const
PORT
=
8788
//
const PORT = 8788
let
port
=
await
getPort
(
PORT
)
let
port
=
await
getPort
()
let
ttl
=
17
const
serverList
=
[]
...
...
@@ -42,7 +40,7 @@ test('getPort() for an available socket port', async t => {
server
.
listen
(
port
,
'
127.0.0.1
'
)
serverList
.
push
(
server
)
port
=
await
getPort
(
PORT
)
port
=
await
getPort
()
}
catch
(
e
)
{
t
.
fail
(
'
should not exception:
'
+
e
.
message
+
'
,
'
+
e
.
stack
)
...
...
src/helper-functions/impure/get-port.ts
浏览文件 @
ee0c8803
import
net
from
'
net
'
const
DEFAULT_PORT
=
38788
/**
*
* @param port is just a suggestion.
...
...
@@ -11,8 +13,8 @@ import net from 'net'
* const DEFAULT_IANA_RANGE = {min: 49152, max: 65535}
*
*/
export
function
getPort
(
port
:
number
):
Promise
<
number
>
{
let
tryPort
=
nextPort
(
port
||
38788
)
export
function
getPort
(
port
=
DEFAULT_PORT
):
Promise
<
number
>
{
let
tryPort
=
nextPort
(
port
)
return
new
Promise
(
resolve
=>
{
// https://gist.github.com/mikeal/1840641
...
...
@@ -39,13 +41,13 @@ export function getPort (port: number): Promise<number> {
function
nextPort
(
currentPort
:
number
):
number
{
const
RANGE
=
1733
// do not use Math.random() here, because AVA will fork, then here will get the same random number, cause a race condition for socket listen
// const n = Math.floor(Math.random() * BETWEEN_
RANGE)
const
n
=
Math
.
floor
(
Math
.
random
()
*
RANGE
)
/**
* nano seconds from node: http://stackoverflow.com/a/18197438/1123955
*/
const
[,
nanoSeed
]
=
process
.
hrtime
()
const
n
=
1
+
nanoSeed
%
RANGE
// +1 to prevent same port
//
const [, nanoSeed] = process.hrtime()
//
const n = 1 + nanoSeed % RANGE // +1 to prevent same port
if
(
currentPort
+
n
>
65000
)
{
return
currentPort
+
n
-
RANGE
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录