summaryrefslogtreecommitdiff
path: root/src/pkg/net/fd_posix_test.go
blob: 8be0335d61ce228bc9384c3fceeab60cc2883a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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)
		}
	}
}