Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
2949f3b6
Commit
2949f3b6
authored
Dec 08, 2011
by
David Symonds
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time: use Duration for AfterFunc.
R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/5465043
parent
f00340f0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
23 deletions
+22
-23
src/pkg/net/http/serve_test.go
src/pkg/net/http/serve_test.go
+9
-9
src/pkg/testing/testing.go
src/pkg/testing/testing.go
+1
-1
src/pkg/time/sleep.go
src/pkg/time/sleep.go
+3
-3
src/pkg/time/sleep_test.go
src/pkg/time/sleep_test.go
+9
-10
No files found.
src/pkg/net/http/serve_test.go
View file @
2949f3b6
...
@@ -361,7 +361,7 @@ func TestIdentityResponse(t *testing.T) {
...
@@ -361,7 +361,7 @@ func TestIdentityResponse(t *testing.T) {
// The ReadAll will hang for a failing test, so use a Timer to
// The ReadAll will hang for a failing test, so use a Timer to
// fail explicitly.
// fail explicitly.
goTimeout
(
t
,
2
e9
,
func
()
{
goTimeout
(
t
,
2
*
time
.
Second
,
func
()
{
got
,
_
:=
ioutil
.
ReadAll
(
conn
)
got
,
_
:=
ioutil
.
ReadAll
(
conn
)
expectedSuffix
:=
"
\r\n\r\n
too short"
expectedSuffix
:=
"
\r\n\r\n
too short"
if
!
strings
.
HasSuffix
(
string
(
got
),
expectedSuffix
)
{
if
!
strings
.
HasSuffix
(
string
(
got
),
expectedSuffix
)
{
...
@@ -395,7 +395,7 @@ func testTcpConnectionCloses(t *testing.T, req string, h Handler) {
...
@@ -395,7 +395,7 @@ func testTcpConnectionCloses(t *testing.T, req string, h Handler) {
success
:=
make
(
chan
bool
)
success
:=
make
(
chan
bool
)
go
func
()
{
go
func
()
{
select
{
select
{
case
<-
time
.
After
(
5
e9
)
:
case
<-
time
.
After
(
5
*
time
.
Second
)
:
t
.
Fatal
(
"body not closed after 5s"
)
t
.
Fatal
(
"body not closed after 5s"
)
case
<-
success
:
case
<-
success
:
}
}
...
@@ -546,7 +546,7 @@ func TestTLSHandshakeTimeout(t *testing.T) {
...
@@ -546,7 +546,7 @@ func TestTLSHandshakeTimeout(t *testing.T) {
t
.
Fatalf
(
"Dial: %v"
,
err
)
t
.
Fatalf
(
"Dial: %v"
,
err
)
}
}
defer
conn
.
Close
()
defer
conn
.
Close
()
goTimeout
(
t
,
10
e9
,
func
()
{
goTimeout
(
t
,
10
*
time
.
Second
,
func
()
{
var
buf
[
1
]
byte
var
buf
[
1
]
byte
n
,
err
:=
conn
.
Read
(
buf
[
:
])
n
,
err
:=
conn
.
Read
(
buf
[
:
])
if
err
==
nil
||
n
!=
0
{
if
err
==
nil
||
n
!=
0
{
...
@@ -576,7 +576,7 @@ func TestTLSServer(t *testing.T) {
...
@@ -576,7 +576,7 @@ func TestTLSServer(t *testing.T) {
t
.
Fatalf
(
"Dial: %v"
,
err
)
t
.
Fatalf
(
"Dial: %v"
,
err
)
}
}
defer
idleConn
.
Close
()
defer
idleConn
.
Close
()
goTimeout
(
t
,
10
e9
,
func
()
{
goTimeout
(
t
,
10
*
time
.
Second
,
func
()
{
if
!
strings
.
HasPrefix
(
ts
.
URL
,
"https://"
)
{
if
!
strings
.
HasPrefix
(
ts
.
URL
,
"https://"
)
{
t
.
Errorf
(
"expected test TLS server to start with https://, got %q"
,
ts
.
URL
)
t
.
Errorf
(
"expected test TLS server to start with https://, got %q"
,
ts
.
URL
)
return
return
...
@@ -925,7 +925,7 @@ func testHandlerPanic(t *testing.T, withHijack bool) {
...
@@ -925,7 +925,7 @@ func testHandlerPanic(t *testing.T, withHijack bool) {
select
{
select
{
case
<-
done
:
case
<-
done
:
return
return
case
<-
time
.
After
(
5
e9
)
:
case
<-
time
.
After
(
5
*
time
.
Second
)
:
t
.
Fatal
(
"expected server handler to log an error"
)
t
.
Fatal
(
"expected server handler to log an error"
)
}
}
}
}
...
@@ -1072,7 +1072,7 @@ func TestClientWriteShutdown(t *testing.T) {
...
@@ -1072,7 +1072,7 @@ func TestClientWriteShutdown(t *testing.T) {
}()
}()
select
{
select
{
case
<-
donec
:
case
<-
donec
:
case
<-
time
.
After
(
10
e9
)
:
case
<-
time
.
After
(
10
*
time
.
Second
)
:
t
.
Fatalf
(
"timeout"
)
t
.
Fatalf
(
"timeout"
)
}
}
}
}
...
@@ -1103,10 +1103,10 @@ func TestServerBufferedChunking(t *testing.T) {
...
@@ -1103,10 +1103,10 @@ func TestServerBufferedChunking(t *testing.T) {
}
}
// goTimeout runs f, failing t if f takes more than ns to complete.
// goTimeout runs f, failing t if f takes more than ns to complete.
func
goTimeout
(
t
*
testing
.
T
,
ns
int64
,
f
func
())
{
func
goTimeout
(
t
*
testing
.
T
,
d
time
.
Duration
,
f
func
())
{
ch
:=
make
(
chan
bool
,
2
)
ch
:=
make
(
chan
bool
,
2
)
timer
:=
time
.
AfterFunc
(
ns
,
func
()
{
timer
:=
time
.
AfterFunc
(
d
,
func
()
{
t
.
Errorf
(
"Timeout expired after %
d ns"
,
ns
)
t
.
Errorf
(
"Timeout expired after %
v"
,
d
)
ch
<-
true
ch
<-
true
})
})
defer
timer
.
Stop
()
defer
timer
.
Stop
()
...
...
src/pkg/testing/testing.go
View file @
2949f3b6
...
@@ -322,7 +322,7 @@ var timer *time.Timer
...
@@ -322,7 +322,7 @@ var timer *time.Timer
// startAlarm starts an alarm if requested.
// startAlarm starts an alarm if requested.
func
startAlarm
()
{
func
startAlarm
()
{
if
*
timeout
>
0
{
if
*
timeout
>
0
{
timer
=
time
.
AfterFunc
(
*
timeout
*
1e9
,
alarm
)
timer
=
time
.
AfterFunc
(
time
.
Duration
(
*
timeout
)
*
time
.
Second
,
alarm
)
}
}
}
}
...
...
src/pkg/time/sleep.go
View file @
2949f3b6
...
@@ -72,13 +72,13 @@ func After(d Duration) <-chan Time {
...
@@ -72,13 +72,13 @@ func After(d Duration) <-chan Time {
return
NewTimer
(
d
)
.
C
return
NewTimer
(
d
)
.
C
}
}
// AfterFunc waits
at least ns nanoseconds before calling
f
// AfterFunc waits
for the duration to elapse and then calls
f
// in its own goroutine. It returns a Timer that can
// in its own goroutine. It returns a Timer that can
// be used to cancel the call using its Stop method.
// be used to cancel the call using its Stop method.
func
AfterFunc
(
ns
int64
,
f
func
())
*
Timer
{
func
AfterFunc
(
d
Duration
,
f
func
())
*
Timer
{
t
:=
&
Timer
{
t
:=
&
Timer
{
r
:
runtimeTimer
{
r
:
runtimeTimer
{
when
:
nano
()
+
ns
,
when
:
nano
()
+
int64
(
d
)
,
f
:
goFunc
,
f
:
goFunc
,
arg
:
f
,
arg
:
f
,
},
},
...
...
src/pkg/time/sleep_test.go
View file @
2949f3b6
...
@@ -39,7 +39,7 @@ func TestAfterFunc(t *testing.T) {
...
@@ -39,7 +39,7 @@ func TestAfterFunc(t *testing.T) {
i
--
i
--
if
i
>=
0
{
if
i
>=
0
{
AfterFunc
(
0
,
f
)
AfterFunc
(
0
,
f
)
Sleep
(
1
e9
)
Sleep
(
1
*
Second
)
}
else
{
}
else
{
c
<-
true
c
<-
true
}
}
...
@@ -91,7 +91,7 @@ func BenchmarkAfter(b *testing.B) {
...
@@ -91,7 +91,7 @@ func BenchmarkAfter(b *testing.B) {
func
BenchmarkStop
(
b
*
testing
.
B
)
{
func
BenchmarkStop
(
b
*
testing
.
B
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
NewTimer
(
1
e9
)
.
Stop
()
NewTimer
(
1
*
Second
)
.
Stop
()
}
}
}
}
...
@@ -126,12 +126,11 @@ func TestAfterTick(t *testing.T) {
...
@@ -126,12 +126,11 @@ func TestAfterTick(t *testing.T) {
}
}
func
TestAfterStop
(
t
*
testing
.
T
)
{
func
TestAfterStop
(
t
*
testing
.
T
)
{
const
msec
=
1e6
AfterFunc
(
100
*
Millisecond
,
func
()
{})
AfterFunc
(
100
*
msec
,
func
()
{})
t0
:=
NewTimer
(
50
*
Millisecond
)
t0
:=
NewTimer
(
50
*
msec
)
c1
:=
make
(
chan
bool
,
1
)
c1
:=
make
(
chan
bool
,
1
)
t1
:=
AfterFunc
(
150
*
msec
,
func
()
{
c1
<-
true
})
t1
:=
AfterFunc
(
150
*
Millisecond
,
func
()
{
c1
<-
true
})
c2
:=
After
(
200
*
msec
)
c2
:=
After
(
200
*
Millisecond
)
if
!
t0
.
Stop
()
{
if
!
t0
.
Stop
()
{
t
.
Fatalf
(
"failed to stop event 0"
)
t
.
Fatalf
(
"failed to stop event 0"
)
}
}
...
@@ -212,12 +211,12 @@ func TestTimerStopStress(t *testing.T) {
...
@@ -212,12 +211,12 @@ func TestTimerStopStress(t *testing.T) {
}
}
for
i
:=
0
;
i
<
100
;
i
++
{
for
i
:=
0
;
i
<
100
;
i
++
{
go
func
(
i
int
)
{
go
func
(
i
int
)
{
timer
:=
AfterFunc
(
2
e9
,
func
()
{
timer
:=
AfterFunc
(
2
*
Second
,
func
()
{
t
.
Fatalf
(
"timer %d was not stopped"
,
i
)
t
.
Fatalf
(
"timer %d was not stopped"
,
i
)
})
})
Sleep
(
1
e9
)
Sleep
(
1
*
Second
)
timer
.
Stop
()
timer
.
Stop
()
}(
i
)
}(
i
)
}
}
Sleep
(
3
e9
)
Sleep
(
3
*
Second
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment