mirror of
https://github.com/golang/go
synced 2024-10-05 12:21:22 -06:00
9fb96991e6
Fixes #4434. This proposal replaces the previous CL 6855110. Due to issue 599, 64-bit atomic operations should probably be avoided, so use a sync.Mutex instead. Benchmark comparisons against 025b9d070a85 on linux/386: CL 6855110: benchmark old ns/op new ns/op delta BenchmarkTCPOneShot 710024 727409 +2.45% BenchmarkTCPOneShotTimeout 758178 768620 +1.38% BenchmarkTCPPersistent 223464 228058 +2.06% BenchmarkTCPPersistentTimeout 234494 242600 +3.46% This proposal: benchmark old ns/op new ns/op delta BenchmarkTCPOneShot 710024 718492 +1.19% BenchmarkTCPOneShotTimeout 758178 748783 -1.24% BenchmarkTCPPersistent 223464 227628 +1.86% BenchmarkTCPPersistentTimeout 234494 238321 +1.63% R=rsc, dvyukov, mikioh.mikioh, alex.brainman, bradfitz CC=golang-dev, remyoudompheng https://golang.org/cl/6866050
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
|
|
|
package net
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var deadlineSetTimeTests = []struct {
|
|
input time.Time
|
|
expected int64
|
|
}{
|
|
{time.Time{}, 0},
|
|
{time.Date(2009, 11, 10, 23, 00, 00, 00, time.UTC), 1257894000000000000}, // 2009-11-10 23:00:00 +0000 UTC
|
|
}
|
|
|
|
func TestDeadlineSetTime(t *testing.T) {
|
|
for _, tt := range deadlineSetTimeTests {
|
|
var d deadline
|
|
d.setTime(tt.input)
|
|
actual := d.value()
|
|
expected := int64(0)
|
|
if !tt.input.IsZero() {
|
|
expected = tt.input.UnixNano()
|
|
}
|
|
if actual != expected {
|
|
t.Errorf("set/value failed: expected %v, actual %v", expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
var deadlineExpiredTests = []struct {
|
|
deadline time.Time
|
|
expired bool
|
|
}{
|
|
// note, times are relative to the start of the test run, not
|
|
// the start of TestDeadlineExpired
|
|
{time.Now().Add(5 * time.Minute), false},
|
|
{time.Now().Add(-5 * time.Minute), true},
|
|
{time.Time{}, false}, // no deadline set
|
|
}
|
|
|
|
func TestDeadlineExpired(t *testing.T) {
|
|
for _, tt := range deadlineExpiredTests {
|
|
var d deadline
|
|
d.set(tt.deadline.UnixNano())
|
|
expired := d.expired()
|
|
if expired != tt.expired {
|
|
t.Errorf("expire failed: expected %v, actual %v", tt.expired, expired)
|
|
}
|
|
}
|
|
}
|