aboutsummaryrefslogtreecommitdiff
path: root/net_test.go
blob: 487dd58916429aaafba44ab580b1a6a547090c41 (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
58
59
// SPDX-FileCopyrightText: © 2022 Grégoire Duchêne <gduchene@awhk.org>
// SPDX-License-Identifier: ISC

package core_test

import (
	"context"
	"syscall"
	"testing"

	"go.awhk.org/core"
)

func TestPipeListener(s *testing.T) {
	t := core.T{T: s}

	t.Run("Success", func(t *core.T) {
		p := core.ListenPipe()

		t.Go(func() {
			conn, err := p.Accept()
			t.AssertErrorIs(nil, err)
			t.AssertNotEqual(nil, conn)
		})

		conn, err := p.Dial("", "")
		t.AssertErrorIs(nil, err)
		t.AssertNotEqual(nil, conn)
	})

	t.Run("WhenClosed", func(t *core.T) {
		p := core.ListenPipe()
		p.Close()

		conn, err := p.Accept()
		t.AssertErrorIs(syscall.EINVAL, err)
		t.AssertEqual(nil, conn)

		conn, err = p.Dial("", "")
		t.AssertErrorIs(syscall.ECONNREFUSED, err)
		t.AssertEqual(nil, conn)
	})

	t.Run("WhenClosedTwice", func(t *core.T) {
		p := core.ListenPipe()
		t.AssertEqual(nil, p.Close())
		t.AssertEqual(syscall.EINVAL, p.Close())
	})

	t.Run("WhenContextCanceled", func(t *core.T) {
		p := core.ListenPipe()

		ctx, cancel := context.WithCancel(context.Background())
		cancel()
		conn, err := p.DialContext(ctx, "", "")
		t.AssertErrorIs(context.Canceled, err)
		t.AssertEqual(nil, conn)
	})
}