This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
io_test.go
111 lines (86 loc) · 1.89 KB
/
io_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package dendrite
import (
"bytes"
"io/ioutil"
"net"
"net/url"
"os"
"testing"
)
func check(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
func TestNewFileReadWriter(t *testing.T) {
if err := os.RemoveAll("tmp.out"); err != nil {
t.Fatal(err)
}
hai := []byte("hai world")
u, err := url.Parse("file+json://tmp.out")
check(t, err)
rw, err := NewReadWriter(u)
check(t, err)
n, err := rw.Write(hai)
check(t, err)
if n != 9 {
t.Fatal("wrote wrong bytes")
}
rw.Close()
out, err := ioutil.ReadFile("tmp.out")
check(t, err)
if bytes.Compare(out, hai) != 0 {
t.Fatal("didn't read back bytes", out, hai)
}
}
func TestNewUDPReadWriter(t *testing.T) {
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:4009")
check(t, err)
conn, err := net.ListenUDP("udp", addr)
hai := []byte("hai world")
u, err := url.Parse("udp+json://localhost:4009")
check(t, err)
rw, err := NewReadWriter(u)
check(t, err)
n, err := rw.Write(hai)
check(t, err)
if n != 9 {
t.Fatal("wrote wrong bytes")
}
out := make([]byte, 100)
n, addr, err = conn.ReadFromUDP(out)
if n != 9 {
t.Fatal("read wrong bytes", n)
}
if bytes.Compare(out[0:n], hai) != 0 {
t.Fatal("didn't read back bytes", out, hai)
}
}
func connHandler(t *testing.T, ln net.Listener, ch chan []byte) {
conn, err := ln.Accept()
check(t, err)
buf := make([]byte, 100)
n, err := conn.Read(buf)
check(t, err)
ch <- buf[0:n]
}
func TestNewTCPReadWriter(t *testing.T) {
ch := make(chan []byte, 1)
conn, err := net.Listen("tcp", "127.0.0.1:4009")
go connHandler(t, conn, ch)
hai := []byte("hai world")
u, err := url.Parse("tcp+json://localhost:4009")
check(t, err)
rw, err := NewReadWriter(u)
check(t, err)
n, err := rw.Write(hai)
check(t, err)
if n != 9 {
t.Fatal("wrote wrong bytes")
}
rw.Close()
out := <-ch
if bytes.Compare(out[0:n], hai) != 0 {
t.Fatal("didn't read back bytes", out, hai)
}
}